01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.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: * 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: * @author Richard A. Sitze
38: */
39: public class DiscoverNamesInAlternateManagedProperties extends
40: ResourceNameDiscoverImpl implements ResourceNameDiscover {
41: private static Log log = DiscoveryLogFactory
42: .newLog(DiscoverNamesInAlternateManagedProperties.class);
43:
44: public static void setLog(Log _log) {
45: log = _log;
46: }
47:
48: HashMap mapping = new HashMap();
49:
50: /** Construct a new resource discoverer
51: */
52: public DiscoverNamesInAlternateManagedProperties() {
53: }
54:
55: /**
56: */
57: public void addClassToPropertyNameMapping(String className,
58: String propertyName) {
59: mapping.put(className, propertyName);
60: }
61:
62: /**
63: * @return Enumeration of ResourceInfo
64: */
65: public ResourceNameIterator findResourceNames(
66: final String resourceName) {
67: final String mappedName = (String) mapping.get(resourceName);
68:
69: if (log.isDebugEnabled()) {
70: if (mappedName == null) {
71: log.debug("find: resourceName='" + resourceName
72: + "', no mapping");
73: } else {
74: log.debug("find: resourceName='" + resourceName
75: + "', lookup property '" + mappedName + "'");
76: }
77: }
78:
79: return new ResourceNameIterator() {
80: private String resource = (mappedName == null) ? null
81: : ManagedProperties.getProperty(mappedName);
82:
83: public boolean hasNext() {
84: return resource != null;
85: }
86:
87: public String nextResourceName() {
88: String element = resource;
89: resource = null;
90: return element;
91: }
92: };
93: }
94: }
|