View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.discovery.resource.names;
18  
19  import java.util.Dictionary;
20  import java.util.Hashtable;
21  
22  import org.apache.commons.discovery.ResourceNameDiscover;
23  import org.apache.commons.discovery.ResourceNameIterator;
24  import org.apache.commons.discovery.log.DiscoveryLogFactory;
25  import org.apache.commons.logging.Log;
26  
27  
28  /***
29   * Recover resources from a Dictionary.  This covers Properties as well,
30   * since <code>Properties extends Hashtable extends Dictionary</code>.
31   * 
32   * The recovered value is expected to be either a <code>String</code>
33   * or a <code>String[]</code>.
34   * 
35   * @author Richard A. Sitze
36   */
37  public class DiscoverNamesInDictionary
38      extends ResourceNameDiscoverImpl
39      implements ResourceNameDiscover
40  {
41      private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInDictionary.class);
42      public static void setLog(Log _log) {
43          log = _log;
44      }
45  
46      private Dictionary dictionary;
47      
48      /*** Construct a new resource discoverer
49       */
50      public DiscoverNamesInDictionary() {
51          setDictionary(new Hashtable());
52      }
53      
54      /*** Construct a new resource discoverer
55       */
56      public DiscoverNamesInDictionary(Dictionary dictionary) {
57          setDictionary(dictionary);
58      }
59  
60      protected Dictionary getDictionary() {
61          return dictionary;
62      }
63  
64      /***
65       * Specify set of class loaders to be used in searching.
66       */
67      public void setDictionary(Dictionary table) {
68          this.dictionary = table;
69      }
70      
71      public void addResource(String resourceName, String resource) {
72          dictionary.put(resourceName, resource);
73      }
74      
75      public void addResource(String resourceName, String[] resources) {
76          dictionary.put(resourceName, resources);
77      }
78  
79      /***
80       * @return Enumeration of ResourceInfo
81       */
82      public ResourceNameIterator findResourceNames(final String resourceName) {
83          if (log.isDebugEnabled())
84              log.debug("find: resourceName='" + resourceName + "'");
85  
86          Object baseResource = dictionary.get(resourceName);
87  
88          final String[] resources;
89          if (baseResource instanceof String) {
90              resources = new String[] { (String)baseResource };
91          } else if (baseResource instanceof String[]) {
92              resources = (String[])baseResource;
93          } else {
94              resources = null;
95          }
96  
97          return new ResourceNameIterator() {
98              private int idx = 0;
99              
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 }