View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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,
56        InvocationTargetException  {
57      if(this.key == null) {
58        this.key = key;
59      } else if (this.key != key) {
60        throw new IllegalAccessException("Only certain classes can access this method.");
61      }
62      
63      
64      String contextSelectorStr = OptionHelper
65          .getSystemProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR);
66      if (contextSelectorStr == null) {
67        contextSelector = new DefaultContextSelector(defaultLoggerContext);
68      } else if (contextSelectorStr.equals("JNDI")) {
69        // if jndi is specified, let's use the appropriate class
70        contextSelector = new ContextJNDISelector(defaultLoggerContext);
71      } else {
72        contextSelector = dynamicalContextSelector(defaultLoggerContext,
73            contextSelectorStr);
74      }
75    }
76    
77    /**
78     * Instantiate the context selector class designated by the user. The selector
79     * must have a constructor taking a LoggerContext instance as an argument.
80     * 
81     * @param defaultLoggerContext
82     * @param contextSelectorStr
83     * @return an instance of the designated context selector class
84     * @throws ClassNotFoundException
85     * @throws SecurityException
86     * @throws NoSuchMethodException
87     * @throws IllegalArgumentException
88     * @throws InstantiationException
89     * @throws IllegalAccessException
90     * @throws InvocationTargetException
91     */
92    static ContextSelector dynamicalContextSelector(
93        LoggerContext defaultLoggerContext, String contextSelectorStr)
94        throws ClassNotFoundException, SecurityException, NoSuchMethodException,
95        IllegalArgumentException, InstantiationException, IllegalAccessException,
96        InvocationTargetException {
97      Class<?> contextSelectorClass = Loader.loadClass(contextSelectorStr);
98      Constructor cons = contextSelectorClass
99          .getConstructor(new Class[] { LoggerContext.class });
100     return (ContextSelector) cons.newInstance(defaultLoggerContext);
101   }
102   
103   public ContextSelector getContextSelector() {
104     return contextSelector;
105   }
106 
107 }