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.Dictionary;
020: import java.util.Hashtable;
021:
022: import org.apache.commons.discovery.ResourceNameDiscover;
023: import org.apache.commons.discovery.ResourceNameIterator;
024: import org.apache.commons.discovery.log.DiscoveryLogFactory;
025: import org.apache.commons.logging.Log;
026:
027: /**
028: * Recover resources from a Dictionary. This covers Properties as well,
029: * since <code>Properties extends Hashtable extends Dictionary</code>.
030: *
031: * The recovered value is expected to be either a <code>String</code>
032: * or a <code>String[]</code>.
033: *
034: * @author Richard A. Sitze
035: */
036: public class DiscoverNamesInDictionary extends ResourceNameDiscoverImpl
037: implements ResourceNameDiscover {
038: private static Log log = DiscoveryLogFactory
039: .newLog(DiscoverNamesInDictionary.class);
040:
041: public static void setLog(Log _log) {
042: log = _log;
043: }
044:
045: private Dictionary dictionary;
046:
047: /** Construct a new resource discoverer
048: */
049: public DiscoverNamesInDictionary() {
050: setDictionary(new Hashtable());
051: }
052:
053: /** Construct a new resource discoverer
054: */
055: public DiscoverNamesInDictionary(Dictionary dictionary) {
056: setDictionary(dictionary);
057: }
058:
059: protected Dictionary getDictionary() {
060: return dictionary;
061: }
062:
063: /**
064: * Specify set of class loaders to be used in searching.
065: */
066: public void setDictionary(Dictionary table) {
067: this .dictionary = table;
068: }
069:
070: public void addResource(String resourceName, String resource) {
071: dictionary.put(resourceName, resource);
072: }
073:
074: public void addResource(String resourceName, String[] resources) {
075: dictionary.put(resourceName, resources);
076: }
077:
078: /**
079: * @return Enumeration of ResourceInfo
080: */
081: public ResourceNameIterator findResourceNames(
082: final String resourceName) {
083: if (log.isDebugEnabled())
084: log.debug("find: resourceName='" + resourceName + "'");
085:
086: Object baseResource = dictionary.get(resourceName);
087:
088: final String[] resources;
089: if (baseResource instanceof String) {
090: resources = new String[] { (String) baseResource };
091: } else if (baseResource instanceof String[]) {
092: resources = (String[]) baseResource;
093: } else {
094: resources = null;
095: }
096:
097: return new ResourceNameIterator() {
098: private int idx = 0;
099:
100: public boolean hasNext() {
101: if (resources != null) {
102: while (idx < resources.length
103: && resources[idx] == null) {
104: idx++;
105: }
106: return idx < resources.length;
107: }
108: return false;
109: }
110:
111: public String nextResourceName() {
112: return hasNext() ? resources[idx++] : null;
113: }
114: };
115: }
116: }
|