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    /**
032     * @author Richard A. Sitze
033     * @author Craig R. McClanahan
034     * @author Costin Manolache
035     * @author James Strachan
036     */
037    public class DiscoverResources
038        extends ResourceDiscoverImpl
039        implements ResourceDiscover
040    {
041        private static Log log = DiscoveryLogFactory.newLog(DiscoverResources.class);
042        public static void setLog(Log _log) {
043            log = _log;
044        }
045        
046        /**
047         * Construct a new resource discoverer
048         */
049        public DiscoverResources() {
050            super();
051        }
052        
053        /**
054         *  Construct a new resource discoverer
055         */
056        public DiscoverResources(ClassLoaders classLoaders) {
057            super(classLoaders);
058        }
059    
060        /**
061         * @return ResourceIterator
062         */
063        public ResourceIterator findResources(final String resourceName) {
064            if (log.isDebugEnabled())
065                log.debug("find: resourceName='" + resourceName + "'");
066    
067            return new ResourceIterator() {
068                private int idx = 0;
069                private ClassLoader loader = null;
070                private Enumeration resources = null;
071                private Resource resource = null;
072                
073                public boolean hasNext() {
074                    if (resource == null) {
075                        resource = getNextResource();
076                    }
077                    return resource != null;
078                }
079                
080                public Resource nextResource() {
081                    Resource element = resource;
082                    resource = null;
083                    return element;
084                }
085                
086                private Resource getNextResource() {
087                    if (resources == null || !resources.hasMoreElements()) {
088                        resources = getNextResources();
089                    }
090    
091                    Resource resourceInfo;
092                    if (resources != null) {
093                        URL url = (URL)resources.nextElement();
094    
095                        if (log.isDebugEnabled())
096                            log.debug("getNextResource: next URL='" + url + "'");
097    
098                        resourceInfo = new Resource(resourceName, url, loader);
099                    } else {
100                        resourceInfo = null;
101                    }
102                    
103                    return resourceInfo;
104                }
105                
106                private Enumeration getNextResources() {
107                    while (idx < getClassLoaders().size()) {
108                        loader = getClassLoaders().get(idx++);
109                        if (log.isDebugEnabled())
110                            log.debug("getNextResources: search using ClassLoader '" + loader + "'");
111                        try {
112                            Enumeration e = JDKHooks.getJDKHooks().getResources(loader, resourceName);
113                            if (e != null && e.hasMoreElements()) {
114                                return e;
115                            }
116                        } catch( IOException ex ) {
117                            log.warn("getNextResources: Ignoring Exception", ex);
118                        }
119                    }
120                    return null;
121                }
122            };
123        }
124    }