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.Hashtable;
20 import java.util.Map;
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 resource name from Managed Properties,
29 * using OLD property names.
30 *
31 * This class maintains a mapping between old names and
32 * (new) the class names they represent. The discovery
33 * mechanism uses the class names as property names.
34 *
35 * @see org.apache.commons.discovery.tools.ManagedProperties
36 */
37 public class DiscoverMappedNames extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
38
39 private static Log log = LogFactory.getLog(DiscoverMappedNames.class);
40
41 /**
42 * Sets the {@code Log} for this class.
43 *
44 * @param _log This class {@code Log}
45 * @deprecated This method is not thread-safe
46 */
47 @Deprecated
48 public static void setLog(Log _log) {
49 log = _log;
50 }
51
52 /**
53 * The String name ==> String[] newNames mapping
54 */
55 private final Map<String, String[]> mapping = new Hashtable<String, String[]>();
56
57 /**
58 * Construct a new resource discoverer
59 */
60 public DiscoverMappedNames() {
61 }
62
63 /**
64 * Maps a name to another name.
65 *
66 * @param fromName The name has to be mapped
67 * @param toName The mapping target
68 */
69 public void map(String fromName, String toName) {
70 map(fromName, new String[]{ toName });
71 }
72
73 /**
74 * Maps a name to multiple names.
75 *
76 * @param fromName The name has to be mapped
77 * @param toNames The mapping targets
78 */
79 public void map(String fromName, String [] toNames) {
80 mapping.put(fromName, toNames);
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 public ResourceNameIterator findResourceNames(final String resourceName) {
88 if (log.isDebugEnabled()) {
89 log.debug("find: resourceName='" + resourceName + "', mapping to constants");
90 }
91
92 final String[] names = mapping.get(resourceName);
93
94 return new ResourceNameIterator() {
95
96 private int idx = 0;
97
98 public boolean hasNext() {
99 if (names != null) {
100 while (idx < names.length && names[idx] == null) {
101 idx++;
102 }
103 return idx < names.length;
104 }
105 return false;
106 }
107
108 public String nextResourceName() {
109 return hasNext() ? names[idx++] : null;
110 }
111 };
112 }
113
114 }