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