public static ClassLoader findClassLoader() { return LoaderUtil.getThreadContextClassLoader(); }
public TestClassLoader() { super(LoaderUtil.getThreadContextClassLoader()); }
/** * Gets the {@link LoggerContext} associated with the given caller class. * * @param callerClass the caller class * @return the LoggerContext for the calling class */ protected LoggerContext getContext(final Class<?> callerClass) { ClassLoader cl = null; if (callerClass != null) { cl = callerClass.getClassLoader(); } if (cl == null) { cl = LoaderUtil.getThreadContextClassLoader(); } return LogManager.getContext(cl, false); }
public static ClassLoader[] getClassLoaders() { List<ClassLoader> classLoaders = new ArrayList<>(); ClassLoader tcl = getThreadContextClassLoader(); classLoaders.add(tcl); // Some implementations may use null to represent the bootstrap class loader. ClassLoader current = LoaderUtil.class.getClassLoader(); if (current != null && current != tcl) { classLoaders.add(current); ClassLoader parent = current.getParent(); while (parent != null && !classLoaders.contains(parent)) { classLoaders.add(parent); } } ClassLoader parent = tcl == null ? null : tcl.getParent(); while (parent != null && !classLoaders.contains(parent)) { classLoaders.add(parent); parent = parent.getParent(); } ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); if (!classLoaders.contains(systemClassLoader)) { classLoaders.add(systemClassLoader); } return classLoaders.toArray(new ClassLoader[classLoaders.size()]); }
/** * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property is * specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
/** * This should replicate the classpath search for configuration file that Log4J 2 performs. * Returns the configuration location as URI or null if none is found. * * @return configuration location or null if not found */ public static URL findConfigInClasspath() { final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); for (final String suffix : SUFFIXES) { String resource = PREFIX + suffix; URL url = Loader.getResource(resource, loader); if (url != null) { // found it return url; } } return null; } }
static Collection<UrlResource> findUrlResources(final String resource) { // @formatter:off final ClassLoader[] candidates = { getThreadContextClassLoader(), LoaderUtil.class.getClassLoader(), GET_CLASS_LOADER_DISABLED ? null : ClassLoader.getSystemClassLoader()}; // @formatter:on final Collection<UrlResource> resources = new LinkedHashSet<>(); for (final ClassLoader cl : candidates) { if (cl != null) { try { final Enumeration<URL> resourceEnum = cl.getResources(resource); while (resourceEnum.hasMoreElements()) { resources.add(new UrlResource(cl, resourceEnum.nextElement())); } } catch (final IOException e) { LowLevelLogUtil.logException(e); } } } return resources; }
private ClassLoader getClassLoader() { try { // if container is Servlet 3.0, use its getClassLoader method // this may look odd, but the call below will throw NoSuchMethodError if user is on Servlet 2.5 // we compile against 3.0 to support Log4jServletContainerInitializer, but we don't require 3.0 return this.servletContext.getClassLoader(); } catch (final Throwable ignore) { // LOG4J2-248: use TCCL if possible return LoaderUtil.getThreadContextClassLoader(); } }
/** * Returns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is * available. * * @return the TCCL. * @see org.apache.logging.log4j.util.LoaderUtil#getThreadContextClassLoader() */ public static ClassLoader getThreadContextClassLoader() { return LoaderUtil.getThreadContextClassLoader(); }
public static ClassLoader findClassLoader() { return LoaderUtil.getThreadContextClassLoader(); }
/** * Gets the {@link LoggerContext} associated with the given caller class. * * @param callerClass the caller class * @return the LoggerContext for the calling class */ protected LoggerContext getContext(final Class<?> callerClass) { ClassLoader cl = null; if (callerClass != null) { cl = callerClass.getClassLoader(); } if (cl == null) { cl = LoaderUtil.getThreadContextClassLoader(); } return LogManager.getContext(cl, false); }
/** * This should replicate the classpath search for configuration file that * Log4J 2 performs. Returns the configuration location as URI or null if * none is found. * * @return configuration location or null if not found */ public static URL findConfigInClasspath() { final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); for (final String suffix : SUFFIXES) { String resource = PREFIX + suffix; URL url = Loader.getResource(resource, loader); if (url != null) { // found it return url; } } return null; } }
/** * Loads a class by name. This method respects the {@link # IGNORE_TCCL_PROPERTY} Log4j property. If this property * is specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
/** * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property is * specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
static Collection<UrlResource> findUrlResources(final String resource) { final ClassLoader[] candidates = {getThreadContextClassLoader(), LoaderUtil.class.getClassLoader(), GET_CLASS_LOADER_DISABLED ? null : ClassLoader.getSystemClassLoader()}; final Collection<UrlResource> resources = new LinkedHashSet<>(); for (final ClassLoader cl : candidates) { if (cl != null) { try { final Enumeration<URL> resourceEnum = cl.getResources(resource); while (resourceEnum.hasMoreElements()) { resources.add(new UrlResource(cl, resourceEnum.nextElement())); } } catch (final IOException e) { LowLevelLogUtil.logException(e); } } } return resources; }
final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); final String path = ConfigurationFactory.extractClassLoaderUriPath(configLocation); final ConfigurationSource source = fromResource(path, loader);
private Configuration getConfiguration(final LoggerContext loggerContext, final boolean isTest, final String name) { final boolean named = Strings.isNotEmpty(name); final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); for (final ConfigurationFactory factory : getFactories()) { String configName; final String prefix = isTest ? TEST_PREFIX : DEFAULT_PREFIX; final String [] types = factory.getSupportedTypes(); if (types == null) { continue; } for (final String suffix : types) { if (suffix.equals(ALL_TYPES)) { continue; } configName = named ? prefix + name + suffix : prefix + suffix; final ConfigurationSource source = ConfigurationSource.fromResource(configName, loader); if (source != null) { if (!factory.isActive()) { LOGGER.warn("Found configuration file {} for inactive ConfigurationFactory {}", configName, factory.getClass().getName()); } return factory.getConfiguration(loggerContext, source); } } } return null; }
private Configuration getConfiguration(final LoggerContext loggerContext, final String configLocationStr) { ConfigurationSource source = null; try { source = ConfigurationSource.fromUri(NetUtils.toURI(configLocationStr)); } catch (final Exception ex) { // Ignore the error and try as a String. LOGGER.catching(Level.DEBUG, ex); } if (source == null) { final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); source = getInputFromString(configLocationStr, loader); } if (source != null) { for (final ConfigurationFactory factory : getFactories()) { final String[] types = factory.getSupportedTypes(); if (types != null) { for (final String type : types) { if (type.equals(ALL_TYPES) || configLocationStr.endsWith(type)) { final Configuration config = factory.getConfiguration(loggerContext, source); if (config != null) { return config; } } } } } } return null; }