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.classes;
18  
19  import org.apache.commons.discovery.ResourceClass;
20  import org.apache.commons.discovery.ResourceClassDiscover;
21  import org.apache.commons.discovery.ResourceClassIterator;
22  import org.apache.commons.discovery.ResourceIterator;
23  import org.apache.commons.discovery.ResourceNameIterator;
24  import org.apache.commons.discovery.resource.ClassLoaders;
25  import org.apache.commons.discovery.resource.ResourceDiscoverImpl;
26  
27  
28  /***
29   * @author Richard A. Sitze
30   */
31  public abstract class ResourceClassDiscoverImpl
32      extends ResourceDiscoverImpl
33      implements ResourceClassDiscover
34  {
35      /***
36       * Construct a new resource discoverer
37       */
38      public ResourceClassDiscoverImpl() {
39          super();
40      }
41      
42      /***
43       *  Construct a new resource discoverer
44       */
45      public ResourceClassDiscoverImpl(ClassLoaders classLoaders) {
46          super(classLoaders);
47      }
48  
49  
50      /***
51       * Locate names of resources that are bound to <code>resourceName</code>.
52       * 
53       * @return ResourceNameIterator
54       */
55      public ResourceNameIterator findResourceNames(String resourceName) {
56          return findResourceClasses(resourceName);
57      }
58  
59      /***
60       * Locate names of resources that are bound to <code>resourceNames</code>.
61       * 
62       * @return ResourceNameIterator
63       */
64      public ResourceNameIterator findResourceNames(ResourceNameIterator resourceNames) {
65          return findResourceClasses(resourceNames);
66      }
67  
68      /***
69       * Locate resources that are bound to <code>resourceName</code>.
70       * 
71       * @return ResourceIterator
72       */
73      public ResourceIterator findResources(String resourceName) {
74          return findResourceClasses(resourceName);
75      }
76  
77      /***
78       * Locate resources that are bound to <code>resourceNames</code>.
79       * 
80       * @return ResourceIterator
81       */
82      public ResourceIterator findResources(ResourceNameIterator resourceNames) {
83          return findResourceClasses(resourceNames);
84      }
85  
86  
87      /***
88       * Locate class resources that are bound to <code>className</code>.
89       * 
90       * @return ResourceClassIterator
91       */
92      public abstract ResourceClassIterator findResourceClasses(String className);
93  
94      /***
95       * Locate class resources that are bound to <code>resourceNames</code>.
96       * 
97       * @return ResourceIterator
98       */
99      public ResourceClassIterator findResourceClasses(final ResourceNameIterator inputNames) {
100         return new ResourceClassIterator() {
101             private ResourceClassIterator classes = null;
102             private ResourceClass resource = null;
103             
104             public boolean hasNext() {
105                 if (resource == null) {
106                     resource = getNextResource();
107                 }
108                 return resource != null;
109             }
110             
111             public ResourceClass nextResourceClass() {
112                 ResourceClass rsrc = resource;
113                 resource = null;
114                 return rsrc;
115             }
116             
117             private ResourceClass getNextResource() {
118                 while (inputNames.hasNext() &&
119                        (classes == null  ||  !classes.hasNext())) {
120                     classes =
121                         findResourceClasses(inputNames.nextResourceName());
122                 }
123     
124                 return (classes != null  &&  classes.hasNext())
125                        ? classes.nextResourceClass()
126                        : null;
127             }
128         };
129     }
130 }