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    
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.discovery.tools.ManagedProperties;
025    import org.apache.commons.logging.Log;
026    
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     * @author Richard A. Sitze
039     */
040    public class DiscoverNamesInAlternateManagedProperties
041        extends ResourceNameDiscoverImpl
042        implements ResourceNameDiscover
043    {
044        private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInAlternateManagedProperties.class);
045        public static void setLog(Log _log) {
046            log = _log;
047        }
048    
049        HashMap mapping = new HashMap();
050        
051        /** Construct a new resource discoverer
052         */
053        public DiscoverNamesInAlternateManagedProperties() {
054        }
055        
056        /**
057         */
058        public void addClassToPropertyNameMapping(String className, String propertyName) {
059            mapping.put(className, propertyName);
060        }
061    
062        /**
063         * @return Enumeration of ResourceInfo
064         */
065        public ResourceNameIterator findResourceNames(final String resourceName) {
066            final String mappedName = (String)mapping.get(resourceName);
067    
068            if (log.isDebugEnabled()) {
069                if (mappedName == null) {
070                    log.debug("find: resourceName='" + resourceName + "', no mapping");
071                } else {
072                    log.debug("find: resourceName='" + resourceName + "', lookup property '" + mappedName + "'");
073                }
074            }
075    
076            return new ResourceNameIterator() {
077                private String resource =
078                    (mappedName == null) ? null : ManagedProperties.getProperty(mappedName);
079                
080                public boolean hasNext() {
081                    return resource != null;
082                }
083                
084                public String nextResourceName() {
085                    String element = resource;
086                    resource = null;
087                    return element;
088                }
089            };
090        }
091    }