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 /**
028 * Holder for multiple ResourceNameDiscover instances.
029 * The result is the union of the results from each
030 * (not a chained sequence, where results feed the next in line.
031 *
032 * @author Richard A. Sitze
033 */
034 public class NameDiscoverers
035 extends ResourceNameDiscoverImpl
036 implements ResourceNameDiscover
037 {
038 private static Log log = DiscoveryLogFactory.newLog(NameDiscoverers.class);
039 public static void setLog(Log _log) {
040 log = _log;
041 }
042
043 private Vector discoverers = new Vector();
044
045 /**
046 * Construct a new resource name discoverer
047 */
048 public NameDiscoverers() {
049 }
050
051 /**
052 * Specify an additional class loader to be used in searching.
053 * The order of loaders determines the order of the result.
054 * It is recommended to add the most specific loaders first.
055 */
056 public void addResourceNameDiscover(ResourceNameDiscover discover) {
057 if (discover != null) {
058 discoverers.addElement(discover);
059 }
060 }
061
062 protected ResourceNameDiscover getResourceNameDiscover(int idx) {
063 return (ResourceNameDiscover)discoverers.get(idx);
064 }
065
066 protected int size() {
067 return discoverers.size();
068 }
069
070 /**
071 * Set of results of all discoverers.
072 *
073 * @return ResourceIterator
074 */
075 public ResourceNameIterator findResourceNames(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 =
100 getResourceNameDiscover(idx++).findResourceNames(resourceName);
101
102 if (iter.hasNext()) {
103 return iter;
104 }
105 }
106 return null;
107 }
108 };
109 }
110 }