View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.impl;
18  
19  import java.net.URI;
20  
21  import org.apache.logging.log4j.core.LifeCycle;
22  import org.apache.logging.log4j.core.LoggerContext;
23  import org.apache.logging.log4j.core.config.Configuration;
24  import org.apache.logging.log4j.core.config.ConfigurationFactory;
25  import org.apache.logging.log4j.core.config.ConfigurationSource;
26  import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
27  import org.apache.logging.log4j.core.selector.ContextSelector;
28  import org.apache.logging.log4j.core.util.Constants;
29  import org.apache.logging.log4j.core.util.Loader;
30  import org.apache.logging.log4j.spi.LoggerContextFactory;
31  import org.apache.logging.log4j.status.StatusLogger;
32  import org.apache.logging.log4j.util.PropertiesUtil;
33  
34  /**
35   * Factory to locate a ContextSelector and then load a LoggerContext.
36   */
37  public class Log4jContextFactory implements LoggerContextFactory {
38  
39      private static final StatusLogger LOGGER = StatusLogger.getLogger();
40  
41      private ContextSelector selector;
42  
43      /**
44       * Initializes the ContextSelector.
45       */
46      public Log4jContextFactory() {
47          final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
48          if (sel != null) {
49              try {
50                  selector = Loader.newCheckedInstanceOf(sel, ContextSelector.class);
51              } catch (final Exception ex) {
52                  LOGGER.error("Unable to create context {}", sel, ex);
53              }
54          }
55          if (selector == null) {
56              selector = new ClassLoaderContextSelector();
57          }
58      }
59  
60      /**
61       * Loads the LoggerContext using the ContextSelector.
62       * @param fqcn The fully qualified class name of the caller.
63       * @param loader The ClassLoader to use or null.
64       * @param currentContext If true returns the current Context, if false returns the Context appropriate
65       * for the caller if a more appropriate Context can be determined.
66       * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
67       * @return The LoggerContext.
68       */
69      @Override
70      public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
71                                      final boolean currentContext) {
72          final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
73          ctx.setExternalContext(externalContext);
74          if (ctx.getState() == LifeCycle.State.INITIALIZED) {
75              ctx.start();
76          }
77          return ctx;
78      }
79  
80      /**
81       * Loads the LoggerContext using the ContextSelector.
82       * @param fqcn The fully qualified class name of the caller.
83       * @param loader The ClassLoader to use or null.
84       * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
85       * @param currentContext If true returns the current Context, if false returns the Context appropriate
86       * for the caller if a more appropriate Context can be determined.
87       * @param source The configuration source.
88       * @return The LoggerContext.
89       */
90      public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
91                                      final boolean currentContext, final ConfigurationSource source) {
92          final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
93          if (externalContext != null && ctx.getExternalContext() == null) {
94              ctx.setExternalContext(externalContext);
95          }
96          if (ctx.getState() == LifeCycle.State.INITIALIZED) {
97              if (source != null) {
98                  ContextAnchor.THREAD_CONTEXT.set(ctx);
99                  final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
100                 LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source);
101                 ctx.start(config);
102                 ContextAnchor.THREAD_CONTEXT.remove();
103             } else {
104                 ctx.start();
105             }
106         }
107         return ctx;
108     }
109 
110     /**
111      * Loads the LoggerContext using the ContextSelector.
112      * @param fqcn The fully qualified class name of the caller.
113      * @param loader The ClassLoader to use or null.
114      * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
115      * @param currentContext If true returns the current Context, if false returns the Context appropriate
116      * for the caller if a more appropriate Context can be determined.
117      * @param configLocation The location of the configuration for the LoggerContext.
118      * @return The LoggerContext.
119      */
120     @Override
121     public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
122                                     final boolean currentContext, final URI configLocation, final String name) {
123         final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
124         if (externalContext != null && ctx.getExternalContext() == null) {
125             ctx.setExternalContext(externalContext);
126         }
127         if (ctx.getState() == LifeCycle.State.INITIALIZED) {
128             if (configLocation != null || name != null) {
129                 ContextAnchor.THREAD_CONTEXT.set(ctx);
130                 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
131                 LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation);
132                 ctx.start(config);
133                 ContextAnchor.THREAD_CONTEXT.remove();
134             } else {
135                 ctx.start();
136             }
137         }
138         return ctx;
139     }
140 
141     /**
142      * Returns the ContextSelector.
143      * @return The ContextSelector.
144      */
145     public ContextSelector getSelector() {
146         return selector;
147     }
148 
149     /**
150      * Removes knowledge of a LoggerContext.
151      *
152      * @param context The context to remove.
153      */
154     @Override
155     public void removeContext(final org.apache.logging.log4j.spi.LoggerContext context) {
156         if (context instanceof LoggerContext) {
157             selector.removeContext((LoggerContext) context);
158         }
159     }
160 }