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