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