1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.util; |
20 | |
|
21 | |
import org.slf4j.Logger; |
22 | |
import org.slf4j.LoggerFactory; |
23 | |
|
24 | |
import java.io.InputStream; |
25 | |
import java.lang.reflect.Constructor; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 102 | public class ClassUtils { |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 1 | private static final Logger log = LoggerFactory.getLogger(ClassUtils.class); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 1 | private static final ClassLoaderAccessor THREAD_CL_ACCESSOR = new ExceptionIgnoringAccessor() { |
47 | |
@Override |
48 | |
protected ClassLoader doGetClassLoader() throws Throwable { |
49 | 94 | return Thread.currentThread().getContextClassLoader(); |
50 | |
} |
51 | |
}; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 1 | private static final ClassLoaderAccessor CLASS_CL_ACCESSOR = new ExceptionIgnoringAccessor() { |
57 | |
@Override |
58 | |
protected ClassLoader doGetClassLoader() throws Throwable { |
59 | 19 | return ClassUtils.class.getClassLoader(); |
60 | |
} |
61 | |
}; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 1 | private static final ClassLoaderAccessor SYSTEM_CL_ACCESSOR = new ExceptionIgnoringAccessor() { |
67 | |
@Override |
68 | |
protected ClassLoader doGetClassLoader() throws Throwable { |
69 | 19 | return ClassLoader.getSystemClassLoader(); |
70 | |
} |
71 | |
}; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public static InputStream getResourceAsStream(String name) { |
86 | |
|
87 | 7 | InputStream is = THREAD_CL_ACCESSOR.getResourceStream(name); |
88 | |
|
89 | 7 | if (is == null) { |
90 | 2 | if (log.isTraceEnabled()) { |
91 | 2 | log.trace("Resource [" + name + "] was not found via the thread context ClassLoader. Trying the " + |
92 | |
"current ClassLoader..."); |
93 | |
} |
94 | 2 | is = CLASS_CL_ACCESSOR.getResourceStream(name); |
95 | |
} |
96 | |
|
97 | 7 | if (is == null) { |
98 | 2 | if (log.isTraceEnabled()) { |
99 | 2 | log.trace("Resource [" + name + "] was not found via the current class loader. Trying the " + |
100 | |
"system/application ClassLoader..."); |
101 | |
} |
102 | 2 | is = SYSTEM_CL_ACCESSOR.getResourceStream(name); |
103 | |
} |
104 | |
|
105 | 7 | if (is == null && log.isTraceEnabled()) { |
106 | 2 | log.trace("Resource [" + name + "] was not found via the thread context, current, or " + |
107 | |
"system/application ClassLoaders. All heuristics have been exhausted. Returning null."); |
108 | |
} |
109 | |
|
110 | 7 | return is; |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static Class forName(String fqcn) throws UnknownClassException { |
126 | |
|
127 | 87 | Class clazz = THREAD_CL_ACCESSOR.loadClass(fqcn); |
128 | |
|
129 | 87 | if (clazz == null) { |
130 | 17 | if (log.isTraceEnabled()) { |
131 | 17 | log.trace("Unable to load class named [" + fqcn + |
132 | |
"] from the thread context ClassLoader. Trying the current ClassLoader..."); |
133 | |
} |
134 | 17 | clazz = CLASS_CL_ACCESSOR.loadClass(fqcn); |
135 | |
} |
136 | |
|
137 | 87 | if (clazz == null) { |
138 | 17 | if (log.isTraceEnabled()) { |
139 | 17 | log.trace("Unable to load class named [" + fqcn + "] from the current ClassLoader. " + |
140 | |
"Trying the system/application ClassLoader..."); |
141 | |
} |
142 | 17 | clazz = SYSTEM_CL_ACCESSOR.loadClass(fqcn); |
143 | |
} |
144 | |
|
145 | 87 | if (clazz == null) { |
146 | 17 | String msg = "Unable to load class named [" + fqcn + "] from the thread context, current, or " + |
147 | |
"system/application ClassLoaders. All heuristics have been exhausted. Class could not be found."; |
148 | 17 | throw new UnknownClassException(msg); |
149 | |
} |
150 | |
|
151 | 70 | return clazz; |
152 | |
} |
153 | |
|
154 | |
public static boolean isAvailable(String fullyQualifiedClassName) { |
155 | |
try { |
156 | 0 | forName(fullyQualifiedClassName); |
157 | 0 | return true; |
158 | 0 | } catch (UnknownClassException e) { |
159 | 0 | return false; |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
public static Object newInstance(String fqcn) { |
164 | 66 | return newInstance(forName(fqcn)); |
165 | |
} |
166 | |
|
167 | |
public static Object newInstance(String fqcn, Object... args) { |
168 | 0 | return newInstance(forName(fqcn), args); |
169 | |
} |
170 | |
|
171 | |
public static Object newInstance(Class clazz) { |
172 | 92 | if (clazz == null) { |
173 | 0 | String msg = "Class method parameter cannot be null."; |
174 | 0 | throw new IllegalArgumentException(msg); |
175 | |
} |
176 | |
try { |
177 | 92 | return clazz.newInstance(); |
178 | 0 | } catch (Exception e) { |
179 | 0 | throw new InstantiationException("Unable to instantiate class [" + clazz.getName() + "]", e); |
180 | |
} |
181 | |
} |
182 | |
|
183 | |
public static Object newInstance(Class clazz, Object... args) { |
184 | 33 | Class[] argTypes = new Class[args.length]; |
185 | 77 | for (int i = 0; i < args.length; i++) { |
186 | 44 | argTypes[i] = args[i].getClass(); |
187 | |
} |
188 | 33 | Constructor ctor = getConstructor(clazz, argTypes); |
189 | 33 | return instantiate(ctor, args); |
190 | |
} |
191 | |
|
192 | |
public static Constructor getConstructor(Class clazz, Class... argTypes) { |
193 | |
try { |
194 | 33 | return clazz.getConstructor(argTypes); |
195 | 0 | } catch (NoSuchMethodException e) { |
196 | 0 | throw new IllegalStateException(e); |
197 | |
} |
198 | |
|
199 | |
} |
200 | |
|
201 | |
public static Object instantiate(Constructor ctor, Object... args) { |
202 | |
try { |
203 | 33 | return ctor.newInstance(args); |
204 | 0 | } catch (Exception e) { |
205 | 0 | String msg = "Unable to instantiate Permission instance with constructor [" + ctor + "]"; |
206 | 0 | throw new InstantiationException(msg, e); |
207 | |
} |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
private static interface ClassLoaderAccessor { |
214 | |
Class loadClass(String fqcn); |
215 | |
InputStream getResourceStream(String name); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | 6 | private static abstract class ExceptionIgnoringAccessor implements ClassLoaderAccessor { |
222 | |
|
223 | |
public Class loadClass(String fqcn) { |
224 | 121 | Class clazz = null; |
225 | 121 | ClassLoader cl = getClassLoader(); |
226 | 121 | if (cl != null) { |
227 | |
try { |
228 | 121 | clazz = cl.loadClass(fqcn); |
229 | 51 | } catch (ClassNotFoundException e) { |
230 | 51 | if (log.isTraceEnabled()) { |
231 | 51 | log.trace("Unable to load clazz named [" + fqcn + "] from class loader [" + cl + "]"); |
232 | |
} |
233 | 70 | } |
234 | |
} |
235 | 121 | return clazz; |
236 | |
} |
237 | |
|
238 | |
public InputStream getResourceStream(String name) { |
239 | 11 | InputStream is = null; |
240 | 11 | ClassLoader cl = getClassLoader(); |
241 | 11 | if (cl != null) { |
242 | 11 | is = cl.getResourceAsStream(name); |
243 | |
} |
244 | 11 | return is; |
245 | |
} |
246 | |
|
247 | |
protected final ClassLoader getClassLoader() { |
248 | |
try { |
249 | 132 | return doGetClassLoader(); |
250 | 0 | } catch (Throwable t) { |
251 | 0 | if (log.isDebugEnabled()) { |
252 | 0 | log.debug("Unable to acquire ClassLoader.", t); |
253 | |
} |
254 | |
} |
255 | 0 | return null; |
256 | |
} |
257 | |
|
258 | |
protected abstract ClassLoader doGetClassLoader() throws Throwable; |
259 | |
} |
260 | |
} |