View Javadoc
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.net.URI;
21  import java.net.URISyntaxException;
22  import java.util.Properties;
23  import java.util.concurrent.TimeUnit;
24  
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  
29  import org.apache.logging.log4j.core.appender.AbstractManager;
30  import org.apache.logging.log4j.core.appender.ManagerFactory;
31  import org.apache.logging.log4j.core.util.JndiCloser;
32  import org.apache.logging.log4j.util.PropertiesUtil;
33  
34  /**
35   * Manages a JNDI {@link javax.naming.Context}.
36   *
37   * @since 2.1
38   */
39  public class JndiManager extends AbstractManager {
40  
41      private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
42      private static final String PREFIX = "log4j2.enableJndi";
43      private static final String JAVA_SCHEME = "java";
44  
45      private static final boolean JNDI_CONTEXT_SELECTOR_ENABLED = isJndiEnabled("ContextSelector");
46      private static final boolean JNDI_JDBC_ENABLED = isJndiEnabled("Jdbc");
47      private static final boolean JNDI_JMS_ENABLED = isJndiEnabled("Jms");
48      private static final boolean JNDI_LOOKUP_ENABLED = isJndiEnabled("Lookup");
49  
50      private final InitialContext context;
51  
52      private static boolean isJndiEnabled(final String subKey) {
53          return PropertiesUtil.getProperties().getBooleanProperty(PREFIX + subKey, false);
54      }
55  
56      public static boolean isJndiEnabled() {
57          return isJndiContextSelectorEnabled() || isJndiJdbcEnabled() || isJndiJmsEnabled() || isJndiLookupEnabled();
58      }
59  
60      public static boolean isJndiContextSelectorEnabled() {
61          return JNDI_CONTEXT_SELECTOR_ENABLED;
62      }
63  
64      public static boolean isJndiJdbcEnabled() {
65          return JNDI_JDBC_ENABLED;
66      }
67  
68      public static boolean isJndiJmsEnabled() {
69          return JNDI_JMS_ENABLED;
70      }
71  
72      public static boolean isJndiLookupEnabled() {
73          return JNDI_LOOKUP_ENABLED;
74      }
75  
76      private JndiManager(final String name, final InitialContext context) {
77          super(null, name);
78          this.context = context;
79      }
80  
81      /**
82       * Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
83       *
84       * @return the default JndiManager
85       */
86      public static JndiManager getDefaultManager() {
87          return getManager(JndiManager.class.getName(), FACTORY, null);
88      }
89  
90      /**
91       * Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
92       *
93       * @param name the name of the JndiManager instance to create or use if available
94       * @return a default JndiManager
95       */
96      public static JndiManager getDefaultManager(final String name) {
97          return getManager(name, FACTORY, null);
98      }
99  
100     /**
101      * Gets a JndiManager with the provided configuration information.
102      *
103      * @param initialContextFactoryName Fully qualified class name of an implementation of
104      *                                  {@link javax.naming.spi.InitialContextFactory}.
105      * @param providerURL               The provider URL to use for the JNDI connection (specific to the above factory).
106      * @param urlPkgPrefixes            A colon-separated list of package prefixes for the class name of the factory
107      *                                  class that will create a URL context factory
108      * @param securityPrincipal         The name of the identity of the Principal.
109      * @param securityCredentials       The security credentials of the Principal.
110      * @param additionalProperties      Any additional JNDI environment properties to set or {@code null} for none.
111      * @return the JndiManager for the provided parameters.
112      */
113     public static JndiManager getJndiManager(final String initialContextFactoryName,
114             final String providerURL,
115             final String urlPkgPrefixes,
116             final String securityPrincipal,
117             final String securityCredentials,
118             final Properties additionalProperties) {
119         final Properties properties = createProperties(initialContextFactoryName, providerURL, urlPkgPrefixes,
120                 securityPrincipal, securityCredentials, additionalProperties);
121         return getManager(createManagerName(), FACTORY, properties);
122     }
123 
124     /**
125      * Gets a JndiManager with the provided configuration information.
126      *
127      * @param properties JNDI properties, usually created by calling {@link #createProperties(String, String, String, String, String, Properties)}.
128      * @return the JndiManager for the provided parameters.
129      * @see #createProperties(String, String, String, String, String, Properties)
130      * @since 2.9
131      */
132     public static JndiManager getJndiManager(final Properties properties) {
133         return getManager(createManagerName(), FACTORY, properties);
134     }
135 
136     private static String createManagerName() {
137         return JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
138     }
139 
140     /**
141      * Creates JNDI Properties with the provided configuration information.
142      *
143      * @param initialContextFactoryName
144      *            Fully qualified class name of an implementation of {@link javax.naming.spi.InitialContextFactory}.
145      * @param providerURL
146      *            The provider URL to use for the JNDI connection (specific to the above factory).
147      * @param urlPkgPrefixes
148      *            A colon-separated list of package prefixes for the class name of the factory class that will create a
149      *            URL context factory
150      * @param securityPrincipal
151      *            The name of the identity of the Principal.
152      * @param securityCredentials
153      *            The security credentials of the Principal.
154      * @param additionalProperties
155      *            Any additional JNDI environment properties to set or {@code null} for none.
156      * @return the Properties for the provided parameters.
157      * @since 2.9
158      */
159     public static Properties createProperties(final String initialContextFactoryName, final String providerURL,
160             final String urlPkgPrefixes, final String securityPrincipal, final String securityCredentials,
161             final Properties additionalProperties) {
162         if (initialContextFactoryName == null) {
163             return null;
164         }
165         final Properties properties = new Properties();
166         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
167         if (providerURL != null) {
168             properties.setProperty(Context.PROVIDER_URL, providerURL);
169         } else {
170             LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated "
171                     + "provider URL. This is likely to cause problems.", initialContextFactoryName);
172         }
173         if (urlPkgPrefixes != null) {
174             properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
175         }
176         if (securityPrincipal != null) {
177             properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
178             if (securityCredentials != null) {
179                 properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
180             } else {
181                 LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
182                         securityPrincipal);
183             }
184         }
185         if (additionalProperties != null) {
186             properties.putAll(additionalProperties);
187         }
188         return properties;
189     }
190 
191     @Override
192     protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
193         return JndiCloser.closeSilently(this.context);
194     }
195 
196     /**
197      * Looks up a named object through this JNDI context.
198      *
199      * @param name name of the object to look up.
200      * @param <T>  the type of the object.
201      * @return the named object if it could be located.
202      * @throws  NamingException if a naming exception is encountered
203      */
204     @SuppressWarnings("unchecked")
205     public <T> T lookup(final String name) throws NamingException {
206         if (context == null) {
207             return null;
208         }
209         try {
210             URI uri = new URI(name);
211             if (uri.getScheme() == null || uri.getScheme().equals(JAVA_SCHEME)) {
212                 return (T) this.context.lookup(name);
213             }
214             LOGGER.warn("Unsupported JNDI URI - {}", name);
215         } catch (URISyntaxException ex) {
216             LOGGER.warn("Invalid JNDI URI - {}", name);
217         }
218         return null;
219     }
220 
221     private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
222 
223         @Override
224         public JndiManager createManager(final String name, final Properties data) {
225             if (!isJndiEnabled()) {
226                 throw new IllegalStateException(String.format("JNDI must be enabled by setting one of the %s* properties to true", PREFIX));
227             }
228             try {
229                 return new JndiManager(name, new InitialContext(data));
230             } catch (final NamingException e) {
231                 LOGGER.error("Error creating JNDI InitialContext for '{}'.", name, e);
232                 return null;
233             }
234         }
235 
236     }
237 
238     @Override
239     public String toString() {
240         return "JndiManager [context=" + context + ", count=" + count + "]";
241     }
242 
243 }