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.impl;
018    
019    import org.apache.logging.log4j.core.LoggerContext;
020    import org.apache.logging.log4j.core.helpers.Constants;
021    import org.apache.logging.log4j.core.helpers.Loader;
022    import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
023    import org.apache.logging.log4j.core.selector.ContextSelector;
024    import org.apache.logging.log4j.status.StatusLogger;
025    import org.apache.logging.log4j.spi.LoggerContextFactory;
026    
027    /**
028     * Factory to locate a ContextSelector and then load a LoggerContext.
029     */
030    public class Log4jContextFactory implements LoggerContextFactory {
031    
032        private static final StatusLogger logger = StatusLogger.getLogger();
033    
034        private ContextSelector selector;
035    
036        /**
037         * Constructor that initializes the ContextSelector.
038         */
039        public Log4jContextFactory() {
040            String sel = System.getProperty(Constants.LOG4J_CONTEXT_SELECTOR);
041            if (sel != null) {
042                try {
043                    Class clazz = Loader.loadClass(sel);
044                    if (clazz != null && ContextSelector.class.isAssignableFrom(clazz)) {
045                        selector = (ContextSelector) clazz.newInstance();
046                        return;
047                    }
048                } catch (Exception ex) {
049                    logger.error("Unable to create context " + sel, ex);
050                }
051    
052            }
053            selector = new ClassLoaderContextSelector();
054        }
055    
056        /**
057         * Returns the ContextSelector.
058         * @return The ContextSelector.
059         */
060        public ContextSelector getSelector() {
061            return selector;
062        }
063    
064        /**
065         * Load the LoggerContext using the ContextSelector.
066         * @param fqcn The fully qualified class name of the caller.
067         * @param loader The ClassLoader to use or null.
068         * @param currentContext If true returns the current Context, if false returns the Context appropriate
069         * for the caller if a more appropriate Context can be determined.
070         * @return The LoggerContext.
071         */
072        public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
073            LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
074            if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
075                ctx.start();
076            }
077            return ctx;
078        }
079    }