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