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.LoggerContext;
22  import org.apache.logging.log4j.core.helpers.Constants;
23  import org.apache.logging.log4j.core.helpers.Loader;
24  import org.apache.logging.log4j.core.jmx.Server;
25  import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
26  import org.apache.logging.log4j.core.selector.ContextSelector;
27  import org.apache.logging.log4j.spi.LoggerContextFactory;
28  import org.apache.logging.log4j.status.StatusLogger;
29  import org.apache.logging.log4j.util.PropertiesUtil;
30  
31  /**
32   * Factory to locate a ContextSelector and then load a LoggerContext.
33   */
34  public class Log4jContextFactory implements LoggerContextFactory {
35  
36      private static final StatusLogger LOGGER = StatusLogger.getLogger();
37  
38      private ContextSelector selector;
39  
40      /**
41       * Constructor that initializes the ContextSelector.
42       */
43      public Log4jContextFactory() {
44          final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
45          if (sel != null) {
46              try {
47                  final Class<?> clazz = Loader.loadClass(sel);
48                  if (clazz != null && ContextSelector.class.isAssignableFrom(clazz)) {
49                      selector = (ContextSelector) clazz.newInstance();
50                  }
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          try {
59              Server.registerMBeans(selector);
60          } catch (Exception ex) {
61              LOGGER.error("Could not start JMX", ex);
62          }
63      }
64  
65      /**
66       * Returns the ContextSelector.
67       * @return The ContextSelector.
68       */
69      public ContextSelector getSelector() {
70          return selector;
71      }
72  
73      /**
74       * Load the LoggerContext using the ContextSelector.
75       * @param fqcn The fully qualified class name of the caller.
76       * @param loader The ClassLoader to use or null.
77       * @param currentContext If true returns the current Context, if false returns the Context appropriate
78       * for the caller if a more appropriate Context can be determined.
79       * @return The LoggerContext.
80       */
81      @Override
82      public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
83          final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
84          if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
85              ctx.start();
86          }
87          return ctx;
88      }
89  
90      /**
91       * Load the LoggerContext using the ContextSelector.
92       * @param fqcn The fully qualified class name of the caller.
93       * @param loader The ClassLoader to use or null.
94       * @param currentContext If true returns the current Context, if false returns the Context appropriate
95       * for the caller if a more appropriate Context can be determined.
96       * @param configLocation The location of the configuration for the LoggerContext.
97       * @return The LoggerContext.
98       */
99      @Override
100     public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
101             URI configLocation) {
102         final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
103         if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
104             ctx.start();
105         }
106         return ctx;
107     }
108 
109 
110     /**
111      * Removes knowledge of a LoggerContext.
112      * 
113      * @param context The context to remove.
114      */
115     @Override
116     public void removeContext(org.apache.logging.log4j.spi.LoggerContext context) {
117         if (context instanceof LoggerContext) {
118             selector.removeContext((LoggerContext) context);
119         }
120     }
121 }