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.tools;
018    
019    import java.util.Properties;
020    
021    import org.apache.commons.discovery.resource.ClassLoaders;
022    
023    
024    /**
025     * Holder for a default class.
026     * 
027     * Class may be specified by name (String) or class (Class).
028     * Using the holder complicates the users job, but minimized # of API's.
029     * 
030     * @author Richard A. Sitze
031     */
032    public class PropertiesHolder {
033        private Properties   properties;
034        private final String propertiesFileName;
035        
036        public PropertiesHolder(Properties properties) {
037            this.properties = properties;
038            this.propertiesFileName = null;
039        }
040        
041        public PropertiesHolder(String propertiesFileName) {
042            this.properties = null;
043            this.propertiesFileName = propertiesFileName;
044        }
045    
046        /**
047         * @param spi Optional SPI (may be null).
048         *            If provided, an attempt is made to load the
049         *            property file as-per Class.getResource().
050         * 
051         * @param loaders Used only if properties need to be loaded.
052         * 
053         * @return Properties.  Load the properties if necessary.
054         */
055        public Properties getProperties(SPInterface spi, ClassLoaders loaders) {
056            if (properties == null) {
057                properties = ResourceUtils.loadProperties(spi.getSPClass(), getPropertiesFileName(), loaders);
058            }
059            return properties;
060        }
061    
062        public String getPropertiesFileName() {
063            return propertiesFileName;
064        }
065    }