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.ArrayList; 020 import java.util.List; 021 022 import org.apache.commons.discovery.ResourceNameDiscover; 023 import org.apache.commons.discovery.ResourceNameIterator; 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 027 /** 028 * Holder for multiple ResourceNameDiscover instances. 029 * 030 * The result is the union of the results from each 031 * (not a chained sequence, where results feed the next in line. 032 */ 033 public class NameDiscoverers extends ResourceNameDiscoverImpl implements ResourceNameDiscover { 034 035 private static Log log = LogFactory.getLog(NameDiscoverers.class); 036 037 /** 038 * Sets the {@code Log} for this class. 039 * 040 * @param _log This class {@code Log} 041 * @deprecated This method is not thread-safe 042 */ 043 @Deprecated 044 public static void setLog(Log _log) { 045 log = _log; 046 } 047 048 private final List<ResourceNameDiscover> discoverers = new ArrayList<ResourceNameDiscover>(); 049 050 /** 051 * Construct a new resource name discoverer 052 */ 053 public NameDiscoverers() { 054 } 055 056 /** 057 * Specify an discover to be used in searching. 058 * The order of discover determines the order of the result. 059 * It is recommended to add the most specific discover first. 060 * 061 * @param discover The discover to be added 062 */ 063 public void addResourceNameDiscover(ResourceNameDiscover discover) { 064 if (discover != null) { 065 discoverers.add(discover); 066 } 067 } 068 069 /** 070 * Retrieve the discover positioned at the given index. 071 * 072 * @param idx The discover index position client is requiring 073 * @return The discover positioned at the input index 074 */ 075 protected ResourceNameDiscover getResourceNameDiscover(int idx) { 076 return discoverers.get(idx); 077 } 078 079 /** 080 * Returns the current size of set discovers. 081 * 082 * @return The current size of set discovers 083 */ 084 protected int size() { 085 return discoverers.size(); 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 @Override 092 public ResourceNameIterator findResourceNames(final String resourceName) { 093 if (log.isDebugEnabled()) { 094 log.debug("find: resourceName='" + resourceName + "'"); 095 } 096 097 return new ResourceNameIterator() { 098 099 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 }