1 package org.ocltf.utils;
2
3 import java.io.BufferedReader;
4 import java.io.InputStreamReader;
5 import java.net.URL;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10
11 /***
12 * Provides utilities for loading resources using ClassLoaders.
13 *
14 * @author Chad Brandon
15 */
16 public class FileResourceUtils {
17
18 private static final Log logger = LogFactory.getLog(FileResourceUtils.class);
19
20 /***
21 * Retrieves aresource from the class package
22 * @param resourceName - the name of the resource
23 * @return java.net.URL
24 */
25 public static URL getResource(String resourceName) {
26 String methodName = "getResource";
27 if (logger.isDebugEnabled()) {
28 logger.debug("performing " + methodName
29 + " with resourceName (" + resourceName + ")");
30 }
31 ExceptionUtils.checkEmpty(methodName, "resourceName", resourceName);
32 ClassLoader loader = Thread.currentThread().getContextClassLoader();
33
34 URL resource = loader.getResource(resourceName);
35 return resource;
36 }
37
38 /***
39 * Takes a package as an argument and returns the URL for the
40 * package.
41 * @param clazz - the Class to load as a resource
42 * @return java.net.URL
43 */
44 public static URL getPackageResource(Class clazz) {
45 String methodName = "getPackageResource";
46 ExceptionUtils.checkNull(methodName, "clazz", clazz);
47 return FileResourceUtils.getResource(
48 ClassUtils.getPackageName(clazz).replace('.', '/'));
49 }
50
51 /***
52 * Takes a Class as an argument and returns the URL for the
53 * class.
54 * @param clazz - the Class to load as a resource
55 * @return java.net.URL
56 */
57 public static URL getClassResource(Class clazz) {
58 String methodName = "getClassResource";
59 ExceptionUtils.checkNull(methodName, "clazz", clazz);
60 return getResource(
61 getClassNameAsResource(
62 clazz.getName()));
63 }
64
65 /***
66 * Takes a className as an argument and returns the URL for the
67 * class.
68 * @param className
69 * @return java.net.URL
70 */
71 public static URL getClassResource(String className) {
72 String methodName = "getClassResource";
73 ExceptionUtils.checkEmpty(methodName, "className", className);
74 return getResource(getClassNameAsResource(className));
75 }
76
77 /***
78 * Takes a <code>packageName<code> as an argument and returns the URL for the
79 * class.
80 * @param packageName
81 * @return java.net.URL
82 */
83 public static URL getPackageResource(String packageName) {/package-summary.html">> static URL getPackageResource(String packageName) {
84 String methodName = "getPackageResource";
85 ExceptionUtils.checkEmpty(methodName, "packageName", packageName);
86 return getResource(packageName/replace('/', '/'))/package-summary.html">g> getResource(packageName.replace('.', '/'));
87 }
88
89 /***
90 * Private helper method.
91 * @param className
92 * @return String
93 */
94 private static String getClassNameAsResource(String className) {
95 return className.replace('.', '/') + ".class";
96 }
97
98 /***
99 * Loads the file resource and returns the contents
100 * as a String.
101 * @param resourceName the name of the resource.
102 * @return String
103 */
104 public static String getResourceContentsAsString(String resourceName) {
105 String methodName = "getResourceContentsAsString";
106 if (logger.isDebugEnabled()) {
107 logger.debug("performing " + methodName
108 + " with resourceName (" + resourceName + ")");
109 }
110 StringBuffer contents = new StringBuffer();
111 try {
112 URL resource = getResource(resourceName);
113 if (resource != null) {
114 BufferedReader in =
115 new BufferedReader(
116 new InputStreamReader(resource.openStream()));
117 String line;
118 while ((line = in.readLine()) != null){
119 contents.append(line + "\n");
120 }
121 in.close();
122 }
123 } catch (Exception ex) {
124 String errMsg = "Error performing " + methodName;
125 logger.error(errMsg, ex);
126 throw new RuntimeException(errMsg, ex);
127 }
128 return contents.toString();
129 }
130
131 }