View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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 org.apache.juddi.util;
17  
18  import java.io.InputStream;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.net.URL;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /***
27   * The idea for most of this code was taken from the Apache
28   * (Jakarta) Log4j project: http://jakarta.apache.org/log4j
29   *
30   *  - Steve
31   *
32   * @author Ceki Gülcü
33   * @author anou_mana;
34  */
35  public class Loader
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(Loader.class);
39    /***
40     * Added for unittesting with maven
41     * 
42     * @param resource
43     * @return
44     */
45    public InputStream getResourceAsStreamFromClass(String resource) {
46        InputStream stream = getClass().getClassLoader().getResourceAsStream(resource);
47        return stream;
48    }
49    /***
50     * @param resource
51     * @return InputStream to the named resource
52     */
53    public static InputStream getResourceAsStream(String resource)
54    {
55      ClassLoader classLoader = null;
56      InputStream stream = null;
57  
58      // Get the Thread Context Class Loader which is
59      // only available in JDK 1.2 and later.
60      try
61      {
62        classLoader = getContextClassLoader();
63        if (classLoader != null)
64        {
65          log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+".");
66          stream = classLoader.getResourceAsStream(resource);
67          if(stream != null) {
68            return stream;
69          }
70        }
71  
72        // We could not find resource. Ler us now try
73        // with the class loader that loaded this class.
74        classLoader = Loader.class.getClassLoader();
75        if (classLoader != null)
76        {
77          log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader.");
78          stream = classLoader.getResourceAsStream(resource);
79          if(stream != null) {
80            return stream;
81          }
82        }
83      }
84      catch(Throwable th) {
85        log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th);
86      }
87  
88      // Last ditch attempt: get the resource from the class path. It
89      // may be the case that class was loaded by the Extentsion class
90      // loader which the parent of the system class loader.
91      log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource().");
92  
93      return ClassLoader.getSystemResourceAsStream(resource);
94    }
95  
96    /***
97     * @param resource name
98     * @return URL to the named resource
99     */
100   public static URL getResource(String resource)
101   {
102     ClassLoader classLoader = null;
103     URL url = null;
104 
105     // Get the Thread Context Class Loader which is
106     // only available in JDK 1.2 and later.
107     try
108     {
109       classLoader = getContextClassLoader();
110       if (classLoader != null)
111       {
112         log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+".");
113         url = classLoader.getResource(resource);
114         if(url != null) {
115           return url;
116         }
117       }
118 
119       // We could not find resource. Ler us now try
120       // with the class loader that loaded this class.
121       classLoader = Loader.class.getClassLoader();
122       if (classLoader != null)
123       {
124         log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader.");
125         url = classLoader.getResource(resource);
126         if(url != null) {
127           return url;
128         }
129       }
130     }
131     catch(Throwable th) {
132       log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th);
133     }
134 
135     // Last ditch attempt: get the resource from the class path. It
136     // may be the case that class was loaded by the Extentsion class
137     // loader which the parent of the system class loader.
138     log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource().");
139 
140     return ClassLoader.getSystemResource(resource);
141   }
142 
143   /***
144    * Get the Thread Context Class Loader which is only available
145    * in JDK 1.2 and later.
146    * @return null if running under a JDK that's earlier than 1.2
147    **/
148   private static ClassLoader getContextClassLoader()
149     throws IllegalAccessException, InvocationTargetException
150   {
151     Method method = null;
152     try {
153       method = Thread.class.getMethod("getContextClassLoader",(Class[])null);
154     }
155     catch (NoSuchMethodException e) {
156       return null; // Using JDK 1.1 or earlier
157     }
158 
159     return (ClassLoader)method.invoke(Thread.currentThread(),(Object[])null);
160   }
161 
162   /***
163    * 
164    * @param name
165    * @return The class object for the name given
166    * @throws ClassNotFoundException
167    * @throws NoClassDefFoundError
168    */
169   public static Class getClassForName(String name)
170     throws ClassNotFoundException, NoClassDefFoundError
171   {
172     Class clazz = null;
173 
174     try
175     {
176       log.info("Using the Context ClassLoader");
177       ClassLoader ccl = Thread.currentThread().getContextClassLoader();
178       clazz = Class.forName(name, true, ccl);
179     }
180     catch (Exception e)
181     {
182       log.warn("Failed to load the class " + name + " with context class loader " + e);
183     }
184 
185     if (null == clazz)
186     {
187       ClassLoader scl = ClassLoader.getSystemClassLoader();
188       try
189       {
190         log.info("Using the System ClassLoader");
191         clazz = Class.forName(name, true, scl);
192       }
193       catch (Exception e)
194       {
195         log.warn("Failed to load the class " + name + " with system class loader " + e);
196       }
197     }
198 
199     return clazz;
200   }
201 }