    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.core.config.yaml;
    018
    019    import org.apache.logging.log4j.core.config.Configuration;
    020    import org.apache.logging.log4j.core.config.ConfigurationFactory;
    021    import org.apache.logging.log4j.core.config.ConfigurationSource;
    022    import org.apache.logging.log4j.core.config.Order;
    023    import org.apache.logging.log4j.core.config.plugins.Plugin;
    024    import org.apache.logging.log4j.core.util.Loader;
    025
    026    @Plugin(name = "YamlConfigurationFactory", category = ConfigurationFactory.CATEGORY)
    027    @Order(7)
    028    public class YamlConfigurationFactory extends ConfigurationFactory {
    029
    030        /**
    031         * The file extensions supported by this factory.
    032         */
    033        private static final String[] SUFFIXES = new String[] {".yml", ".yaml"};
    034
    035        private static final String[] dependencies = new String[] {
    036                "com.fasterxml.jackson.databind.ObjectMapper",
    037                "com.fasterxml.jackson.databind.JsonNode",
    038                "com.fasterxml.jackson.core.JsonParser",
    039                "com.fasterxml.jackson.dataformat.yaml.YAMLFactory"
    040        };
    041
    042        private final boolean isActive;
    043
    044        public YamlConfigurationFactory() {
    045            for (final String dependency : dependencies) {
    046                if (!Loader.isClassAvailable(dependency)) {
    047                    LOGGER.debug("Missing dependencies for Yaml support");
    048                    isActive = false;
    049                    return;
    050                }
    051            }
    052            isActive = true;
    053        }
    054
    055        @Override
    056        protected boolean isActive() {
    057            return isActive;
    058        }
    059
    060        @Override
    061        public Configuration getConfiguration(final ConfigurationSource source) {
    062            if (!isActive) {
    063                return null;
    064            }
    065            return new YamlConfiguration(source);
    066        }
    067
    068        @Override
    069        public String[] getSupportedTypes() {
    070            return SUFFIXES;
    071        }
    072
    073    }