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.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   * JNDI {@link javax.naming.Context} manager.
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       * @param name the name of the JndiManager instance to create or use if available
59       * @return a default JndiManager
60       */
61      public static JndiManager getDefaultManager(final String name) {
62          return getManager(name, FACTORY, null);
63      }
64  
65      /**
66       * Gets a JndiManager with the provided configuration information.
67       *
68       * @param initialContextFactoryName Fully qualified class name of an implementation of
69       *                                  {@link javax.naming.spi.InitialContextFactory}.
70       * @param providerURL               The provider URL to use for the JNDI connection (specific to the above factory).
71       * @param urlPkgPrefixes            A colon-separated list of package prefixes for the class name of the factory
72       *                                  class that will create a URL context factory
73       * @param securityPrincipal         The name of the identity of the Principal.
74       * @param securityCredentials       The security credentials of the Principal.
75       * @param additionalProperties      Any additional JNDI environment properties to set or {@code null} for none.
76       * @return the JndiManager for the provided parameters.
77       */
78      public static JndiManager getJndiManager(final String initialContextFactoryName,
79                                               final String providerURL,
80                                               final String urlPkgPrefixes,
81                                               final String securityPrincipal,
82                                               final String securityCredentials,
83                                               final Properties additionalProperties) {
84          final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
85          if (initialContextFactoryName == null) {
86              return getManager(name, FACTORY, null);
87          }
88          final Properties properties = new Properties();
89          properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
90          if (providerURL != null) {
91              properties.setProperty(Context.PROVIDER_URL, providerURL);
92          } else {
93              LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " +
94                  "provider URL. This is likely to cause problems.", initialContextFactoryName);
95          }
96          if (urlPkgPrefixes != null) {
97              properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
98          }
99          if (securityPrincipal != null) {
100             properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
101             if (securityCredentials != null) {
102                 properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
103             } else {
104                 LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
105                     securityPrincipal);
106             }
107         }
108         if (additionalProperties != null) {
109             properties.putAll(additionalProperties);
110         }
111         return getManager(name, FACTORY, properties);
112     }
113 
114     @Override
115     protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
116         return JndiCloser.closeSilently(this.context);
117     }
118 
119     /**
120      * Looks up a named object through this JNDI context.
121      *
122      * @param name name of the object to look up.
123      * @param <T>  the type of the object.
124      * @return the named object if it could be located.
125      * @throws NamingException
126      */
127     @SuppressWarnings("unchecked")
128     public <T> T lookup(final String name) throws NamingException {
129         return (T) this.context.lookup(name);
130     }
131 
132     private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
133 
134         @Override
135         public JndiManager createManager(final String name, final Properties data) {
136             try {
137                 return new JndiManager(name, new InitialContext(data));
138             } catch (final NamingException e) {
139                 LOGGER.error("Error creating JNDI InitialContext.", e);
140                 return null;
141             }
142         }
143     }
144 }