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 org.apache.logging.log4j.core.LoggerContext;
20  import org.apache.logging.log4j.core.helpers.Constants;
21  import org.apache.logging.log4j.core.helpers.Loader;
22  import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
23  import org.apache.logging.log4j.core.selector.ContextSelector;
24  import org.apache.logging.log4j.status.StatusLogger;
25  import org.apache.logging.log4j.spi.LoggerContextFactory;
26  
27  /**
28   * Factory to locate a ContextSelector and then load a LoggerContext.
29   */
30  public class Log4jContextFactory implements LoggerContextFactory {
31  
32      private ContextSelector selector;
33  
34      private StatusLogger logger = StatusLogger.getLogger();
35  
36      private ThreadLocal<Log4jContextFactory> recursive = new ThreadLocal<Log4jContextFactory>();
37  
38      /**
39       * Constructor that initializes the ContextSelector.
40       */
41      public Log4jContextFactory() {
42          String sel = System.getProperty(Constants.LOG4J_CONTEXT_SELECTOR);
43          if (sel != null) {
44              try {
45                  Class clazz = Loader.loadClass(sel);
46                  if (clazz != null && ContextSelector.class.isAssignableFrom(clazz)) {
47                      selector = (ContextSelector) clazz.newInstance();
48                      return;
49                  }
50              } catch (Exception ex) {
51                  logger.error("Unable to create context " + sel, ex);
52              }
53  
54          }
55          selector = new ClassLoaderContextSelector();
56      }
57  
58      /**
59       * Return the ContextSelector.
60       * @return The ContextSelector.
61       */
62      public ContextSelector getSelector() {
63          return selector;
64      }
65  
66      /**
67       * Load the LoggerContext using the ContextSelector.
68       * @param fqcn The fully qualified class name of the caller.
69       * @param currentContext If true returns the current Context, if false returns the Context appropriate
70       * for the caller if a more appropriate Context can be determined.
71       * @return The LoggerContext.
72       */
73      public LoggerContext getContext(String fqcn, boolean currentContext) {
74          LoggerContext ctx = selector.getContext(fqcn, currentContext);
75          synchronized (ctx) {
76              if (recursive.get() != null || ctx.isStarted()) {
77                  return ctx;
78              }
79              try {
80                  recursive.set(this);
81                  ctx.start();
82                  return ctx;
83              } finally {
84                  recursive.remove();
85              }
86          }
87      }
88  }