01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: * 'Resource' located by discovery.
28: * Naming of methods becomes a real pain ('getClass()')
29: * so I've patterned this after ClassLoader...
30: *
31: * I think it works well as it will give users a point-of-reference.
32: *
33: * @author Richard A. Sitze
34: */
35: public class ResourceClass extends Resource {
36: private static Log log = DiscoveryLogFactory
37: .newLog(ResourceClass.class);
38:
39: public static void setLog(Log _log) {
40: log = _log;
41: }
42:
43: protected Class resourceClass;
44:
45: public ResourceClass(Class resourceClass, URL resource) {
46: super (resourceClass.getName(), resource, resourceClass
47: .getClassLoader());
48: this .resourceClass = resourceClass;
49: }
50:
51: public ResourceClass(String resourceName, URL resource,
52: ClassLoader loader) {
53: super (resourceName, resource, loader);
54: }
55:
56: /**
57: * Get the value of resourceClass.
58: * Loading the class does NOT guarentee that the class can be
59: * instantiated. Go figure.
60: * The class can be instantiated when the class is linked/resolved,
61: * and all dependencies are resolved.
62: * Various JDKs do this at different times, so beware:
63: * java.lang.NoClassDefFoundError when
64: * calling Class.getDeclaredMethod() (JDK14),
65: * java.lang.reflect.InvocationTargetException
66: * (wrapping java.lang.NoClassDefFoundError) when calling
67: * java.lang.newInstance (JDK13),
68: * and who knows what else..
69: *
70: * @return value of resourceClass.
71: */
72: public Class loadClass() {
73: if (resourceClass == null && getClassLoader() != null) {
74: if (log.isDebugEnabled())
75: log.debug("loadClass: Loading class '" + getName()
76: + "' with " + getClassLoader());
77:
78: resourceClass = (Class) AccessController
79: .doPrivileged(new PrivilegedAction() {
80: public Object run() {
81: try {
82: return getClassLoader().loadClass(
83: getName());
84: } catch (ClassNotFoundException e) {
85: return null;
86: }
87: }
88: });
89: }
90: return resourceClass;
91: }
92:
93: public String toString() {
94: return "ResourceClass[" + getName() + ", " + getResource()
95: + ", " + getClassLoader() + "]";
96: }
97: }
|