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.names;
18  
19  import java.util.Vector;
20  
21  import org.apache.commons.discovery.ResourceNameDiscover;
22  import org.apache.commons.discovery.ResourceNameIterator;
23  import org.apache.commons.discovery.log.DiscoveryLogFactory;
24  import org.apache.commons.logging.Log;
25  
26  
27  /***
28   * Holder for multiple ResourceNameDiscover instances.
29   * The result is the union of the results from each
30   * (not a chained sequence, where results feed the next in line.
31   *
32   * @author Richard A. Sitze
33   */
34  public class NameDiscoverers
35      extends ResourceNameDiscoverImpl
36      implements ResourceNameDiscover
37  {
38      private static Log log = DiscoveryLogFactory.newLog(NameDiscoverers.class);
39      public static void setLog(Log _log) {
40          log = _log;
41      }
42  
43      private Vector discoverers = new Vector();
44      
45      /***
46       *  Construct a new resource name discoverer
47       */
48      public NameDiscoverers() {
49      }
50      
51      /***
52       * Specify an additional class loader to be used in searching.
53       * The order of loaders determines the order of the result.
54       * It is recommended to add the most specific loaders first.
55       */
56      public void addResourceNameDiscover(ResourceNameDiscover discover) {
57          if (discover != null) {
58              discoverers.addElement(discover);
59          }
60      }
61  
62      protected ResourceNameDiscover getResourceNameDiscover(int idx) {
63          return (ResourceNameDiscover)discoverers.get(idx);
64      }
65  
66      protected int size() {
67          return discoverers.size();
68      }
69  
70      /***
71       * Set of results of all discoverers.
72       * 
73       * @return ResourceIterator
74       */
75      public ResourceNameIterator findResourceNames(final String resourceName) {
76          if (log.isDebugEnabled())
77              log.debug("find: resourceName='" + resourceName + "'");
78  
79          return new ResourceNameIterator() {
80              private int idx = 0;
81              private ResourceNameIterator iterator = null;
82              
83              public boolean hasNext() {
84                  if (iterator == null  ||  !iterator.hasNext()) {
85                      iterator = getNextIterator();
86                      if (iterator == null) {
87                          return false;
88                      }
89                  }
90                  return iterator.hasNext();
91              }
92              
93              public String nextResourceName() {
94                  return iterator.nextResourceName();
95              }
96              
97              private ResourceNameIterator getNextIterator() {
98                  while (idx < size()) {
99                      ResourceNameIterator iter =
100                         getResourceNameDiscover(idx++).findResourceNames(resourceName);
101 
102                     if (iter.hasNext()) {
103                         return iter;
104                     }
105                 }
106                 return null;
107             }
108         };
109     }
110 }