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.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.discovery.ResourceNameDiscover;
23 import org.apache.commons.discovery.ResourceNameIterator;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28 * Holder for multiple ResourceNameDiscover instances.
29 *
30 * The result is the union of the results from each
31 * (not a chained sequence, where results feed the next in line.
32 */
33 public class NameDiscoverers extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
34
35 private static Log log = LogFactory.getLog(NameDiscoverers.class);
36
37 /**
38 * Sets the {@code Log} for this class.
39 *
40 * @param _log This class {@code Log}
41 * @deprecated This method is not thread-safe
42 */
43 @Deprecated
44 public static void setLog(Log _log) {
45 log = _log;
46 }
47
48 private final List<ResourceNameDiscover> discoverers = new ArrayList<ResourceNameDiscover>();
49
50 /**
51 * Construct a new resource name discoverer
52 */
53 public NameDiscoverers() {
54 }
55
56 /**
57 * Specify an discover to be used in searching.
58 * The order of discover determines the order of the result.
59 * It is recommended to add the most specific discover first.
60 *
61 * @param discover The discover to be added
62 */
63 public void addResourceNameDiscover(ResourceNameDiscover discover) {
64 if (discover != null) {
65 discoverers.add(discover);
66 }
67 }
68
69 /**
70 * Retrieve the discover positioned at the given index.
71 *
72 * @param idx The discover index position client is requiring
73 * @return The discover positioned at the input index
74 */
75 protected ResourceNameDiscover getResourceNameDiscover(int idx) {
76 return discoverers.get(idx);
77 }
78
79 /**
80 * Returns the current size of set discovers.
81 *
82 * @return The current size of set discovers
83 */
84 protected int size() {
85 return discoverers.size();
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public ResourceNameIterator findResourceNames(final String resourceName) {
93 if (log.isDebugEnabled()) {
94 log.debug("find: resourceName='" + resourceName + "'");
95 }
96
97 return new ResourceNameIterator() {
98
99 private int idx = 0;
100
101 private ResourceNameIterator iterator = null;
102
103 public boolean hasNext() {
104 if (iterator == null || !iterator.hasNext()) {
105 iterator = getNextIterator();
106 if (iterator == null) {
107 return false;
108 }
109 }
110 return iterator.hasNext();
111 }
112
113 public String nextResourceName() {
114 return iterator.nextResourceName();
115 }
116
117 private ResourceNameIterator getNextIterator() {
118 while (idx < size()) {
119 ResourceNameIterator iter =
120 getResourceNameDiscover(idx++).findResourceNames(resourceName);
121
122 if (iter.hasNext()) {
123 return iter;
124 }
125 }
126 return null;
127 }
128 };
129 }
130
131 }