001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.discovery.resource;
018:
019: import java.io.IOException;
020: import java.net.URL;
021: import java.util.Enumeration;
022:
023: import org.apache.commons.discovery.Resource;
024: import org.apache.commons.discovery.ResourceDiscover;
025: import org.apache.commons.discovery.ResourceIterator;
026: import org.apache.commons.discovery.jdk.JDKHooks;
027: import org.apache.commons.discovery.log.DiscoveryLogFactory;
028: import org.apache.commons.logging.Log;
029:
030: /**
031: * @author Richard A. Sitze
032: * @author Craig R. McClanahan
033: * @author Costin Manolache
034: * @author James Strachan
035: */
036: public class DiscoverResources extends ResourceDiscoverImpl implements
037: ResourceDiscover {
038: private static Log log = DiscoveryLogFactory
039: .newLog(DiscoverResources.class);
040:
041: public static void setLog(Log _log) {
042: log = _log;
043: }
044:
045: /**
046: * Construct a new resource discoverer
047: */
048: public DiscoverResources() {
049: super ();
050: }
051:
052: /**
053: * Construct a new resource discoverer
054: */
055: public DiscoverResources(ClassLoaders classLoaders) {
056: super (classLoaders);
057: }
058:
059: /**
060: * @return ResourceIterator
061: */
062: public ResourceIterator findResources(final String resourceName) {
063: if (log.isDebugEnabled())
064: log.debug("find: resourceName='" + resourceName + "'");
065:
066: return new ResourceIterator() {
067: private int idx = 0;
068: private ClassLoader loader = null;
069: private Enumeration resources = null;
070: private Resource resource = null;
071:
072: public boolean hasNext() {
073: if (resource == null) {
074: resource = getNextResource();
075: }
076: return resource != null;
077: }
078:
079: public Resource nextResource() {
080: Resource element = resource;
081: resource = null;
082: return element;
083: }
084:
085: private Resource getNextResource() {
086: if (resources == null || !resources.hasMoreElements()) {
087: resources = getNextResources();
088: }
089:
090: Resource resourceInfo;
091: if (resources != null) {
092: URL url = (URL) resources.nextElement();
093:
094: if (log.isDebugEnabled())
095: log.debug("getNextResource: next URL='" + url
096: + "'");
097:
098: resourceInfo = new Resource(resourceName, url,
099: loader);
100: } else {
101: resourceInfo = null;
102: }
103:
104: return resourceInfo;
105: }
106:
107: private Enumeration getNextResources() {
108: while (idx < getClassLoaders().size()) {
109: loader = getClassLoaders().get(idx++);
110: if (log.isDebugEnabled())
111: log
112: .debug("getNextResources: search using ClassLoader '"
113: + loader + "'");
114: try {
115: Enumeration e = JDKHooks.getJDKHooks()
116: .getResources(loader, resourceName);
117: if (e != null && e.hasMoreElements()) {
118: return e;
119: }
120: } catch (IOException ex) {
121: log.warn(
122: "getNextResources: Ignoring Exception",
123: ex);
124: }
125: }
126: return null;
127: }
128: };
129: }
130: }
|