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