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.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28 * Recover resources from a Dictionary. This covers Properties as well,
29 * since <code>Properties extends Hashtable extends Dictionary</code>.
30 *
31 * The recovered value is expected to be either a <code>String</code>
32 * or a <code>String[]</code>.
33 */
34 public class DiscoverNamesInDictionary extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
35
36 private static Log log = LogFactory.getLog(DiscoverNamesInDictionary.class);
37
38 /**
39 * Sets the {@code Log} for this class.
40 *
41 * @param _log This class {@code Log}
42 * @deprecated This method is not thread-safe
43 */
44 @Deprecated
45 public static void setLog(Log _log) {
46 log = _log;
47 }
48
49 private Dictionary<String, String[]> dictionary;
50
51 /**
52 * Construct a new resource discoverer with an empty Dictionary.
53 */
54 public DiscoverNamesInDictionary() {
55 setDictionary(new Hashtable<String, String[]>());
56 }
57
58 /**
59 * Construct a new resource discoverer with the given Dictionary.
60 *
61 * @param dictionary The initial Dictionary
62 */
63 public DiscoverNamesInDictionary(Dictionary<String, String[]> dictionary) {
64 setDictionary(dictionary);
65 }
66
67 /**
68 * Returns the current Dictionary for names mapping.
69 *
70 * @return The current Dictionary for names mapping
71 */
72 protected Dictionary<String, String[]> getDictionary() {
73 return dictionary;
74 }
75
76 /**
77 * Specify the Dictionary for names mapping.
78 *
79 * @param table The Dictionary for names mapping
80 */
81 public void setDictionary(Dictionary<String, String[]> table) {
82 this.dictionary = table;
83 }
84
85 /**
86 * Add a resource name to a single name mapping.
87 *
88 * @param resourceName The resource name
89 * @param resource The target name
90 */
91 public void addResource(String resourceName, String resource) {
92 addResource(resourceName, new String[]{ resource });
93 }
94
95 /**
96 * Add a resource name to multiple names mapping.
97 *
98 * @param resourceName The resource name
99 * @param resources The target names
100 */
101 public void addResource(String resourceName, String[] resources) {
102 dictionary.put(resourceName, resources);
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public ResourceNameIterator findResourceNames(final String resourceName) {
110 if (log.isDebugEnabled()) {
111 log.debug("find: resourceName='" + resourceName + "'");
112 }
113
114 final String[] resources = dictionary.get(resourceName);
115
116 return new ResourceNameIterator() {
117 private int idx = 0;
118
119 public boolean hasNext() {
120 if (resources != null) {
121 while (idx < resources.length && resources[idx] == null) {
122 idx++;
123 }
124 return idx < resources.length;
125 }
126 return false;
127 }
128
129 public String nextResourceName() {
130 return hasNext() ? resources[idx++] : null;
131 }
132 };
133 }
134
135 }