1
2
3
4
5
6
7
8
9
10
11
12
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
29
30
31
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
46
47
48
49
50
51
52
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
70 contextSelector = new ContextJNDISelector(defaultLoggerContext);
71 } else {
72 contextSelector = dynamicalContextSelector(defaultLoggerContext,
73 contextSelectorStr);
74 }
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 }