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.lang.reflect.InvocationTargetException;
21 import java.net.URL;
22 import java.security.AccessController;
23 import java.security.PrivilegedAction;
24 import java.util.Collection;
25 import java.util.Enumeration;
26 import java.util.LinkedHashSet;
27
28 /**
29 * <em>Consider this class private.</em> Utility class for ClassLoaders.
30 * @see ClassLoader
31 * @see RuntimePermission
32 * @see Thread#getContextClassLoader()
33 * @see ClassLoader#getSystemClassLoader()
34 */
35 public final class LoaderUtil {
36 private LoaderUtil() {}
37
38 /**
39 * System property to set to ignore the thread context ClassLoader.
40 *
41 * @since 2.1
42 */
43 public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL";
44
45 private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager();
46
47 // this variable must be lazily loaded; otherwise, we get a nice circular class loading problem where LoaderUtil
48 // wants to use PropertiesUtil, but then PropertiesUtil wants to use LoaderUtil.
49 private static Boolean ignoreTCCL;
50
51 private static final boolean GET_CLASS_LOADER_DISABLED;
52
53 private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new ThreadContextClassLoaderGetter();
54
55 static {
56 if (SECURITY_MANAGER != null) {
57 boolean getClassLoaderDisabled;
58 try {
59 SECURITY_MANAGER.checkPermission(new RuntimePermission("getClassLoader"));
60 getClassLoaderDisabled = false;
61 } catch (final SecurityException ignored) {
62 getClassLoaderDisabled = true;
63 }
64 GET_CLASS_LOADER_DISABLED = getClassLoaderDisabled;
65 } else {
66 GET_CLASS_LOADER_DISABLED = false;
67 }
68 }
69
70 /**
71 * Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL is {@code null}. If the
72 * system ClassLoader is {@code null} as well, then the ClassLoader for this class is returned.
73 * If running with a {@link SecurityManager} that does not allow access to the Thread ClassLoader or system
74 * ClassLoader, then the ClassLoader for this class is returned.
75 *
76 * @return the current ThreadContextClassLoader.
77 */
78 public static ClassLoader getThreadContextClassLoader() {
79 if (GET_CLASS_LOADER_DISABLED) {
80 // we can at least get this class's ClassLoader regardless of security context
81 // however, if this is null, there's really no option left at this point
82 return LoaderUtil.class.getClassLoader();
83 }
84 return SECURITY_MANAGER == null
85 ? TCCL_GETTER.run()
86 : AccessController.doPrivileged(TCCL_GETTER);
87 }
88
89 private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> {
90 @Override
91 public ClassLoader run() {
92 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
93 if (cl != null) {
94 return cl;
95 }
96 final ClassLoader ccl = LoaderUtil.class.getClassLoader();
97 return ccl == null ? ClassLoader.getSystemClassLoader() : ccl;
98 }
99 }
100
101 /**
102 * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property
103 * is specified and set to anything besides {@code false}, then the default ClassLoader will be used.
104 *
105 * @param className The class name.
106 * @return the Class for the given name.
107 * @throws ClassNotFoundException if the specified class name could not be found
108 * @since 2.1
109 */
110 public static Class<?> loadClass(final String className) throws ClassNotFoundException {
111 if (isIgnoreTccl()) {
112 return Class.forName(className);
113 }
114 try {
115 return getThreadContextClassLoader().loadClass(className);
116 } catch (final Throwable ignored) {
117 return Class.forName(className);
118 }
119 }
120
121 /**
122 * Loads and instantiates a Class using the default constructor.
123 *
124 * @param className The class name.
125 * @return new instance of the class.
126 * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders
127 * @throws IllegalAccessException if the class can't be instantiated through a public constructor
128 * @throws InstantiationException if there was an exception whilst instantiating the class
129 * @throws NoSuchMethodException if there isn't a no-args constructor on the class
130 * @throws InvocationTargetException if there was an exception whilst constructing the class
131 * @since 2.1
132 */
133 public static Object newInstanceOf(final String className)
134 throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException,
135 InvocationTargetException {
136 final Class<?> clazz = loadClass(className);
137 try {
138 return clazz.getConstructor().newInstance();
139 } catch (final NoSuchMethodException ignored) {
140 // FIXME: looking at the code for Class.newInstance(), this seems to do the same thing as above
141 return clazz.newInstance();
142 }
143 }
144
145 /**
146 * Loads and instantiates a derived class using its default constructor.
147 *
148 * @param className The class name.
149 * @param clazz The class to cast it to.
150 * @param <T> The type of the class to check.
151 * @return new instance of the class cast to {@code T}
152 * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders
153 * @throws IllegalAccessException if the class can't be instantiated through a public constructor
154 * @throws InstantiationException if there was an exception whilst instantiating the class
155 * @throws NoSuchMethodException if there isn't a no-args constructor on the class
156 * @throws InvocationTargetException if there was an exception whilst constructing the class
157 * @throws ClassCastException if the constructed object isn't type compatible with {@code T}
158 * @since 2.1
159 */
160 public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz)
161 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException,
162 IllegalAccessException {
163 return clazz.cast(newInstanceOf(className));
164 }
165
166 private static boolean isIgnoreTccl() {
167 // we need to lazily initialize this, but concurrent access is not an issue
168 if (ignoreTCCL == null) {
169 final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null);
170 ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim());
171 }
172 return ignoreTCCL;
173 }
174
175 /**
176 * Finds classpath {@linkplain URL resources}.
177 *
178 * @param resource the name of the resource to find.
179 * @return a Collection of URLs matching the resource name. If no resources could be found, then this will be empty.
180 * @since 2.1
181 */
182 public static Collection<URL> findResources(final String resource) {
183 final Collection<UrlResource> urlResources = findUrlResources(resource);
184 final Collection<URL> resources = new LinkedHashSet<URL>(urlResources.size());
185 for (final UrlResource urlResource : urlResources) {
186 resources.add(urlResource.getUrl());
187 }
188 return resources;
189 }
190
191 static Collection<UrlResource> findUrlResources(final String resource) {
192 final ClassLoader[] candidates = {
193 getThreadContextClassLoader(),
194 LoaderUtil.class.getClassLoader(),
195 ClassLoader.getSystemClassLoader()
196 };
197 final Collection<UrlResource> resources = new LinkedHashSet<UrlResource>();
198 for (final ClassLoader cl : candidates) {
199 if (cl != null) {
200 try {
201 final Enumeration<URL> resourceEnum = cl.getResources(resource);
202 while (resourceEnum.hasMoreElements()) {
203 resources.add(new UrlResource(cl, resourceEnum.nextElement()));
204 }
205 } catch (final IOException e) {
206 e.printStackTrace();
207 }
208 }
209 }
210 return resources;
211 }
212
213 /**
214 * {@link URL} and {@link ClassLoader} pair.
215 */
216 static class UrlResource {
217 private final ClassLoader classLoader;
218 private final URL url;
219
220 public UrlResource(final ClassLoader classLoader, final URL url) {
221 this.classLoader = classLoader;
222 this.url = url;
223 }
224
225 public ClassLoader getClassLoader() {
226 return classLoader;
227 }
228
229 public URL getUrl() {
230 return url;
231 }
232
233 @Override
234 public boolean equals(final Object o) {
235 if (this == o) {
236 return true;
237 }
238 if (o == null || getClass() != o.getClass()) {
239 return false;
240 }
241
242 final UrlResource that = (UrlResource) o;
243
244 if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) {
245 return false;
246 }
247 if (url != null ? !url.equals(that.url) : that.url != null) {
248 return false;
249 }
250
251 return true;
252 }
253
254 @Override
255 public int hashCode() {
256 int result = classLoader != null ? classLoader.hashCode() : 0;
257 result = 31 * result + (url != null ? url.hashCode() : 0);
258 return result;
259 }
260 }
261 }