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  package org.apache.logging.log4j.util;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.Collection;
22  import java.util.Enumeration;
23  import java.util.HashSet;
24  import java.util.Properties;
25  import java.util.concurrent.locks.Lock;
26  import java.util.concurrent.locks.ReentrantLock;
27  
28  import org.apache.logging.log4j.Logger;
29  import org.apache.logging.log4j.spi.Provider;
30  import org.apache.logging.log4j.status.StatusLogger;
31  
32  /**
33   * <em>Consider this class private.</em> Utility class for Log4j {@link Provider}s. When integrating with an application
34   * container framework, any Log4j Providers not accessible through standard classpath scanning should
35   * {@link #loadProvider(java.net.URL, ClassLoader)} a classpath accordingly.
36   */
37  public final class ProviderUtil {
38  
39      /**
40       * Resource name for a Log4j 2 provider properties file.
41       */
42      protected static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties";
43  
44      /**
45       * Loaded providers.
46       */
47      protected static final Collection<Provider> PROVIDERS = new HashSet<>();
48  
49      /**
50       * Guards the ProviderUtil singleton instance from lazy initialization. This is primarily used for OSGi support.
51       *
52       * @since 2.1
53       */
54      protected static final Lock STARTUP_LOCK = new ReentrantLock();
55  
56      private static final String API_VERSION = "Log4jAPIVersion";
57      private static final String[] COMPATIBLE_API_VERSIONS = {"2.0.0", "2.1.0", "2.2.0", "2.3.0", "2.4.0", "2.5.0", "2.6.0"};
58      private static final Logger LOGGER = StatusLogger.getLogger();
59  
60      // STARTUP_LOCK guards INSTANCE for lazy initialization; this allows the OSGi Activator to pause the startup and
61      // wait for a Provider to be installed. See LOG4J2-373
62      private static volatile ProviderUtil instance;
63  
64      private ProviderUtil() {
65          for (final LoaderUtil.UrlResource resource : LoaderUtil.findUrlResources(PROVIDER_RESOURCE)) {
66              loadProvider(resource.getUrl(), resource.getClassLoader());
67          }
68      }
69  
70      /**
71       * Loads an individual Provider implementation. This method is really only useful for the OSGi bundle activator and
72       * this class itself.
73       *
74       * @param url the URL to the provider properties file
75       * @param cl the ClassLoader to load the provider classes with
76       */
77      protected static void loadProvider(final URL url, final ClassLoader cl) {
78          try {
79              final Properties props = PropertiesUtil.loadClose(url.openStream(), url);
80              if (validVersion(props.getProperty(API_VERSION))) {
81                  final Provider provider = new Provider(props, url, cl);
82                  PROVIDERS.add(provider);
83                  LOGGER.debug("Loaded Provider {}", provider);
84              }
85          } catch (final IOException e) {
86              LOGGER.error("Unable to open {}", url, e);
87          }
88      }
89  
90      /**
91       * @deprecated Use {@link #loadProvider(java.net.URL, ClassLoader)} instead. Will be removed in 3.0.
92       */
93      @Deprecated
94      protected static void loadProviders(final Enumeration<URL> urls, final ClassLoader cl) {
95          if (urls != null) {
96              while (urls.hasMoreElements()) {
97                  loadProvider(urls.nextElement(), cl);
98              }
99          }
100     }
101 
102     public static Iterable<Provider> getProviders() {
103         lazyInit();
104         return PROVIDERS;
105     }
106 
107     public static boolean hasProviders() {
108         lazyInit();
109         return !PROVIDERS.isEmpty();
110     }
111 
112     /**
113      * Lazily initializes the ProviderUtil singleton.
114      *
115      * @since 2.1
116      */
117     protected static void lazyInit() {
118         // noinspection DoubleCheckedLocking
119         if (instance == null) {
120             try {
121                 STARTUP_LOCK.lockInterruptibly();
122                 try {
123                     if (instance == null) {
124                         instance = new ProviderUtil();
125                     }
126                 } finally {
127                     STARTUP_LOCK.unlock();
128                 }
129             } catch (final InterruptedException e) {
130                 LOGGER.fatal("Interrupted before Log4j Providers could be loaded.", e);
131                 Thread.currentThread().interrupt();
132             }
133         }
134     }
135 
136     public static ClassLoader findClassLoader() {
137         return LoaderUtil.getThreadContextClassLoader();
138     }
139 
140     private static boolean validVersion(final String version) {
141         for (final String v : COMPATIBLE_API_VERSIONS) {
142             if (version.startsWith(v)) {
143                 return true;
144             }
145         }
146         return false;
147     }
148 }