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