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.ArrayList;
22  import java.util.Enumeration;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.spi.Provider;
29  import org.apache.logging.log4j.status.StatusLogger;
30  
31  /**
32   *
33   */
34  public final class ProviderUtil {
35  
36      private static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties";
37      private static final String API_VERSION = "Log4jAPIVersion";
38  
39      private static final String[] COMPATIBLE_API_VERSIONS = {
40          "2.0.0"
41      };
42  
43      private static final Logger LOGGER = StatusLogger.getLogger();
44  
45      private static final List<Provider> PROVIDERS = new ArrayList<Provider>();
46  
47      private ProviderUtil() {
48      }
49  
50      static {
51          final ClassLoader cl = findClassLoader();
52          Enumeration<URL> enumResources = null;
53          try {
54              enumResources = cl.getResources(PROVIDER_RESOURCE);
55          } catch (final IOException e) {
56              LOGGER.fatal("Unable to locate " + PROVIDER_RESOURCE, e);
57          }
58  
59          if (enumResources != null) {
60              while (enumResources.hasMoreElements()) {
61                  final URL url = enumResources.nextElement();
62                  Properties props;
63                  try {
64                      props = PropertiesUtil.loadClose(url.openStream(), url);
65                      if (!validVersion(props.getProperty(API_VERSION))) {
66                          continue;
67                      }
68                      PROVIDERS.add(new Provider(props, url));
69                  } catch (final IOException ioe) {
70                      LOGGER.error("Unable to open " + url.toString(), ioe);
71                  }
72              }
73          }
74      }
75  
76      public static Iterator<Provider> getProviders() {
77          return PROVIDERS.iterator();
78      }
79  
80      public static boolean hasProviders() {
81          return PROVIDERS.size() > 0;
82      }
83  
84      public static ClassLoader findClassLoader() {
85          ClassLoader cl;
86          if (System.getSecurityManager() == null) {
87              cl = Thread.currentThread().getContextClassLoader();
88          } else {
89              cl = java.security.AccessController.doPrivileged(
90                  new java.security.PrivilegedAction<ClassLoader>() {
91                      @Override
92                      public ClassLoader run() {
93                          return Thread.currentThread().getContextClassLoader();
94                      }
95                  }
96              );
97          }
98          if (cl == null) {
99              cl = ProviderUtil.class.getClassLoader();
100         }
101 
102         return cl;
103     }
104 
105     private static boolean validVersion(final String version) {
106         for (final String v : COMPATIBLE_API_VERSIONS) {
107             if (version.startsWith(v)) {
108                 return true;
109             }
110         }
111         return false;
112     }
113 }