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
68 public static Set<URL> getResources(String resource, ClassLoader classLoader) throws IOException {
69
70 Set<URL> urlSet = new HashSet<URL>();
71 Enumeration<URL> urlEnum = classLoader.getResources(resource);
72 while (urlEnum.hasMoreElements()) {
73 URL url = urlEnum.nextElement();
74 urlSet.add(url);
75 }
76 return urlSet;
77 }
78
79
80
81
82
83
84
85 public static URL getResource(String resource, ClassLoader classLoader) {
86 try {
87 return classLoader.getResource(resource);
88 } catch (Throwable t) {
89 return null;
90 }
91 }
92
93
94
95
96
97
98
99
100 public static URL getResourceBySelfClassLoader(String resource) {
101 return getResource(resource, getClassLoaderOfClass(Loader.class));
102 }
103
104
105
106
107
108
109
110
111
112 public static ClassLoader getTCL() {
113 return Thread.currentThread().getContextClassLoader();
114 }
115
116 public static Class<?> loadClass(String clazz, Context context) throws ClassNotFoundException {
117 ClassLoader cl = getClassLoaderOfObject(context);
118 return cl.loadClass(clazz);
119 }
120
121
122
123
124
125
126
127
128 public static ClassLoader getClassLoaderOfObject(Object o) {
129 if (o == null) {
130 throw new NullPointerException("Argument cannot be null");
131 }
132 return getClassLoaderOfClass(o.getClass());
133 }
134
135
136
137
138
139
140
141 public static ClassLoader getClassLoaderAsPrivileged(final Class<?> clazz) {
142 if (!HAS_GET_CLASS_LOADER_PERMISSION)
143 return null;
144 else
145 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
146 public ClassLoader run() {
147 return clazz.getClassLoader();
148 }
149 });
150 }
151
152
153
154
155
156
157
158
159 public static ClassLoader getClassLoaderOfClass(final Class<?> clazz) {
160 ClassLoader cl = clazz.getClassLoader();
161 if (cl == null) {
162 return ClassLoader.getSystemClassLoader();
163 } else {
164 return cl;
165 }
166 }
167
168
169
170
171
172
173 public static Class<?> loadClass(String clazz) throws ClassNotFoundException {
174
175
176 if (ignoreTCL) {
177 return Class.forName(clazz);
178 } else {
179 try {
180 return getTCL().loadClass(clazz);
181 } catch (Throwable e) {
182
183
184
185 return Class.forName(clazz);
186 }
187 }
188 }
189 }