View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.util;
15  
16  import java.lang.reflect.Constructor;
17  import java.lang.reflect.InvocationTargetException;
18  
19  import ch.qos.logback.classic.ClassicConstants;
20  import ch.qos.logback.classic.LoggerContext;
21  import ch.qos.logback.classic.selector.ContextJNDISelector;
22  import ch.qos.logback.classic.selector.ContextSelector;
23  import ch.qos.logback.classic.selector.DefaultContextSelector;
24  import ch.qos.logback.core.util.Loader;
25  import ch.qos.logback.core.util.OptionHelper;
26  
27  /**
28   * Holds the context selector for use in the current environment.
29   * 
30   * @author Ceki Gülcü
31   * @since 0.9.19
32   */
33  public class ContextSelectorStaticBinder {
34  
35      static ContextSelectorStaticBinder singleton = new ContextSelectorStaticBinder();
36  
37      ContextSelector contextSelector;
38      Object key;
39  
40      public static ContextSelectorStaticBinder getSingleton() {
41          return singleton;
42      }
43  
44      /**
45       * FOR INTERNAL USE. This method is intended for use by StaticLoggerBinder.
46       * 
47       * @param defaultLoggerContext
48       * @throws ClassNotFoundException
49       * @throws NoSuchMethodException
50       * @throws InstantiationException
51       * @throws IllegalAccessException
52       * @throws InvocationTargetException
53       */
54      public void init(LoggerContext defaultLoggerContext, Object key) throws ClassNotFoundException,
55              NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
56          if (this.key == null) {
57              this.key = key;
58          } else if (this.key != key) {
59              throw new IllegalAccessException("Only certain classes can access this method.");
60          }
61  
62          String contextSelectorStr = OptionHelper.getSystemProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR);
63          if (contextSelectorStr == null) {
64              contextSelector = new DefaultContextSelector(defaultLoggerContext);
65          } else if (contextSelectorStr.equals("JNDI")) {
66              // if jndi is specified, let's use the appropriate class
67              contextSelector = new ContextJNDISelector(defaultLoggerContext);
68          } else {
69              contextSelector = dynamicalContextSelector(defaultLoggerContext, contextSelectorStr);
70          }
71      }
72  
73      /**
74       * Instantiate the context selector class designated by the user. The selector
75       * must have a constructor taking a LoggerContext instance as an argument.
76       * 
77       * @param defaultLoggerContext
78       * @param contextSelectorStr
79       * @return an instance of the designated context selector class
80       * @throws ClassNotFoundException
81       * @throws SecurityException
82       * @throws NoSuchMethodException
83       * @throws IllegalArgumentException
84       * @throws InstantiationException
85       * @throws IllegalAccessException
86       * @throws InvocationTargetException
87       */
88      static ContextSelector dynamicalContextSelector(LoggerContext defaultLoggerContext, String contextSelectorStr)
89              throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
90              InstantiationException, IllegalAccessException, InvocationTargetException {
91          Class<?> contextSelectorClass = Loader.loadClass(contextSelectorStr);
92          Constructor<?> cons = contextSelectorClass.getConstructor(new Class[] { LoggerContext.class });
93          return (ContextSelector) cons.newInstance(defaultLoggerContext);
94      }
95  
96      public ContextSelector getContextSelector() {
97          return contextSelector;
98      }
99  
100 }