001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.discovery.resource.names;
018:
019: import java.util.Vector;
020:
021: import org.apache.commons.discovery.ResourceNameDiscover;
022: import org.apache.commons.discovery.ResourceNameIterator;
023: import org.apache.commons.discovery.log.DiscoveryLogFactory;
024: import org.apache.commons.logging.Log;
025:
026: /**
027: * Holder for multiple ResourceNameDiscover instances.
028: * The result is the union of the results from each
029: * (not a chained sequence, where results feed the next in line.
030: *
031: * @author Richard A. Sitze
032: */
033: public class NameDiscoverers extends ResourceNameDiscoverImpl implements
034: ResourceNameDiscover {
035: private static Log log = DiscoveryLogFactory
036: .newLog(NameDiscoverers.class);
037:
038: public static void setLog(Log _log) {
039: log = _log;
040: }
041:
042: private Vector discoverers = new Vector();
043:
044: /**
045: * Construct a new resource name discoverer
046: */
047: public NameDiscoverers() {
048: }
049:
050: /**
051: * Specify an additional class loader to be used in searching.
052: * The order of loaders determines the order of the result.
053: * It is recommended to add the most specific loaders first.
054: */
055: public void addResourceNameDiscover(ResourceNameDiscover discover) {
056: if (discover != null) {
057: discoverers.addElement(discover);
058: }
059: }
060:
061: protected ResourceNameDiscover getResourceNameDiscover(int idx) {
062: return (ResourceNameDiscover) discoverers.get(idx);
063: }
064:
065: protected int size() {
066: return discoverers.size();
067: }
068:
069: /**
070: * Set of results of all discoverers.
071: *
072: * @return ResourceIterator
073: */
074: public ResourceNameIterator findResourceNames(
075: final String resourceName) {
076: if (log.isDebugEnabled())
077: log.debug("find: resourceName='" + resourceName + "'");
078:
079: return new ResourceNameIterator() {
080: private int idx = 0;
081: private ResourceNameIterator iterator = null;
082:
083: public boolean hasNext() {
084: if (iterator == null || !iterator.hasNext()) {
085: iterator = getNextIterator();
086: if (iterator == null) {
087: return false;
088: }
089: }
090: return iterator.hasNext();
091: }
092:
093: public String nextResourceName() {
094: return iterator.nextResourceName();
095: }
096:
097: private ResourceNameIterator getNextIterator() {
098: while (idx < size()) {
099: ResourceNameIterator iter = getResourceNameDiscover(
100: idx++).findResourceNames(resourceName);
101:
102: if (iter.hasNext()) {
103: return iter;
104: }
105: }
106: return null;
107: }
108: };
109: }
110: }
|