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 */
017package org.apache.commons.discovery.resource.names;
018
019import java.util.Dictionary;
020import java.util.Hashtable;
021
022import org.apache.commons.discovery.ResourceNameDiscover;
023import org.apache.commons.discovery.ResourceNameIterator;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027/**
028 * Recover resources from a Dictionary.  This covers Properties as well,
029 * since <code>Properties extends Hashtable extends Dictionary</code>.
030 *
031 * The recovered value is expected to be either a <code>String</code>
032 * or a <code>String[]</code>.
033 */
034public class DiscoverNamesInDictionary extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
035
036    private static Log log = LogFactory.getLog(DiscoverNamesInDictionary.class);
037
038    /**
039     * Sets the {@code Log} for this class.
040     *
041     * @param _log This class {@code Log}
042     * @deprecated This method is not thread-safe
043     */
044    @Deprecated
045    public static void setLog(Log _log) {
046        log = _log;
047    }
048
049    private Dictionary<String, String[]> dictionary;
050
051    /**
052     * Construct a new resource discoverer with an empty Dictionary.
053     */
054    public DiscoverNamesInDictionary() {
055        setDictionary(new Hashtable<String, String[]>());
056    }
057
058    /**
059     * Construct a new resource discoverer with the given Dictionary.
060     *
061     * @param dictionary The initial Dictionary
062     */
063    public DiscoverNamesInDictionary(Dictionary<String, String[]> dictionary) {
064        setDictionary(dictionary);
065    }
066
067    /**
068     * Returns the current Dictionary for names mapping.
069     *
070     * @return The current Dictionary for names mapping
071     */
072    protected Dictionary<String, String[]> getDictionary() {
073        return dictionary;
074    }
075
076    /**
077     * Specify the Dictionary for names mapping.
078     *
079     * @param table The Dictionary for names mapping
080     */
081    public void setDictionary(Dictionary<String, String[]> table) {
082        this.dictionary = table;
083    }
084
085    /**
086     * Add a resource name to a single name mapping.
087     *
088     * @param resourceName The resource name
089     * @param resource The target name
090     */
091    public void addResource(String resourceName, String resource) {
092        addResource(resourceName, new String[]{ resource });
093    }
094
095    /**
096     * Add a resource name to multiple names mapping.
097     *
098     * @param resourceName The resource name
099     * @param resources The target names
100     */
101    public void addResource(String resourceName, String[] resources) {
102        dictionary.put(resourceName, resources);
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public ResourceNameIterator findResourceNames(final String resourceName) {
110        if (log.isDebugEnabled()) {
111            log.debug("find: resourceName='" + resourceName + "'");
112        }
113
114        final String[] resources = dictionary.get(resourceName);
115
116        return new ResourceNameIterator() {
117            private int idx = 0;
118
119            public boolean hasNext() {
120                if (resources != null) {
121                    while (idx < resources.length  &&  resources[idx] == null) {
122                        idx++;
123                    }
124                    return idx < resources.length;
125                }
126                return false;
127            }
128
129            public String nextResourceName() {
130                return hasNext() ? resources[idx++] : null;
131            }
132        };
133    }
134
135}