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(final String name, final ClassLoader loader, final String configLocation) {
040    
041            try {
042                final URI uri = configLocation == null ? null : new URI(configLocation);
043                return initialize(name, loader, uri);
044            } catch (final 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(final String name, final ClassLoader loader, final URI configLocation) {
058    
059            try {
060                final LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
061                final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
062                ctx.setConfiguration(config);
063                return ctx;
064            } catch (final 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(final ClassLoader loader,
077                                               final ConfigurationFactory.ConfigurationSource source) {
078    
079            try {
080                final LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
081                final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
082                ctx.setConfiguration(config);
083                return ctx;
084            } catch (final Exception ex) {
085                ex.printStackTrace();
086            }
087            return null;
088        }
089    
090        /**
091         * Shuts down the given logging context.
092         * @param ctx the logging context to shut down, may be null.
093         */
094        public static void shutdown(final LoggerContext ctx) {
095            if (ctx != null) {
096                ctx.setConfiguration(new DefaultConfiguration());
097            }
098        }
099    }