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, 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
67 contextSelector = new ContextJNDISelector(defaultLoggerContext);
68 } else {
69 contextSelector = dynamicalContextSelector(defaultLoggerContext, contextSelectorStr);
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 }