1 57 58 package org.apache.commons.discovery.resource; 59 60 import java.io.IOException ; 61 import java.net.URL ; 62 import java.util.Enumeration ; 63 64 import org.apache.commons.discovery.Resource; 65 import org.apache.commons.discovery.ResourceDiscover; 66 import org.apache.commons.discovery.ResourceNameListener; 67 import org.apache.commons.discovery.jdk.JDKHooks; 68 import org.apache.commons.discovery.log.DiscoveryLogFactory; 69 import org.apache.commons.logging.Log; 70 71 72 78 public class DiscoverResources extends ResourceDiscoverImpl 79 implements ResourceDiscover, ResourceNameListener 80 { 81 private static Log log = DiscoveryLogFactory.newLog(DiscoverResources.class); 82 public static void setLog(Log _log) { 83 log = _log; 84 } 85 86 89 public DiscoverResources() { 90 super(); 91 } 92 93 96 public DiscoverResources(ClassLoaders classLoaders) { 97 super(classLoaders); 98 } 99 100 103 public boolean find(String resourceName) { 104 if (log.isDebugEnabled()) 105 log.debug("find: resourceName='" + resourceName + "'"); 106 107 for (int idx = 0; idx < getClassLoaders().size(); idx++) { 108 ClassLoader loader = getClassLoaders().get(idx); 109 if (log.isDebugEnabled()) 110 log.debug("find: search using ClassLoader '" + loader + "'"); 111 112 try { 113 Enumeration resources = 114 JDKHooks.getJDKHooks().getResources(loader, resourceName); 115 116 if (resources != null) { 117 while(resources.hasMoreElements()) { 118 if (!notifyListener(new Resource(resourceName, 119 (URL )resources.nextElement(), 120 loader))) { 121 return false; 122 } 123 } 124 } 125 } catch( IOException ex ) { 126 log.warn("find: Ignoring Exception", ex); 127 } 128 } 129 return true; 130 } 131 132 public boolean found(String resourceName) { 133 return find(resourceName); 134 } 135 } 136 | Popular Tags |