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