1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.resource.names;
18
19 import java.util.HashMap;
20
21 import org.apache.commons.discovery.ResourceNameDiscover;
22 import org.apache.commons.discovery.ResourceNameIterator;
23 import org.apache.commons.discovery.log.DiscoveryLogFactory;
24 import org.apache.commons.discovery.tools.ManagedProperties;
25 import org.apache.commons.logging.Log;
26
27
28 /***
29 * Recover resource name from Managed Properties,
30 * using OLD property names.
31 *
32 * This class maintains a mapping between old names and
33 * (new) the class names they represent. The discovery
34 * mechanism uses the class names as property names.
35 *
36 * @see org.apache.commons.discovery.tools.ManagedProperties
37 *
38 * @author Richard A. Sitze
39 */
40 public class DiscoverNamesInAlternateManagedProperties
41 extends ResourceNameDiscoverImpl
42 implements ResourceNameDiscover
43 {
44 private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInAlternateManagedProperties.class);
45 public static void setLog(Log _log) {
46 log = _log;
47 }
48
49 HashMap mapping = new HashMap();
50
51 /*** Construct a new resource discoverer
52 */
53 public DiscoverNamesInAlternateManagedProperties() {
54 }
55
56 /***
57 */
58 public void addClassToPropertyNameMapping(String className, String propertyName) {
59 mapping.put(className, propertyName);
60 }
61
62 /***
63 * @return Enumeration of ResourceInfo
64 */
65 public ResourceNameIterator findResourceNames(final String resourceName) {
66 final String mappedName = (String)mapping.get(resourceName);
67
68 if (log.isDebugEnabled()) {
69 if (mappedName == null) {
70 log.debug("find: resourceName='" + resourceName + "', no mapping");
71 } else {
72 log.debug("find: resourceName='" + resourceName + "', lookup property '" + mappedName + "'");
73 }
74 }
75
76 return new ResourceNameIterator() {
77 private String resource =
78 (mappedName == null) ? null : ManagedProperties.getProperty(mappedName);
79
80 public boolean hasNext() {
81 return resource != null;
82 }
83
84 public String nextResourceName() {
85 String element = resource;
86 resource = null;
87 return element;
88 }
89 };
90 }
91 }