1 package org.ocltf.common;
2
3 import java.net.URL;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Enumeration;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.ocltf.utils.ExceptionUtils;
11
12 /***
13 * Finds and loads file resources from the current classpath.
14 *
15 * @author Chad Brandon
16 */
17 public class ResourceFinder {
18
19 private static Log logger = LogFactory.getLog(ResourceFinder.class);
20
21 private ResourceFinder() {}
22
23 /***
24 * Returns a Map containing the URL of each
25 * resource and the File which represents the library
26 * the resource was found in.
27 *
28 * @param resource the resource to find
29 *
30 * @return a <code>array of resource URLs<code>
31 */
32 public static URL[] findResources(String resource) {
33 String methodName = "findResource";
34 if (logger.isDebugEnabled()) {
35 logger.debug("performing " + methodName);
36 }
37 ExceptionUtils.checkEmpty(methodName, "resource", resource);
38
39 URL[] resourceUrls;
40
41 try {
42
43 Collection resources = new ArrayList();
44
45 Enumeration resourceEnum = Thread.currentThread().getContextClassLoader().getResources(resource);
46 while (resourceEnum.hasMoreElements()) {
47 resources.add(resourceEnum.nextElement());
48 }
49
50 resourceUrls = (URL[])resources.toArray(new URL[0]);
51 } catch (Exception ex) {
52 String errMsg = "Error performing " + methodName;
53 logger.error(errMsg, ex);
54 throw new ResourceFinderException(errMsg, ex);
55 }
56
57 return resourceUrls;
58 }
59
60 }