/*
 * $Id: PropertiesComponent.java,v 1.2 2003/09/02 04:10:45 rago2483 Exp $
 */
package com.diginsite.services.presentation.cocoon.components;

import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;

import java.util.Properties;

/**
 * This class allows a set of values to be configured as a Properties object
 *
 * @author Ralph Geors
 * @version $Revision: 1.2 $
 * @created 08-13-2003
 */

public final class PropertiesComponent extends AbstractLogEnabled
    implements Configurable, Component, ThreadSafe
{
    /**
     * Configure the instance. Called by Cocoon during initialization
     * @prarm conf the Configuration data
     */
    public void configure(final Configuration conf)
        throws ConfigurationException
    {
        this.properties = new Properties();

        if (conf != null)
        {
            Configuration config[] = conf.getChildren("property");

            for (int i=0; i < config.length; ++i)
            {
                String name = config[i].getAttribute("name");
                String value = config[i].getAttribute("value");
                if (name == null || value == null)
                {
                    if (name == null)
                    {
                        getLogger().error("No property name specified - property ignored");
                    }
                    else
                    {
                        getLogger().error("No property value speicified - property " +
                                          name + " ignored");
                    }
                }
                else
                {
                    getLogger().debug("Property name: " + name + " value: " + value);
                    this.properties.put(name, value);
                }
            }
        }
    }


    /**
     * Return the Properties managed by this component
     * @return the properties configured into this component
     */
    public Properties getProperties()
    {
        return properties;
    }

    private Properties properties = null;
}

