Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../img/srcFileCovDistChart0.png 88% of files have more coverage
50   243   27   5
26   109   0,54   10
10     2,7  
1    
 
  ResourceLoaderUtility       Line # 37 50 27 0% 0.0
 
No Tests
 
1    /* $Id: ResourceLoaderUtility.java,v 1.1 2008/07/26 07:55:42 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17   
18    package org.qedeq.base.utility;
19   
20    import java.io.File;
21    import java.io.IOException;
22    import java.io.InputStream;
23    import java.lang.reflect.Method;
24    import java.net.URL;
25   
26    import org.qedeq.base.io.IoUtility;
27    import org.qedeq.base.trace.Trace;
28   
29    /**
30    * Utility methods for accessing classes and resources using an appropriate class loader.
31    * Adapted from org.apache.myfaces.trinidad.util.ClassLoaderUtils.
32    *
33    * @version $Revision: 1.1 $
34    * @author Michael Meyling
35    *
36    */
 
37    public final class ResourceLoaderUtility {
38   
39    /** This class. */
40    private static final Class CLASS = ResourceLoaderUtility.class;
41   
42    /**
43    * Constructor, should never be called.
44    */
 
45  0 toggle private ResourceLoaderUtility() {
46    // don't call me
47    }
48   
49    /**
50    * Loads the class with the specified name. For Java 2 callers, the current thread's context
51    * class loader is preferred, falling back on the system class loader of the caller when the
52    * current thread's context is not set, or the caller is pre Java 2.
53    *
54    * @param name Name of class to load.
55    * @return The resulting <code>Class</code> object
56    * @exception ClassNotFoundException Class was not found.
57    */
 
58  0 toggle public static Class loadClass(final String name) throws ClassNotFoundException {
59  0 return loadClass(name, null);
60    }
61   
62    /**
63    * Locates the resource with the specified name. For Java 2 callers, the current thread's
64    * context class loader is preferred, falling back on the system class loader of the caller when
65    * the current thread's context is not set, or the caller is pre Java 2.
66    *
67    * @param name Resource name.
68    * @return Resulting <code>URL</code> object. Maybe <code>null</code>.
69    */
 
70  0 toggle public static URL getResourceUrl(final String name) {
71  0 return getResourceUrl(name, ResourceLoaderUtility.class.getClassLoader());
72    }
73   
74    /**
75    * Locates the stream resource with the specified name. For Java 2 callers, the current thread's
76    * context class loader is preferred, falling back on the system class loader of the caller when
77    * the current thread's context is not set, or the caller is pre Java 2.
78    *
79    * @param name the name of the resource
80    * @return the resulting <code>InputStream</code> object
81    */
 
82  0 toggle public static InputStream getResourceAsStream(final String name) {
83  0 return getResourceAsStream(name, null);
84    }
85   
86    /**
87    * Loads the class with the specified name. For Java 2 callers, the current thread's context
88    * class loader is preferred, falling back on the class loader of the caller when the current
89    * thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is null,
90    * then fall back on the system class loader.
91    *
92    * @param name the name of the class
93    * @param callerClassLoader the calling class loader context
94    * @return the resulting <code>Class</code> object
95    * @exception ClassNotFoundException if the class was not found
96    */
 
97  0 toggle public static Class loadClass(final String name, final ClassLoader callerClassLoader)
98    throws ClassNotFoundException {
99  0 Class clazz = null;
100   
101  0 try {
102  0 final ClassLoader loader = getContextClassLoader();
103   
104  0 if (loader != null) {
105  0 clazz = loader.loadClass(name);
106    }
107    } catch (ClassNotFoundException e) {
108    // treat as though loader not set
109    }
110   
111  0 if (clazz == null) {
112  0 if (callerClassLoader != null) {
113  0 clazz = callerClassLoader.loadClass(name);
114    } else {
115  0 clazz = Class.forName(name);
116    }
117    }
118   
119  0 return clazz;
120    }
121   
122    /**
123    * Locates the resource with the specified name. For Java 2 callers, the current thread's
124    * context class loader is preferred, falling back on the class loader of the caller when the
125    * current thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is
126    * null, then fall back on the system class loader.
127    *
128    * @param name the name of the resource
129    * @param callerClassLoader the calling class loader context
130    * @return the resulting <code>URL</code> object
131    */
 
132  0 toggle public static URL getResourceUrl(final String name, final ClassLoader callerClassLoader) {
133  0 checkResourceName(name);
134   
135  0 URL url = null;
136   
137  0 final ClassLoader loader = getContextClassLoader();
138   
139  0 if (loader != null) {
140  0 url = loader.getResource(name);
141    }
142   
143  0 if (url == null) {
144    // no success, now we try the given class loader
145  0 if (callerClassLoader != null) {
146  0 url = callerClassLoader.getResource(name);
147    } else {
148    // last try: get resource via classpath
149  0 url = ClassLoader.getSystemResource(name);
150    }
151    }
152  0 return url;
153    }
154   
155    /**
156    * Locates the resource stream with the specified name. For Java 2 callers, the current thread's
157    * context class loader is preferred, falling back on the class loader of the caller when the
158    * current thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is
159    * null, then fall back on the system class loader.
160    *
161    * @param name the name of the resource
162    * @param callerClassLoader the calling class loader context
163    * @return the resulting <code>InputStream</code> object
164    */
 
165  0 toggle public static InputStream getResourceAsStream(final String name,
166    final ClassLoader callerClassLoader) {
167  0 checkResourceName(name);
168   
169  0 InputStream stream = null;
170   
171  0 final ClassLoader loader = getContextClassLoader();
172   
173  0 if (loader != null) {
174  0 stream = loader.getResourceAsStream(name);
175    }
176  0 if (stream == null) {
177  0 if (callerClassLoader != null) {
178  0 stream = callerClassLoader.getResourceAsStream(name);
179    } else {
180  0 stream = ClassLoader.getSystemResourceAsStream(name);
181    }
182    }
183   
184  0 return stream;
185    }
186   
187    /**
188    * Dynamically accesses the current context class loader. Returns <code>null</code> if there is
189    * no per-thread context class loader. Also if the JRE is below 1.2 or something else went wrong
190    * the method returns <code>null</code>.
191    *
192    * @return ClassLoader.
193    */
 
194  0 toggle public static ClassLoader getContextClassLoader() {
195  0 try {
196  0 final Method method = Thread.class.getMethod("getContextClassLoader", null);
197  0 return (ClassLoader) method.invoke(Thread.currentThread(), null);
198    } catch (Exception e) {
199  0 return null;
200    }
201    }
202   
 
203  0 toggle private static void checkResourceName(final String name) {
204  0 if ((name != null) && name.startsWith("/")) {
205  0 Trace.info(CLASS, "ClassLoaderUtility", "checkResourceName",
206    "resource name not portable: " + name);
207   
208    }
209    }
210   
211    /**
212    * Get resource file. The resource is located within the file system if it exists already.
213    * If not it is loaded as resource and then saved as a file.
214    *
215    * @param startDirectory Start looking from this directory.
216    * @param resourceDirectoryName Within this directory
217    * (relative to <code>startDirectory</code>).
218    * @param resourceName Look for this resource file.
219    * @return Resource file.
220    */
 
221  0 toggle public static File getResourceFile(final File startDirectory,
222    final String resourceDirectoryName, final String resourceName) {
223  0 final File resourceDir = new File(startDirectory, resourceDirectoryName);
224  0 final File resource = new File(resourceDir, resourceName);
225  0 if (!resource.exists()) {
226  0 final URL url = getResourceUrl(resourceDirectoryName + "/" + resourceName);
227  0 try {
228  0 if (!resourceDir.exists()) {
229  0 if (!resourceDir.mkdirs()) {
230  0 Trace.info(ResourceLoaderUtility.class, "getResourceUrlAndMakeLocalCopy",
231    "creation failed: " + resourceDir);
232    }
233    }
234  0 IoUtility.saveFile(url, resource);
235    } catch (IOException e) {
236  0 Trace.fatal(ResourceLoaderUtility.class, "getResourceUrlAndMakeLocalCopy",
237    "resource can not be saved", e);
238    }
239    }
240  0 return resource;
241    }
242   
243    }