View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.germinus.easyconf;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.commons.configuration.ConfigurationUtils;
21  
22  import java.net.URL;
23  
24  /***
25   * Contains util methods to search in the classpath
26   * 
27   * @author jferrer
28   */
29  public class ClasspathUtil {
30      private static final Log log = LogFactory.getLog(ClasspathUtil.class);
31  
32      /***
33       * Return the Class object of the specified class name by searching the
34       * current classpath and the system classpath.
35       *
36       * @param name the name of the class
37       *
38       * @return the <code>Class</code> instance
39       */
40      public static Class locateClass(String name) throws ClassNotFoundException {
41          Class foundClass = null;
42          if (foundClass == null) {
43              ClassLoader loader = Thread.currentThread().getContextClassLoader();
44              try {
45                  foundClass = loader.loadClass(name);
46                  log.debug("Class loaded from the context classpath (" + name + ")");
47              } catch (ClassNotFoundException ignore) {
48              }
49          }
50  
51          if (foundClass == null) {
52              try {
53                  foundClass = ClassLoader.getSystemClassLoader().loadClass(name);
54                  log.debug("Class loaded from the system classpath (" + name + ")");
55              } catch (ClassNotFoundException ignore) {
56              }
57          }
58          if (foundClass == null) {
59              throw new ClassNotFoundException("Class " + name + " was not found " +
60                                               "in context classpath nor system classpath");
61          }
62          return foundClass;
63      }
64  
65      /***
66       * Return an array of Class objects for each of the class names specified. Each
67       * class will be searched for using the <tt>locateClass</tt> method.
68       * If any of the class names does not exist a <tt>ClassNotFoundException</tt>
69       * will be thrown
70       *
71       * @param classNames the names of the classes to load
72       *
73       * @return the <code>Class[]</code> array
74       */
75      public static Class[] locateClasses(String[] classNames) throws ClassNotFoundException {
76          Class[] classes = new Class[classNames.length];
77          for (int i = 0; i < classes.length; i++) {
78              classes[i] = locateClass(classNames[i]);
79          }
80          return classes;
81      }
82  
83      /***
84       * Return the location of the specified resource by searching the user home
85       * directory, the current classpath and the system classpath.
86       *
87       * @param base the base path of the resource
88       * @param name the name of the resource
89       *
90       * @return the location of the resource or <code>null</code> if it has not
91       * been found
92       */
93      public static URL locateResource(String  base, String name) {
94          return ConfigurationUtils.locate(base, name);
95      }
96  
97      /***
98       * Return the location of the specified resource by searching the user home
99       * directory, the current classpath and the system classpath.
100      *
101      * @param name the name of the resource
102      *
103      * @return the location of the resource or <code>null</code> if it has not
104      * been found
105      */
106     public static URL locateResource(String name) {
107         return ConfigurationUtils.locate(null, name);
108     }
109 
110 }