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 */
017package org.apache.logging.log4j.core.config.json;
018
019import org.apache.logging.log4j.core.config.Configuration;
020import org.apache.logging.log4j.core.config.ConfigurationFactory;
021import org.apache.logging.log4j.core.config.ConfigurationSource;
022import org.apache.logging.log4j.core.config.Order;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.util.Loader;
025
026/**
027 *
028 */
029@Plugin(name = "JsonConfigurationFactory", category = ConfigurationFactory.CATEGORY)
030@Order(6)
031public class JsonConfigurationFactory extends ConfigurationFactory {
032
033    /**
034     * The file extensions supported by this factory.
035     */
036    private static final String[] SUFFIXES = new String[] {".json", ".jsn"};
037
038    private static final String[] dependencies = new String[] {
039            "com.fasterxml.jackson.databind.ObjectMapper",
040            "com.fasterxml.jackson.databind.JsonNode",
041            "com.fasterxml.jackson.core.JsonParser"
042    };
043
044    private final boolean isActive;
045
046    public JsonConfigurationFactory() {
047        for (final String dependency : dependencies) {
048            if (!Loader.isClassAvailable(dependency)) {
049                LOGGER.debug("Missing dependencies for Json support");
050                isActive = false;
051                return;
052            }
053        }
054        isActive = true;
055    }
056
057    @Override
058    protected boolean isActive() {
059        return isActive;
060    }
061
062    @Override
063    public Configuration getConfiguration(final ConfigurationSource source) {
064        if (!isActive) {
065            return null;
066        }
067        return new JsonConfiguration(source);
068    }
069
070    @Override
071    public String[] getSupportedTypes() {
072        return SUFFIXES;
073    }
074}