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 org.apache.commons.discovery.Resource;
20  import org.apache.commons.discovery.ResourceDiscover;
21  import org.apache.commons.discovery.ResourceIterator;
22  import org.apache.commons.discovery.ResourceNameIterator;
23  import org.apache.commons.discovery.resource.names.ResourceNameDiscoverImpl;
24  
25  
26  /***
27   * Helper class for methods implementing the ResourceDiscover interface.
28   * 
29   * @author Richard A. Sitze
30   */
31  public abstract class ResourceDiscoverImpl
32      extends ResourceNameDiscoverImpl
33      implements ResourceDiscover
34  {
35      private ClassLoaders classLoaders;
36  
37      
38      /***
39       * Construct a new resource discoverer
40       */
41      public ResourceDiscoverImpl() {
42      }
43      
44      /***
45       *  Construct a new resource discoverer
46       */
47      public ResourceDiscoverImpl(ClassLoaders classLoaders) {
48          setClassLoaders(classLoaders);
49      }
50  
51      /***
52       * Specify set of class loaders to be used in searching.
53       */
54      public void setClassLoaders(ClassLoaders loaders) {
55          classLoaders = loaders;
56      }
57  
58      /***
59       * Specify a new class loader to be used in searching.
60       * The order of loaders determines the order of the result.
61       * It is recommended to add the most specific loaders first.
62       */
63      public void addClassLoader(ClassLoader loader) {
64          getClassLoaders().put(loader);
65      }
66  
67      protected ClassLoaders getClassLoaders() {
68          if (classLoaders == null) {
69              classLoaders = ClassLoaders.getLibLoaders(this.getClass(), null, true);
70          }
71          return classLoaders;
72      }
73  
74      /***
75       * Locate names of resources that are bound to <code>resourceName</code>.
76       * 
77       * @return ResourceNameIterator
78       */
79      public ResourceNameIterator findResourceNames(String resourceName) {
80          return findResources(resourceName);
81      }
82  
83      /***
84       * Locate names of resources that are bound to <code>resourceNames</code>.
85       * 
86       * @return ResourceNameIterator
87       */
88      public ResourceNameIterator findResourceNames(ResourceNameIterator resourceNames) {
89          return findResources(resourceNames);
90      }
91  
92      /***
93       * Locate resources that are bound to <code>resourceName</code>.
94       * 
95       * @return ResourceIterator
96       */
97      public abstract ResourceIterator findResources(String resourceName);
98  
99      /***
100      * Locate resources that are bound to <code>resourceNames</code>.
101      * 
102      * @return ResourceIterator
103      */
104     public ResourceIterator findResources(final ResourceNameIterator inputNames) {
105         return new ResourceIterator() {
106             private ResourceIterator resources = null;
107             private Resource resource = null;
108             
109             public boolean hasNext() {
110                 if (resource == null) {
111                     resource = getNextResource();
112                 }
113                 return resource != null;
114             }
115             
116             public Resource nextResource() {
117                 Resource rsrc = resource;
118                 resource = null;
119                 return rsrc;
120             }
121             
122             private Resource getNextResource() {
123                 while (inputNames.hasNext() &&
124                        (resources == null  ||  !resources.hasNext())) {
125                     resources = findResources(inputNames.nextResourceName());
126                 }
127     
128                 return (resources != null  &&  resources.hasNext())
129                        ? resources.nextResource()
130                        : null;
131             }
132         };
133     }
134 }