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 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 * This class can be used to define a
38 * custom logger repository. It makes use of the fact that in J2EE
39 * environments, each web-application is guaranteed to have its own JNDI
40 * context relative to the <code>java:comp/env</code> context. In EJBs, each
41 * enterprise bean (albeit not each application) has its own context relative
42 * to the <code>java:comp/env</code> context. An <code>env-entry</code> in a
43 * deployment descriptor provides the information to the JNDI context. Once the
44 * <code>env-entry</code> is set, a repository selector can query the JNDI
45 * application context to look up the value of the entry. The logging context of
46 * the web-application will depend on the value the env-entry. The JNDI context
47 * which is looked up by this class is
48 * <code>java:comp/env/log4j/context-name</code>.
49 *
50 * <p>Here is an example of an <code>env-entry<code>:
51 * <blockquote>
52 * <pre>
53 * <env-entry>
54 * <description>JNDI logging context name for this app</description>
55 * <env-entry-name>log4j/context-name</env-entry-name>
56 * <env-entry-value>aDistinctiveLoggingContextName</env-entry-value>
57 * <env-entry-type>java.lang.String</env-entry-type>
58 * </env-entry>
59 * </pre>
60 * </blockquote>
61 * </p>
62 *
63 * <p><em>If multiple applications use the same logging context name, then they
64 * will share the same logging context.</em>
65 * </p>
66 *
67 *<p>You can also specify the URL for this context's configuration resource.
68 * This repository selector (ContextJNDISelector) will use this resource
69 * to automatically configure the log4j repository.
70 *</p>
71 ** <blockquote>
72 * <pre>
73 * <env-entry>
74 * <description>URL for configuring log4j context</description>
75 * <env-entry-name>log4j/configuration-resource</env-entry-name>
76 * <env-entry-value>urlOfConfigrationResource</env-entry-value>
77 * <env-entry-type>java.lang.String</env-entry-type>
78 * </env-entry>
79 * </pre>
80 * </blockquote>
81 *
82 * <p>It usually good practice for configuration resources of distinct
83 * applications to have distinct names. However, if this is not possible
84 * Naming
85 * </p>
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 }