1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery;
18
19 import java.net.URL;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22
23 import org.apache.commons.discovery.log.DiscoveryLogFactory;
24 import org.apache.commons.logging.Log;
25
26
27 /***
28 * 'Resource' located by discovery.
29 * Naming of methods becomes a real pain ('getClass()')
30 * so I've patterned this after ClassLoader...
31 *
32 * I think it works well as it will give users a point-of-reference.
33 *
34 * @author Richard A. Sitze
35 */
36 public class ResourceClass extends Resource
37 {
38 private static Log log = DiscoveryLogFactory.newLog(ResourceClass.class);
39 public static void setLog(Log _log) {
40 log = _log;
41 }
42 protected Class resourceClass;
43
44 public ResourceClass(Class resourceClass, URL resource) {
45 super(resourceClass.getName(), resource, resourceClass.getClassLoader());
46 this.resourceClass = resourceClass;
47 }
48
49 public ResourceClass(String resourceName, URL resource, ClassLoader loader) {
50 super(resourceName, resource, loader);
51 }
52
53 /***
54 * Get the value of resourceClass.
55 * Loading the class does NOT guarentee that the class can be
56 * instantiated. Go figure.
57 * The class can be instantiated when the class is linked/resolved,
58 * and all dependencies are resolved.
59 * Various JDKs do this at different times, so beware:
60 * java.lang.NoClassDefFoundError when
61 * calling Class.getDeclaredMethod() (JDK14),
62 * java.lang.reflect.InvocationTargetException
63 * (wrapping java.lang.NoClassDefFoundError) when calling
64 * java.lang.newInstance (JDK13),
65 * and who knows what else..
66 *
67 * @return value of resourceClass.
68 */
69 public Class loadClass() {
70 if (resourceClass == null && getClassLoader() != null) {
71 if (log.isDebugEnabled())
72 log.debug("loadClass: Loading class '" + getName() + "' with " + getClassLoader());
73
74 resourceClass = (Class)AccessController.doPrivileged(
75 new PrivilegedAction() {
76 public Object run() {
77 try {
78 return getClassLoader().loadClass(getName());
79 } catch (ClassNotFoundException e) {
80 return null;
81 }
82 }
83 });
84 }
85 return resourceClass;
86 }
87
88 public String toString() {
89 return "ResourceClass[" + getName() + ", " + getResource() + ", " + getClassLoader() + "]";
90 }
91 }