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 java.util.HashMap;
020 import java.util.Map;
021
022 import org.apache.commons.discovery.ResourceNameDiscover;
023 import org.apache.commons.discovery.ResourceNameIterator;
024 import org.apache.commons.discovery.tools.ManagedProperties;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027
028 /**
029 * Recover resource name from Managed Properties,
030 * using OLD property names.
031 *
032 * This class maintains a mapping between old names and
033 * (new) the class names they represent. The discovery
034 * mechanism uses the class names as property names.
035 *
036 * @see org.apache.commons.discovery.tools.ManagedProperties
037 */
038 public class DiscoverNamesInAlternateManagedProperties
039 extends ResourceNameDiscoverImpl
040 implements ResourceNameDiscover {
041
042 private static Log log = LogFactory.getLog(DiscoverNamesInAlternateManagedProperties.class);
043
044 /**
045 * Sets the {@code Log} for this class.
046 *
047 * @param _log This class {@code Log}
048 * @deprecated This method is not thread-safe
049 */
050 @Deprecated
051 public static void setLog(Log _log) {
052 log = _log;
053 }
054
055 private final Map<String, String> mapping = new HashMap<String, String>();
056
057 /**
058 * Construct a new resource discoverer.
059 */
060 public DiscoverNamesInAlternateManagedProperties() {
061 }
062
063 /**
064 * Add a class name/property name mapping.
065 *
066 * @param className The class name
067 * @param propertyName The property name
068 */
069 public void addClassToPropertyNameMapping(String className, String propertyName) {
070 mapping.put(className, propertyName);
071 }
072
073 /**
074 * {@inheritDoc}
075 */
076 @Override
077 public ResourceNameIterator findResourceNames(final String resourceName) {
078 final String mappedName = mapping.get(resourceName);
079
080 if (log.isDebugEnabled()) {
081 if (mappedName == null) {
082 log.debug("find: resourceName='" + resourceName + "', no mapping");
083 } else {
084 log.debug("find: resourceName='" + resourceName + "', lookup property '" + mappedName + "'");
085 }
086 }
087
088 return new ResourceNameIterator() {
089 private String resource =
090 (mappedName == null) ? null : ManagedProperties.getProperty(mappedName);
091
092 public boolean hasNext() {
093 return resource != null;
094 }
095
096 public String nextResourceName() {
097 String element = resource;
098 resource = null;
099 return element;
100 }
101 };
102 }
103
104 }