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 org.apache.commons.discovery.ResourceNameDiscover;
20: import org.apache.commons.discovery.ResourceNameIterator;
21: import org.apache.commons.discovery.log.DiscoveryLogFactory;
22: import org.apache.commons.discovery.tools.ManagedProperties;
23: import org.apache.commons.logging.Log;
24:
25: /**
26: * Recover resource name from Managed Properties.
27: * @see org.apache.commons.discovery.tools.ManagedProperties
28: *
29: * @author Richard A. Sitze
30: */
31: public class DiscoverNamesInManagedProperties extends
32: ResourceNameDiscoverImpl implements ResourceNameDiscover {
33: private static Log log = DiscoveryLogFactory
34: .newLog(DiscoverNamesInManagedProperties.class);
35:
36: public static void setLog(Log _log) {
37: log = _log;
38: }
39:
40: private final String _prefix;
41: private final String _suffix;
42:
43: /** Construct a new resource discoverer
44: */
45: public DiscoverNamesInManagedProperties() {
46: _prefix = null;
47: _suffix = null;
48: }
49:
50: /** Construct a new resource discoverer
51: */
52: public DiscoverNamesInManagedProperties(String prefix, String suffix) {
53: _prefix = prefix;
54: _suffix = suffix;
55: }
56:
57: /**
58: * @return Enumeration of ResourceInfo
59: */
60: public ResourceNameIterator findResourceNames(
61: final String resourceName) {
62: String name;
63: if (_prefix != null && _prefix.length() > 0) {
64: name = _prefix + resourceName;
65: } else {
66: name = resourceName;
67: }
68:
69: if (_suffix != null && _suffix.length() > 0) {
70: name = name + _suffix;
71: }
72:
73: if (log.isDebugEnabled()) {
74: if (_prefix != null && _suffix != null) {
75: log.debug("find: resourceName='" + resourceName
76: + "' as '" + name + "'");
77: } else {
78: log.debug("find: resourceName = '" + name + "'");
79: }
80: }
81:
82: final String newResourcName = name;
83: return new ResourceNameIterator() {
84: private String resource = ManagedProperties
85: .getProperty(newResourcName);
86:
87: public boolean hasNext() {
88: return resource != null;
89: }
90:
91: public String nextResourceName() {
92: String element = resource;
93: resource = null;
94: return element;
95: }
96: };
97: }
98: }
|