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.Hashtable;
020
021 import org.apache.commons.discovery.ResourceNameDiscover;
022 import org.apache.commons.discovery.ResourceNameIterator;
023 import org.apache.commons.discovery.log.DiscoveryLogFactory;
024 import org.apache.commons.logging.Log;
025
026
027 /**
028 * Recover resource name from Managed Properties,
029 * using OLD property names.
030 *
031 * This class maintains a mapping between old names and
032 * (new) the class names they represent. The discovery
033 * mechanism uses the class names as property names.
034 *
035 * @see org.apache.commons.discovery.tools.ManagedProperties
036 *
037 * @author Richard A. Sitze
038 */
039 public class DiscoverMappedNames
040 extends ResourceNameDiscoverImpl
041 implements ResourceNameDiscover
042 {
043 private static Log log = DiscoveryLogFactory.newLog(DiscoverMappedNames.class);
044 public static void setLog(Log _log) {
045 log = _log;
046 }
047
048 private Hashtable mapping = new Hashtable(); // String name ==> String[] newNames
049
050 /** Construct a new resource discoverer
051 */
052 public DiscoverMappedNames() {
053 }
054
055 public void map(String fromName, String toName) {
056 mapping.put(fromName, toName);
057 }
058
059 public void map(String fromName, String [] toNames) {
060 mapping.put(fromName, toNames);
061 }
062
063 /**
064 * @return Enumeration of ResourceInfo
065 */
066 public ResourceNameIterator findResourceNames(final String resourceName) {
067 if (log.isDebugEnabled()) {
068 log.debug("find: resourceName='" + resourceName + "', mapping to constants");
069 }
070
071 final Object obj = mapping.get(resourceName);
072
073 final String[] names;
074 if (obj instanceof String) {
075 names = new String[] { (String)obj };
076 } else if (obj instanceof String[]) {
077 names = (String[])obj;
078 } else {
079 names = null;
080 }
081
082 return new ResourceNameIterator() {
083
084 private int idx = 0;
085
086 public boolean hasNext() {
087 if (names != null) {
088 while (idx < names.length && names[idx] == null) {
089 idx++;
090 }
091 return idx < names.length;
092 }
093 return false;
094 }
095
096 public String nextResourceName() {
097 return hasNext() ? names[idx++] : null;
098 }
099 };
100 }
101 }