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 /** 029 * Recover resources from a Dictionary. This covers Properties as well, 030 * since <code>Properties extends Hashtable extends Dictionary</code>. 031 * 032 * The recovered value is expected to be either a <code>String</code> 033 * or a <code>String[]</code>. 034 * 035 * @author Richard A. Sitze 036 */ 037 public class DiscoverNamesInDictionary 038 extends ResourceNameDiscoverImpl 039 implements ResourceNameDiscover 040 { 041 private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInDictionary.class); 042 public static void setLog(Log _log) { 043 log = _log; 044 } 045 046 private Dictionary dictionary; 047 048 /** Construct a new resource discoverer 049 */ 050 public DiscoverNamesInDictionary() { 051 setDictionary(new Hashtable()); 052 } 053 054 /** Construct a new resource discoverer 055 */ 056 public DiscoverNamesInDictionary(Dictionary dictionary) { 057 setDictionary(dictionary); 058 } 059 060 protected Dictionary getDictionary() { 061 return dictionary; 062 } 063 064 /** 065 * Specify set of class loaders to be used in searching. 066 */ 067 public void setDictionary(Dictionary table) { 068 this.dictionary = table; 069 } 070 071 public void addResource(String resourceName, String resource) { 072 dictionary.put(resourceName, resource); 073 } 074 075 public void addResource(String resourceName, String[] resources) { 076 dictionary.put(resourceName, resources); 077 } 078 079 /** 080 * @return Enumeration of ResourceInfo 081 */ 082 public ResourceNameIterator findResourceNames(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 && resources[idx] == null) { 103 idx++; 104 } 105 return idx < resources.length; 106 } 107 return false; 108 } 109 110 public String nextResourceName() { 111 return hasNext() ? resources[idx++] : null; 112 } 113 }; 114 } 115 }