001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.util; 018 019 import java.io.IOException; 020 import java.net.URL; 021 import java.util.ArrayList; 022 import java.util.Collection; 023 import java.util.Enumeration; 024 import java.util.Properties; 025 026 import org.apache.logging.log4j.Logger; 027 import org.apache.logging.log4j.spi.Provider; 028 import org.apache.logging.log4j.status.StatusLogger; 029 030 /** 031 * <em>Consider this class private.</em> 032 */ 033 public final class ProviderUtil { 034 035 private static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties"; 036 private static final String API_VERSION = "Log4jAPIVersion"; 037 038 private static final String[] COMPATIBLE_API_VERSIONS = { 039 "2.0.0" 040 }; 041 042 private static final Logger LOGGER = StatusLogger.getLogger(); 043 044 private static final Collection<Provider> PROVIDERS = new ArrayList<Provider>(); 045 046 private ProviderUtil() { 047 } 048 049 static { 050 final ClassLoader cl = findClassLoader(); 051 Enumeration<URL> enumResources = null; 052 try { 053 enumResources = cl.getResources(PROVIDER_RESOURCE); 054 } catch (final IOException e) { 055 LOGGER.fatal("Unable to locate {}", PROVIDER_RESOURCE, e); 056 } 057 loadProviders(enumResources); 058 } 059 060 protected static void loadProviders(final Enumeration<URL> enumResources) { 061 if (enumResources != null) { 062 while (enumResources.hasMoreElements()) { 063 final URL url = enumResources.nextElement(); 064 try { 065 final Properties props = PropertiesUtil.loadClose(url.openStream(), url); 066 if (!validVersion(props.getProperty(API_VERSION))) { 067 continue; 068 } 069 PROVIDERS.add(new Provider(props, url)); 070 } catch (final IOException ioe) { 071 LOGGER.error("Unable to open {}", url, ioe); 072 } 073 } 074 } 075 } 076 077 public static Iterable<Provider> getProviders() { 078 return PROVIDERS; 079 } 080 081 public static boolean hasProviders() { 082 return !PROVIDERS.isEmpty(); 083 } 084 085 public static ClassLoader findClassLoader() { 086 ClassLoader cl; 087 if (System.getSecurityManager() == null) { 088 cl = Thread.currentThread().getContextClassLoader(); 089 } else { 090 cl = java.security.AccessController.doPrivileged( 091 new java.security.PrivilegedAction<ClassLoader>() { 092 @Override 093 public ClassLoader run() { 094 return Thread.currentThread().getContextClassLoader(); 095 } 096 } 097 ); 098 } 099 if (cl == null) { 100 cl = ProviderUtil.class.getClassLoader(); 101 } 102 103 return cl; 104 } 105 106 private static boolean validVersion(final String version) { 107 for (final String v : COMPATIBLE_API_VERSIONS) { 108 if (version.startsWith(v)) { 109 return true; 110 } 111 } 112 return false; 113 } 114 }