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 */ 017package org.apache.logging.log4j.util; 018 019import java.io.IOException; 020import java.lang.reflect.InvocationTargetException; 021import java.net.URL; 022import java.security.AccessController; 023import java.security.PrivilegedAction; 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.Enumeration; 027import java.util.LinkedHashSet; 028import java.util.List; 029import java.util.Objects; 030 031/** 032 * <em>Consider this class private.</em> Utility class for ClassLoaders. 033 * 034 * @see ClassLoader 035 * @see RuntimePermission 036 * @see Thread#getContextClassLoader() 037 * @see ClassLoader#getSystemClassLoader() 038 */ 039public final class LoaderUtil { 040 041 /** 042 * System property to set to ignore the thread context ClassLoader. 043 * 044 * @since 2.1 045 */ 046 public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL"; 047 048 private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager(); 049 050 // this variable must be lazily loaded; otherwise, we get a nice circular class loading problem where LoaderUtil 051 // wants to use PropertiesUtil, but then PropertiesUtil wants to use LoaderUtil. 052 private static Boolean ignoreTCCL; 053 054 private static final boolean GET_CLASS_LOADER_DISABLED; 055 056 private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new ThreadContextClassLoaderGetter(); 057 058 static { 059 if (SECURITY_MANAGER != null) { 060 boolean getClassLoaderDisabled; 061 try { 062 SECURITY_MANAGER.checkPermission(new RuntimePermission("getClassLoader")); 063 getClassLoaderDisabled = false; 064 } catch (final SecurityException ignored) { 065 getClassLoaderDisabled = true; 066 } 067 GET_CLASS_LOADER_DISABLED = getClassLoaderDisabled; 068 } else { 069 GET_CLASS_LOADER_DISABLED = false; 070 } 071 } 072 073 private LoaderUtil() { 074 } 075 076 /** 077 * Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL is {@code null}. If the system 078 * ClassLoader is {@code null} as well, then the ClassLoader for this class is returned. If running with a 079 * {@link SecurityManager} that does not allow access to the Thread ClassLoader or system ClassLoader, then the 080 * ClassLoader for this class is returned. 081 * 082 * @return the current ThreadContextClassLoader. 083 */ 084 public static ClassLoader getThreadContextClassLoader() { 085 if (GET_CLASS_LOADER_DISABLED) { 086 // we can at least get this class's ClassLoader regardless of security context 087 // however, if this is null, there's really no option left at this point 088 return LoaderUtil.class.getClassLoader(); 089 } 090 return SECURITY_MANAGER == null ? TCCL_GETTER.run() : AccessController.doPrivileged(TCCL_GETTER); 091 } 092 093 /** 094 * 095 */ 096 private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> { 097 @Override 098 public ClassLoader run() { 099 final ClassLoader cl = Thread.currentThread().getContextClassLoader(); 100 if (cl != null) { 101 return cl; 102 } 103 final ClassLoader ccl = LoaderUtil.class.getClassLoader(); 104 return ccl == null && !GET_CLASS_LOADER_DISABLED ? ClassLoader.getSystemClassLoader() : ccl; 105 } 106 } 107 108 public static ClassLoader[] getClassLoaders() { 109 final List<ClassLoader> classLoaders = new ArrayList<>(); 110 final ClassLoader tcl = getThreadContextClassLoader(); 111 classLoaders.add(tcl); 112 // Some implementations may use null to represent the bootstrap class loader. 113 final ClassLoader current = LoaderUtil.class.getClassLoader(); 114 if (current != null && current != tcl) { 115 classLoaders.add(current); 116 final ClassLoader parent = current.getParent(); 117 while (parent != null && !classLoaders.contains(parent)) { 118 classLoaders.add(parent); 119 } 120 } 121 ClassLoader parent = tcl == null ? null : tcl.getParent(); 122 while (parent != null && !classLoaders.contains(parent)) { 123 classLoaders.add(parent); 124 parent = parent.getParent(); 125 } 126 final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); 127 if (!classLoaders.contains(systemClassLoader)) { 128 classLoaders.add(systemClassLoader); 129 } 130 return classLoaders.toArray(new ClassLoader[classLoaders.size()]); 131 } 132 133 /** 134 * Determines if a named Class can be loaded or not. 135 * 136 * @param className The class name. 137 * @return {@code true} if the class could be found or {@code false} otherwise. 138 * @since 2.7 139 */ 140 public static boolean isClassAvailable(final String className) { 141 try { 142 final Class<?> clazz = loadClass(className); 143 return clazz != null; 144 } catch (final ClassNotFoundException | LinkageError e) { 145 return false; 146 } catch (final Throwable e) { 147 LowLevelLogUtil.logException("Unknown error checking for existence of class: " + className, e); 148 return false; 149 } 150 } 151 152 /** 153 * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property is 154 * specified and set to anything besides {@code false}, then the default ClassLoader will be used. 155 * 156 * @param className The class name. 157 * @return the Class for the given name. 158 * @throws ClassNotFoundException if the specified class name could not be found 159 * @since 2.1 160 */ 161 public static Class<?> loadClass(final String className) throws ClassNotFoundException { 162 if (isIgnoreTccl()) { 163 return Class.forName(className); 164 } 165 try { 166 return getThreadContextClassLoader().loadClass(className); 167 } catch (final Throwable ignored) { 168 return Class.forName(className); 169 } 170 } 171 172 /** 173 * Loads and instantiates a Class using the default constructor. 174 * 175 * @param clazz The class. 176 * @return new instance of the class. 177 * @throws IllegalAccessException if the class can't be instantiated through a public constructor 178 * @throws InstantiationException if there was an exception whilst instantiating the class 179 * @throws InvocationTargetException if there was an exception whilst constructing the class 180 * @since 2.7 181 */ 182 public static <T> T newInstanceOf(final Class<T> clazz) 183 throws InstantiationException, IllegalAccessException, InvocationTargetException { 184 try { 185 return clazz.getConstructor().newInstance(); 186 } catch (final NoSuchMethodException ignored) { 187 // FIXME: looking at the code for Class.newInstance(), this seems to do the same thing as above 188 return clazz.newInstance(); 189 } 190 } 191 192 /** 193 * Loads and instantiates a Class using the default constructor. 194 * 195 * @param className The class name. 196 * @return new instance of the class. 197 * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders 198 * @throws IllegalAccessException if the class can't be instantiated through a public constructor 199 * @throws InstantiationException if there was an exception whilst instantiating the class 200 * @throws NoSuchMethodException if there isn't a no-args constructor on the class 201 * @throws InvocationTargetException if there was an exception whilst constructing the class 202 * @since 2.1 203 */ 204 @SuppressWarnings("unchecked") 205 public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, 206 InstantiationException, NoSuchMethodException, InvocationTargetException { 207 return newInstanceOf((Class<T>) loadClass(className)); 208 } 209 210 /** 211 * Loads and instantiates a derived class using its default constructor. 212 * 213 * @param className The class name. 214 * @param clazz The class to cast it to. 215 * @param <T> The type of the class to check. 216 * @return new instance of the class cast to {@code T} 217 * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders 218 * @throws IllegalAccessException if the class can't be instantiated through a public constructor 219 * @throws InstantiationException if there was an exception whilst instantiating the class 220 * @throws NoSuchMethodException if there isn't a no-args constructor on the class 221 * @throws InvocationTargetException if there was an exception whilst constructing the class 222 * @throws ClassCastException if the constructed object isn't type compatible with {@code T} 223 * @since 2.1 224 */ 225 public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) 226 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, 227 IllegalAccessException { 228 return clazz.cast(newInstanceOf(className)); 229 } 230 231 /** 232 * Loads and instantiates a class given by a property name. 233 * 234 * @param propertyName The property name to look up a class name for. 235 * @param clazz The class to cast it to. 236 * @param <T> The type to cast it to. 237 * @return new instance of the class given in the property or {@code null} if the property was unset. 238 * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders 239 * @throws IllegalAccessException if the class can't be instantiated through a public constructor 240 * @throws InstantiationException if there was an exception whilst instantiating the class 241 * @throws NoSuchMethodException if there isn't a no-args constructor on the class 242 * @throws InvocationTargetException if there was an exception whilst constructing the class 243 * @throws ClassCastException if the constructed object isn't type compatible with {@code T} 244 * @since 2.5 245 */ 246 public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) 247 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, 248 IllegalAccessException { 249 final String className = PropertiesUtil.getProperties().getStringProperty(propertyName); 250 if (className == null) { 251 return null; 252 } 253 return newCheckedInstanceOf(className, clazz); 254 } 255 256 private static boolean isIgnoreTccl() { 257 // we need to lazily initialize this, but concurrent access is not an issue 258 if (ignoreTCCL == null) { 259 final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null); 260 ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim()); 261 } 262 return ignoreTCCL; 263 } 264 265 /** 266 * Finds classpath {@linkplain URL resources}. 267 * 268 * @param resource the name of the resource to find. 269 * @return a Collection of URLs matching the resource name. If no resources could be found, then this will be empty. 270 * @since 2.1 271 */ 272 public static Collection<URL> findResources(final String resource) { 273 final Collection<UrlResource> urlResources = findUrlResources(resource); 274 final Collection<URL> resources = new LinkedHashSet<>(urlResources.size()); 275 for (final UrlResource urlResource : urlResources) { 276 resources.add(urlResource.getUrl()); 277 } 278 return resources; 279 } 280 281 static Collection<UrlResource> findUrlResources(final String resource) { 282 // @formatter:off 283 final ClassLoader[] candidates = { 284 getThreadContextClassLoader(), 285 LoaderUtil.class.getClassLoader(), 286 GET_CLASS_LOADER_DISABLED ? null : ClassLoader.getSystemClassLoader()}; 287 // @formatter:on 288 final Collection<UrlResource> resources = new LinkedHashSet<>(); 289 for (final ClassLoader cl : candidates) { 290 if (cl != null) { 291 try { 292 final Enumeration<URL> resourceEnum = cl.getResources(resource); 293 while (resourceEnum.hasMoreElements()) { 294 resources.add(new UrlResource(cl, resourceEnum.nextElement())); 295 } 296 } catch (final IOException e) { 297 LowLevelLogUtil.logException(e); 298 } 299 } 300 } 301 return resources; 302 } 303 304 /** 305 * {@link URL} and {@link ClassLoader} pair. 306 */ 307 static class UrlResource { 308 private final ClassLoader classLoader; 309 private final URL url; 310 311 UrlResource(final ClassLoader classLoader, final URL url) { 312 this.classLoader = classLoader; 313 this.url = url; 314 } 315 316 public ClassLoader getClassLoader() { 317 return classLoader; 318 } 319 320 public URL getUrl() { 321 return url; 322 } 323 324 @Override 325 public boolean equals(final Object o) { 326 if (this == o) { 327 return true; 328 } 329 if (o == null || getClass() != o.getClass()) { 330 return false; 331 } 332 333 final UrlResource that = (UrlResource) o; 334 335 if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) { 336 return false; 337 } 338 if (url != null ? !url.equals(that.url) : that.url != null) { 339 return false; 340 } 341 342 return true; 343 } 344 345 @Override 346 public int hashCode() { 347 return Objects.hashCode(classLoader) + Objects.hashCode(url); 348 } 349 } 350}