1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.selector;
18
19 import org.apache.logging.log4j.core.LoggerContext;
20 import org.apache.logging.log4j.core.helpers.Constants;
21 import org.apache.logging.log4j.core.impl.ContextAnchor;
22 import org.apache.logging.log4j.status.StatusLogger;
23
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NameNotFoundException;
27 import javax.naming.NamingException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.concurrent.ConcurrentMap;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public class JNDIContextSelector implements NamedContextSelector {
88
89 private static final LoggerContext context = new LoggerContext("Default");
90
91 private static final ConcurrentMap<String, LoggerContext> contextMap =
92 new ConcurrentHashMap<String, LoggerContext>();
93
94 private static final StatusLogger LOGGER = StatusLogger.getLogger();
95
96 public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
97
98 LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
99 if (lc != null) {
100 return lc;
101 }
102
103 String loggingContextName = null;
104
105 try {
106 Context ctx = new InitialContext();
107 loggingContextName = (String) lookup(ctx, Constants.JNDI_CONTEXT_NAME);
108 } catch (NamingException ne) {
109 LOGGER.error("Unable to lookup " + Constants.JNDI_CONTEXT_NAME, ne);
110 }
111
112 return loggingContextName == null ? context : locateContext(loggingContextName, null);
113 }
114
115 public LoggerContext locateContext(String name, String configLocation) {
116 if (name == null) {
117 LOGGER.error("A context name is required to locate a LoggerContext");
118 return null;
119 }
120 if (!contextMap.containsKey(name)) {
121 LoggerContext ctx = new LoggerContext(name, null, configLocation);
122 contextMap.putIfAbsent(name, ctx);
123 }
124 return contextMap.get(name);
125 }
126
127 public void removeContext(LoggerContext context) {
128
129 for (Map.Entry<String, LoggerContext> entry : contextMap.entrySet()) {
130 if (entry.getValue().equals(context)) {
131 contextMap.remove(entry.getKey());
132 }
133 }
134 }
135
136 public LoggerContext removeContext(String name) {
137 return contextMap.remove(name);
138 }
139
140 public List<LoggerContext> getLoggerContexts() {
141 List<LoggerContext> list = new ArrayList<LoggerContext>(contextMap.values());
142 return Collections.unmodifiableList(list);
143 }
144
145
146 protected static Object lookup(Context ctx, String name) throws NamingException {
147 if (ctx == null) {
148 return null;
149 }
150 try {
151 return ctx.lookup(name);
152 } catch (NameNotFoundException e) {
153 LOGGER.error("Could not find name [" + name + "].");
154 throw e;
155 }
156 }
157 }