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
18 package org.apache.logging.log4j.core.net;
19
20 import java.util.Properties;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26
27 import org.apache.logging.log4j.core.appender.AbstractManager;
28 import org.apache.logging.log4j.core.appender.ManagerFactory;
29 import org.apache.logging.log4j.core.util.JndiCloser;
30
31 /**
32 * Manages a JNDI {@link javax.naming.Context}.
33 *
34 * @since 2.1
35 */
36 public class JndiManager extends AbstractManager {
37
38 private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
39
40 private final Context context;
41
42 private JndiManager(final String name, final Context context) {
43 super(null, name);
44 this.context = context;
45 }
46
47 /**
48 * Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
49 *
50 * @return the default JndiManager
51 */
52 public static JndiManager getDefaultManager() {
53 return getManager(JndiManager.class.getName(), FACTORY, null);
54 }
55
56 /**
57 * Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
58 *
59 * @param name the name of the JndiManager instance to create or use if available
60 * @return a default JndiManager
61 */
62 public static JndiManager getDefaultManager(final String name) {
63 return getManager(name, FACTORY, null);
64 }
65
66 /**
67 * Gets a JndiManager with the provided configuration information.
68 *
69 * @param initialContextFactoryName Fully qualified class name of an implementation of
70 * {@link javax.naming.spi.InitialContextFactory}.
71 * @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
72 * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
73 * class that will create a URL context factory
74 * @param securityPrincipal The name of the identity of the Principal.
75 * @param securityCredentials The security credentials of the Principal.
76 * @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
77 * @return the JndiManager for the provided parameters.
78 */
79 public static JndiManager getJndiManager(final String initialContextFactoryName,
80 final String providerURL,
81 final String urlPkgPrefixes,
82 final String securityPrincipal,
83 final String securityCredentials,
84 final Properties additionalProperties) {
85 final Properties properties = createProperties(initialContextFactoryName, providerURL, urlPkgPrefixes,
86 securityPrincipal, securityCredentials, additionalProperties);
87 return getManager(createManagerName(), FACTORY, properties);
88 }
89
90 /**
91 * Gets a JndiManager with the provided configuration information.
92 *
93 * @param properties JNDI properties, usually created by calling {@link #createProperties(String, String, String, String, String, Properties)}.
94 * @return the JndiManager for the provided parameters.
95 * @see #createProperties(String, String, String, String, String, Properties)
96 * @since 2.9
97 */
98 public static JndiManager getJndiManager(final Properties properties) {
99 return getManager(createManagerName(), FACTORY, properties);
100 }
101
102 private static String createManagerName() {
103 return JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
104 }
105
106 /**
107 * Creates JNDI Properties with the provided configuration information.
108 *
109 * @param initialContextFactoryName
110 * Fully qualified class name of an implementation of {@link javax.naming.spi.InitialContextFactory}.
111 * @param providerURL
112 * The provider URL to use for the JNDI connection (specific to the above factory).
113 * @param urlPkgPrefixes
114 * A colon-separated list of package prefixes for the class name of the factory class that will create a
115 * URL context factory
116 * @param securityPrincipal
117 * The name of the identity of the Principal.
118 * @param securityCredentials
119 * The security credentials of the Principal.
120 * @param additionalProperties
121 * Any additional JNDI environment properties to set or {@code null} for none.
122 * @return the Properties for the provided parameters.
123 * @since 2.9
124 */
125 public static Properties createProperties(final String initialContextFactoryName, final String providerURL,
126 final String urlPkgPrefixes, final String securityPrincipal, final String securityCredentials,
127 final Properties additionalProperties) {
128 if (initialContextFactoryName == null) {
129 return null;
130 }
131 final Properties properties = new Properties();
132 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
133 if (providerURL != null) {
134 properties.setProperty(Context.PROVIDER_URL, providerURL);
135 } else {
136 LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated "
137 + "provider URL. This is likely to cause problems.", initialContextFactoryName);
138 }
139 if (urlPkgPrefixes != null) {
140 properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
141 }
142 if (securityPrincipal != null) {
143 properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
144 if (securityCredentials != null) {
145 properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
146 } else {
147 LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
148 securityPrincipal);
149 }
150 }
151 if (additionalProperties != null) {
152 properties.putAll(additionalProperties);
153 }
154 return properties;
155 }
156
157 @Override
158 protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
159 return JndiCloser.closeSilently(this.context);
160 }
161
162 /**
163 * Looks up a named object through this JNDI context.
164 *
165 * @param name name of the object to look up.
166 * @param <T> the type of the object.
167 * @return the named object if it could be located.
168 * @throws NamingException if a naming exception is encountered
169 */
170 @SuppressWarnings("unchecked")
171 public <T> T lookup(final String name) throws NamingException {
172 return (T) this.context.lookup(name);
173 }
174
175 private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
176
177 @Override
178 public JndiManager createManager(final String name, final Properties data) {
179 try {
180 return new JndiManager(name, new InitialContext(data));
181 } catch (final NamingException e) {
182 LOGGER.error("Error creating JNDI InitialContext.", e);
183 return null;
184 }
185 }
186 }
187
188 @Override
189 public String toString() {
190 return "JndiManager [context=" + context + ", count=" + count + "]";
191 }
192
193 }