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.net.URI;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
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
88 public class JNDIContextSelector implements NamedContextSelector {
89
90 private static final LoggerContext CONTEXT = new LoggerContext("Default");
91
92 private static final ConcurrentMap<String, LoggerContext> CONTEXT_MAP =
93 new ConcurrentHashMap<String, LoggerContext>();
94
95 private static final StatusLogger LOGGER = StatusLogger.getLogger();
96
97 @Override
98 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
99 return getContext(fqcn, loader, currentContext, null);
100 }
101
102 @Override
103 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
104 URI configLocation) {
105
106 final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
107 if (lc != null) {
108 return lc;
109 }
110
111 String loggingContextName = null;
112
113 try {
114 final Context ctx = new InitialContext();
115 loggingContextName = (String) lookup(ctx, Constants.JNDI_CONTEXT_NAME);
116 } catch (final NamingException ne) {
117 LOGGER.error("Unable to lookup " + Constants.JNDI_CONTEXT_NAME, ne);
118 }
119
120 return loggingContextName == null ? CONTEXT : locateContext(loggingContextName, configLocation);
121 }
122
123 @Override
124 public LoggerContext locateContext(final String name, final URI configLocation) {
125 if (name == null) {
126 LOGGER.error("A context name is required to locate a LoggerContext");
127 return null;
128 }
129 if (!CONTEXT_MAP.containsKey(name)) {
130 final LoggerContext ctx = new LoggerContext(name, null, configLocation);
131 CONTEXT_MAP.putIfAbsent(name, ctx);
132 }
133 return CONTEXT_MAP.get(name);
134 }
135
136 @Override
137 public void removeContext(final LoggerContext context) {
138
139 for (final Map.Entry<String, LoggerContext> entry : CONTEXT_MAP.entrySet()) {
140 if (entry.getValue().equals(context)) {
141 CONTEXT_MAP.remove(entry.getKey());
142 }
143 }
144 }
145
146 @Override
147 public LoggerContext removeContext(final String name) {
148 return CONTEXT_MAP.remove(name);
149 }
150
151 @Override
152 public List<LoggerContext> getLoggerContexts() {
153 final List<LoggerContext> list = new ArrayList<LoggerContext>(CONTEXT_MAP.values());
154 return Collections.unmodifiableList(list);
155 }
156
157
158 protected static Object lookup(final Context ctx, final String name) throws NamingException {
159 if (ctx == null) {
160 return null;
161 }
162 try {
163 return ctx.lookup(name);
164 } catch (final NameNotFoundException e) {
165 LOGGER.error("Could not find name [" + name + "].");
166 throw e;
167 }
168 }
169 }