1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.io.IOException;
17 import java.net.URL;
18 import java.security.AccessControlException;
19 import java.security.AccessController;
20 import java.security.PrivilegedAction;
21 import java.util.ArrayList;
22 import java.util.Enumeration;
23 import java.util.List;
24
25 import ch.qos.logback.core.Context;
26
27
28
29
30
31
32 public class Loader {
33 static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
34
35 private static boolean ignoreTCL = false;
36 public static final String IGNORE_TCL_PROPERTY_NAME = "logback.ignoreTCL";
37 private static boolean HAS_GET_CLASS_LOADER_PERMISSION = false;
38
39 static {
40 String ignoreTCLProp = OptionHelper.getSystemProperty(
41 IGNORE_TCL_PROPERTY_NAME, null);
42
43 if (ignoreTCLProp != null) {
44 ignoreTCL = OptionHelper.toBoolean(ignoreTCLProp, true);
45 }
46
47 HAS_GET_CLASS_LOADER_PERMISSION =
48 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
49 public Boolean run() {
50 try {
51 AccessController.checkPermission(
52 new RuntimePermission("getClassLoader"));
53 return true;
54 } catch (AccessControlException e) {
55 return false;
56 }
57 }
58 });
59 }
60
61
62
63
64
65
66
67
68
69
70 public static List<URL> getResourceOccurenceCount(String resource,
71 ClassLoader classLoader) throws IOException {
72 List<URL> urlList = new ArrayList<URL>();
73 Enumeration<URL> urlEnum = classLoader.getResources(resource);
74 while (urlEnum.hasMoreElements()) {
75 URL url = urlEnum.nextElement();
76 urlList.add(url);
77 }
78
79 return urlList;
80 }
81
82
83
84
85
86
87
88 public static URL getResource(String resource, ClassLoader classLoader) {
89 try {
90 return classLoader.getResource(resource);
91 } catch (Throwable t) {
92 return null;
93 }
94 }
95
96
97
98
99
100
101
102
103 public static URL getResourceBySelfClassLoader(String resource) {
104 return getResource(resource, getClassLoaderOfClass(Loader.class));
105 }
106
107
108
109
110
111
112
113
114
115
116 public static ClassLoader getTCL() {
117 return Thread.currentThread().getContextClassLoader();
118 }
119
120 public static Class loadClass(String clazz, Context context)
121 throws ClassNotFoundException {
122 ClassLoader cl = getClassLoaderOfObject(context);
123 return cl.loadClass(clazz);
124 }
125
126
127
128
129
130
131
132
133 public static ClassLoader getClassLoaderOfObject(Object o) {
134 if (o == null) {
135 throw new NullPointerException("Argument cannot be null");
136 }
137 return getClassLoaderOfClass(o.getClass());
138 }
139
140
141
142
143
144
145 public static ClassLoader getClassLoaderAsPrivileged(final Class clazz) {
146 if (!HAS_GET_CLASS_LOADER_PERMISSION)
147 return null;
148 else
149 return AccessController.doPrivileged(
150 new PrivilegedAction<ClassLoader>() {
151 public ClassLoader run() {
152 return clazz.getClassLoader();
153 }
154 });
155 }
156
157
158
159
160
161
162
163
164 public static ClassLoader getClassLoaderOfClass(final Class clazz) {
165 ClassLoader cl = clazz.getClassLoader();
166 if (cl == null) {
167 return ClassLoader.getSystemClassLoader();
168 } else {
169 return cl;
170 }
171 }
172
173
174
175
176
177
178 public static Class loadClass(String clazz) throws ClassNotFoundException {
179
180
181 if (ignoreTCL) {
182 return Class.forName(clazz);
183 } else {
184 try {
185 return getTCL().loadClass(clazz);
186 } catch (Throwable e) {
187
188
189
190 return Class.forName(clazz);
191 }
192 }
193 }
194 }