Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
Source Code for Apache Log4j API
Apache Log4j API
provides the interface that applications should code to and provides the adapter
components required for implementers to create a logging implementation.
Apache Log4j API is a required module to use Apache Log4j.
Bytecode (Java 8) for Apache Log4j API is provided in a separate JAR file like log4j-api-2.14.1.jar.
Source Code files for Apache Log4j API are provided in both binary packge like apache-log4j-2.14.1-bin.zip and source package like apache-log4j-2.14.1-src.zip. You can download them at Apache Log4j Website.
You can also browse Source Code files for Apache Log4j API 2.14.1 below.
✍: FYIcenter.com
⏎ org/apache/logging/log4j/util/LoaderUtil.java
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache license, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the license for the specific language governing permissions and * limitations under the license. */ package org.apache.logging.log4j.util; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collection; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Objects; /** * <em>Consider this class private.</em> Utility class for ClassLoaders. * * @see ClassLoader * @see RuntimePermission * @see Thread#getContextClassLoader() * @see ClassLoader#getSystemClassLoader() */ public final class LoaderUtil { /** * System property to set to ignore the thread context ClassLoader. * * @since 2.1 */ public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL"; private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager(); // this variable must be lazily loaded; otherwise, we get a nice circular class loading problem where LoaderUtil // wants to use PropertiesUtil, but then PropertiesUtil wants to use LoaderUtil. private static Boolean ignoreTCCL; private static final boolean GET_CLASS_LOADER_DISABLED; private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new ThreadContextClassLoaderGetter(); static { if (SECURITY_MANAGER != null) { boolean getClassLoaderDisabled; try { SECURITY_MANAGER.checkPermission(new RuntimePermission("getClassLoader")); getClassLoaderDisabled = false; } catch (final SecurityException ignored) { getClassLoaderDisabled = true; } GET_CLASS_LOADER_DISABLED = getClassLoaderDisabled; } else { GET_CLASS_LOADER_DISABLED = false; } } private LoaderUtil() { } /** * Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL is {@code null}. If the system * ClassLoader is {@code null} as well, then the ClassLoader for this class is returned. If running with a * {@link SecurityManager} that does not allow access to the Thread ClassLoader or system ClassLoader, then the * ClassLoader for this class is returned. * * @return the current ThreadContextClassLoader. */ public static ClassLoader getThreadContextClassLoader() { if (GET_CLASS_LOADER_DISABLED) { // we can at least get this class's ClassLoader regardless of security context // however, if this is null, there's really no option left at this point return LoaderUtil.class.getClassLoader(); } return SECURITY_MANAGER == null ? TCCL_GETTER.run() : AccessController.doPrivileged(TCCL_GETTER); } /** * */ private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> { @Override public ClassLoader run() { final ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl != null) { return cl; } final ClassLoader ccl = LoaderUtil.class.getClassLoader(); return ccl == null && !GET_CLASS_LOADER_DISABLED ? ClassLoader.getSystemClassLoader() : ccl; } } public static ClassLoader[] getClassLoaders() { final Collection<ClassLoader> classLoaders = new LinkedHashSet<>(); final ClassLoader tcl = getThreadContextClassLoader(); if (tcl != null) { classLoaders.add(tcl); } accumulateClassLoaders(LoaderUtil.class.getClassLoader(), classLoaders); accumulateClassLoaders(tcl == null ? null : tcl.getParent(), classLoaders); final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); if (systemClassLoader != null) { classLoaders.add(systemClassLoader); } return classLoaders.toArray(new ClassLoader[classLoaders.size()]); } /** * Adds the provided loader to the loaders collection, and traverses up the tree until either a null * value or a classloader which has already been added is encountered. */ private static void accumulateClassLoaders(ClassLoader loader, Collection<ClassLoader> loaders) { // Some implementations may use null to represent the bootstrap class loader. if (loader != null && loaders.add(loader)) { accumulateClassLoaders(loader.getParent(), loaders); } } /** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. * @since 2.7 */ public static boolean isClassAvailable(final String className) { try { final Class<?> clazz = loadClass(className); return clazz != null; } catch (final ClassNotFoundException | LinkageError e) { return false; } catch (final Throwable e) { LowLevelLogUtil.logException("Unknown error checking for existence of class: " + className, e); return false; } } /** * 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 { ClassLoader tccl = getThreadContextClassLoader(); if (tccl != null) { return tccl.loadClass(className); } } catch (final Throwable ignored) { } return Class.forName(className); } /** * Loads and instantiates a Class using the default constructor. * * @param clazz The class. * @return new instance of the class. * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.7 */ public static <T> T newInstanceOf(final Class<T> clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException { try { return clazz.getConstructor().newInstance(); } catch (final NoSuchMethodException ignored) { // FIXME: looking at the code for Class.newInstance(), this seems to do the same thing as above return clazz.newInstance(); } } /** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.1 */ @SuppressWarnings("unchecked") public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return newInstanceOf((Class<T>) loadClass(className)); } /** * Loads and instantiates a derived class using its default constructor. * * @param className The class name. * @param clazz The class to cast it to. * @param <T> The type of the class to check. * @return new instance of the class cast to {@code T} * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.1 */ public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { return clazz.cast(newInstanceOf(className)); } /** * Loads and instantiates a class given by a property name. * * @param propertyName The property name to look up a class name for. * @param clazz The class to cast it to. * @param <T> The type to cast it to. * @return new instance of the class given in the property or {@code null} if the property was unset. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.5 */ public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { final String className = PropertiesUtil.getProperties().getStringProperty(propertyName); if (className == null) { return null; } return newCheckedInstanceOf(className, clazz); } private static boolean isIgnoreTccl() { // we need to lazily initialize this, but concurrent access is not an issue if (ignoreTCCL == null) { final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null); ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim()); } return ignoreTCCL; } /** * Finds classpath {@linkplain URL resources}. * * @param resource the name of the resource to find. * @return a Collection of URLs matching the resource name. If no resources could be found, then this will be empty. * @since 2.1 */ public static Collection<URL> findResources(final String resource) { final Collection<UrlResource> urlResources = findUrlResources(resource); final Collection<URL> resources = new LinkedHashSet<>(urlResources.size()); for (final UrlResource urlResource : urlResources) { resources.add(urlResource.getUrl()); } return resources; } 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; } /** * {@link URL} and {@link ClassLoader} pair. */ static class UrlResource { private final ClassLoader classLoader; private final URL url; UrlResource(final ClassLoader classLoader, final URL url) { this.classLoader = classLoader; this.url = url; } public ClassLoader getClassLoader() { return classLoader; } public URL getUrl() { return url; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final UrlResource that = (UrlResource) o; if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) { return false; } if (url != null ? !url.equals(that.url) : that.url != null) { return false; } return true; } @Override public int hashCode() { return Objects.hashCode(classLoader) + Objects.hashCode(url); } } }
⏎ org/apache/logging/log4j/util/LoaderUtil.java
Or download all of them as a single archive file:
File name: log4j-api-2.14.1-sources.jar File size: 264773 bytes Release date: 2021-03-06 Download
⇒ Source Code for Apache Log4j Core Implementation
⇐ Downloading Apache Log4j Binary Package
2015-11-17, 23367👍, 0💬
Popular Posts:
JDK 11 jdk.hotspot.agent.jmod is the JMOD file for JDK 11 Hotspot Agent module. JDK 11 Hotspot Agent...
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms, it was develo...
What Is jtds-1.2.2.jar? jtds-1.2.2.jar is the JAR files of jTDS Java library 1.2.2, which is a JDBC ...
JDK 11 jdk.internal.opt.jmod is the JMOD file for JDK 11 Internal Opt module. JDK 11 Internal Opt mo...
JDK 11 jdk.jdi.jmod is the JMOD file for JDK 11 JDI (Java Debug Interface) tool. JDK 11 JDI tool com...