001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.discovery.resource.names;
018
019 import org.apache.commons.discovery.ResourceNameDiscover;
020 import org.apache.commons.discovery.ResourceNameIterator;
021 import org.apache.commons.discovery.log.DiscoveryLogFactory;
022 import org.apache.commons.discovery.tools.ManagedProperties;
023 import org.apache.commons.logging.Log;
024
025
026 /**
027 * Recover resource name from Managed Properties.
028 * @see org.apache.commons.discovery.tools.ManagedProperties
029 *
030 * @author Richard A. Sitze
031 */
032 public class DiscoverNamesInManagedProperties
033 extends ResourceNameDiscoverImpl
034 implements ResourceNameDiscover
035 {
036 private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInManagedProperties.class);
037 public static void setLog(Log _log) {
038 log = _log;
039 }
040
041
042 private final String _prefix;
043 private final String _suffix;
044
045 /** Construct a new resource discoverer
046 */
047 public DiscoverNamesInManagedProperties() {
048 _prefix = null;
049 _suffix = null;
050 }
051
052 /** Construct a new resource discoverer
053 */
054 public DiscoverNamesInManagedProperties(String prefix, String suffix) {
055 _prefix = prefix;
056 _suffix = suffix;
057 }
058
059 /**
060 * @return Enumeration of ResourceInfo
061 */
062 public ResourceNameIterator findResourceNames(final String resourceName) {
063 String name;
064 if (_prefix != null && _prefix.length() > 0) {
065 name = _prefix + resourceName;
066 } else {
067 name = resourceName;
068 }
069
070 if (_suffix != null && _suffix.length() > 0) {
071 name = name + _suffix;
072 }
073
074 if (log.isDebugEnabled()) {
075 if (_prefix != null && _suffix != null) {
076 log.debug("find: resourceName='" + resourceName + "' as '" + name + "'");
077 } else {
078 log.debug("find: resourceName = '" + name + "'");
079 }
080 }
081
082 final String newResourcName = name;
083 return new ResourceNameIterator() {
084 private String resource = ManagedProperties.getProperty(newResourcName);
085 public boolean hasNext() {
086 return resource != null;
087 }
088
089 public String nextResourceName() {
090 String element = resource;
091 resource = null;
092 return element;
093 }
094 };
095 }
096 }