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;
018    
019    import org.apache.logging.log4j.LogManager;
020    import org.apache.logging.log4j.core.LoggerContext;
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    
024    /**
025     * Initializes and configure the Logging system.
026     */
027    public final class Configurator {
028    
029        private Configurator() {
030        }
031    
032        /**
033         * Initializes the Logging Context.
034         * @param name The Context name.
035         * @param loader The ClassLoader for the Context (or null).
036         * @param configLocation The configuration for the logging context.
037         * @return The LoggerContext.
038         */
039        public static LoggerContext initialize(String name, ClassLoader loader, String configLocation) {
040    
041            try {
042                URI uri = configLocation == null ? null : new URI(configLocation);
043                return initialize(name, loader, uri);
044            } catch (URISyntaxException ex) {
045                ex.printStackTrace();
046            }
047            return null;
048        }
049    
050        /**
051         * Initializes the Logging Context.
052         * @param name The Context name.
053         * @param loader The ClassLoader for the Context (or null).
054         * @param configLocation The configuration for the logging context.
055         * @return The LoggerContext.
056         */
057        public static LoggerContext initialize(String name, ClassLoader loader, URI configLocation) {
058    
059            try {
060                LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
061                Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
062                ctx.setConfiguration(config);
063                return ctx;
064            } catch (Exception ex) {
065                ex.printStackTrace();
066            }
067            return null;
068        }
069    
070        /**
071         * Initializes the Logging Context.
072         * @param loader The ClassLoader for the Context (or null).
073         * @param source The InputSource for the configuration.
074         * @return The LoggerContext.
075         */
076        public static LoggerContext initialize(ClassLoader loader, ConfigurationFactory.ConfigurationSource source) {
077    
078            try {
079                LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
080                Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
081                ctx.setConfiguration(config);
082                return ctx;
083            } catch (Exception ex) {
084                ex.printStackTrace();
085            }
086            return null;
087        }
088    
089        /**
090         * Shuts down the given logging context.
091         * @param ctx the logging context to shut down, may be null.
092         */
093        public static void shutdown(LoggerContext ctx) {
094            if (ctx != null) {
095                ctx.setConfiguration(new DefaultConfiguration());
096            }
097        }
098    
099    }