View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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  /***
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  }