1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
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.NamingException;
27
28 import org.apache.logging.log4j.core.LoggerContext;
29 import org.apache.logging.log4j.core.impl.ContextAnchor;
30 import org.apache.logging.log4j.core.net.JndiManager;
31 import org.apache.logging.log4j.core.util.Constants;
32 import org.apache.logging.log4j.status.StatusLogger;
33
34 /**
35 * This class can be used to define a custom logger repository. It makes use of the fact that in J2EE environments, each
36 * web-application is guaranteed to have its own JNDI context relative to the <code>java:comp/env</code> context. In
37 * EJBs, each enterprise bean (albeit not each application) has its own context relative to the
38 * <code>java:comp/env</code> context. An <code>env-entry</code> in a deployment descriptor provides the information to
39 * the JNDI context. Once the <code>env-entry</code> is set, a repository selector can query the JNDI application
40 * context to look up the value of the entry. The logging context of the web-application will depend on the value the
41 * env-entry. The JNDI context which is looked up by this class is <code>java:comp/env/log4j/context-name</code>.
42 *
43 * <p>
44 * Here is an example of an <code>env-entry</code>:
45 * </p>
46 * <blockquote>
47 *
48 * <pre>
49 * <env-entry>
50 * <description>JNDI logging context name for this app</description>
51 * <env-entry-name>log4j/context-name</env-entry-name>
52 * <env-entry-value>aDistinctiveLoggingContextName</env-entry-value>
53 * <env-entry-type>java.lang.String</env-entry-type>
54 * </env-entry>
55 * </pre>
56 *
57 * </blockquote>
58 *
59 * <p>
60 * <em>If multiple applications use the same logging context name, then they
61 * will share the same logging context.</em>
62 * </p>
63 *
64 * <p>
65 * You can also specify the URL for this context's configuration resource. This repository selector
66 * (ContextJNDISelector) will use this resource to automatically configure the log4j repository.
67 * </p>
68 ** <blockquote>
69 *
70 * <pre>
71 * <env-entry>
72 * <description>URL for configuring log4j context</description>
73 * <env-entry-name>log4j/configuration-resource</env-entry-name>
74 * <env-entry-value>urlOfConfigurationResource</env-entry-value>
75 * <env-entry-type>java.lang.String</env-entry-type>
76 * </env-entry>
77 * </pre>
78 *
79 * </blockquote>
80 *
81 * <p>
82 * It usually good practice for configuration resources of distinct applications to have distinct names. However, if
83 * this is not possible Naming
84 * </p>
85 */
86 public class JndiContextSelector implements NamedContextSelector {
87
88 private static final LoggerContext CONTEXT = new LoggerContext("Default");
89
90 private static final ConcurrentMap<String, LoggerContext> CONTEXT_MAP =
91 new ConcurrentHashMap<String, LoggerContext>();
92
93 private static final StatusLogger LOGGER = StatusLogger.getLogger();
94
95 public JndiContextSelector() {
96 if (!JndiManager.isJndiContextSelectorEnabled()) {
97 throw new IllegalStateException("JNDI must be enabled by setting log4j2.enableJndiContextSelector=true");
98 }
99 }
100
101 @Override
102 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
103 return getContext(fqcn, loader, currentContext, null);
104 }
105
106 @Override
107 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
108 final URI configLocation) {
109
110 final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
111 if (lc != null) {
112 return lc;
113 }
114
115 String loggingContextName = null;
116
117 final JndiManager jndiManager = JndiManager.getDefaultManager();
118 try {
119 loggingContextName = jndiManager.lookup(Constants.JNDI_CONTEXT_NAME);
120 } catch (final NamingException ne) {
121 LOGGER.error("Unable to lookup {}", Constants.JNDI_CONTEXT_NAME, ne);
122 } finally {
123 jndiManager.release();
124 }
125
126 return loggingContextName == null ? CONTEXT : locateContext(loggingContextName, null, configLocation);
127 }
128
129 @Override
130 public LoggerContext locateContext(final String name, final Object externalContext, final URI configLocation) {
131 if (name == null) {
132 LOGGER.error("A context name is required to locate a LoggerContext");
133 return null;
134 }
135 if (!CONTEXT_MAP.containsKey(name)) {
136 final LoggerContext ctx = new LoggerContext(name, externalContext, configLocation);
137 CONTEXT_MAP.putIfAbsent(name, ctx);
138 }
139 return CONTEXT_MAP.get(name);
140 }
141
142 @Override
143 public void removeContext(final LoggerContext context) {
144
145 for (final Map.Entry<String, LoggerContext> entry : CONTEXT_MAP.entrySet()) {
146 if (entry.getValue().equals(context)) {
147 CONTEXT_MAP.remove(entry.getKey());
148 }
149 }
150 }
151
152 @Override
153 public LoggerContext removeContext(final String name) {
154 return CONTEXT_MAP.remove(name);
155 }
156
157 @Override
158 public List<LoggerContext> getLoggerContexts() {
159 final List<LoggerContext> list = new ArrayList<LoggerContext>(CONTEXT_MAP.values());
160 return Collections.unmodifiableList(list);
161 }
162
163 }