View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.discovery.resource;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.Enumeration;
22  
23  import org.apache.commons.discovery.Resource;
24  import org.apache.commons.discovery.ResourceDiscover;
25  import org.apache.commons.discovery.ResourceIterator;
26  import org.apache.commons.discovery.jdk.JDKHooks;
27  import org.apache.commons.discovery.log.DiscoveryLogFactory;
28  import org.apache.commons.logging.Log;
29  
30  
31  /***
32   * @author Richard A. Sitze
33   * @author Craig R. McClanahan
34   * @author Costin Manolache
35   * @author James Strachan
36   */
37  public class DiscoverResources
38      extends ResourceDiscoverImpl
39      implements ResourceDiscover
40  {
41      private static Log log = DiscoveryLogFactory.newLog(DiscoverResources.class);
42      public static void setLog(Log _log) {
43          log = _log;
44      }
45      
46      /***
47       * Construct a new resource discoverer
48       */
49      public DiscoverResources() {
50          super();
51      }
52      
53      /***
54       *  Construct a new resource discoverer
55       */
56      public DiscoverResources(ClassLoaders classLoaders) {
57          super(classLoaders);
58      }
59  
60      /***
61       * @return ResourceIterator
62       */
63      public ResourceIterator findResources(final String resourceName) {
64          if (log.isDebugEnabled())
65              log.debug("find: resourceName='" + resourceName + "'");
66  
67          return new ResourceIterator() {
68              private int idx = 0;
69              private ClassLoader loader = null;
70              private Enumeration resources = null;
71              private Resource resource = null;
72              
73              public boolean hasNext() {
74                  if (resource == null) {
75                      resource = getNextResource();
76                  }
77                  return resource != null;
78              }
79              
80              public Resource nextResource() {
81                  Resource element = resource;
82                  resource = null;
83                  return element;
84              }
85              
86              private Resource getNextResource() {
87                  if (resources == null || !resources.hasMoreElements()) {
88                      resources = getNextResources();
89                  }
90  
91                  Resource resourceInfo;
92                  if (resources != null) {
93                      URL url = (URL)resources.nextElement();
94  
95                      if (log.isDebugEnabled())
96                          log.debug("getNextResource: next URL='" + url + "'");
97  
98                      resourceInfo = new Resource(resourceName, url, loader);
99                  } 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 }