/*
    PCS - A Framework For Java Web Applications
    Copyright (C) 2002 Patrick Carl, patrick.carl@web.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 
    $Id: DefaultFactory.java,v 1.2 2003/01/08 02:32:22 pcs_org Exp $
 
 */

package de.spieleck.pcs.factory;

import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
;
/**
 * The DefaultFactory implements the Factory Interface. It can be configured
 * via a Properties-Object, either by specifying a file or by delievering
 * a Properties Object.
 * @author  Patrick Carl
 */
public class DefaultFactory implements Factory {
    
    protected String configFile;
    private Properties properties;
    
    /** Creates a new instance of DefaultFactory */
    public DefaultFactory() {
        properties = new Properties();
    }
    
    /**
     * creates a new instance of DefaultFactory using the given file for
     * configuration
     */
    public DefaultFactory(String configFile) throws IllegalArgumentException,
    FileNotFoundException{
        this();
        setConfigFile(configFile);
    }
    
    /** Getter for property configFile.
     * @return Value of property configFile.
     */
    public String getConfigFile() {
        return configFile;
    }
    
    /** factory method for creating objects. The class of the Objects is 
     * look upped in the configuration file.
     */

    public Object getNewObject(String key) throws FactoryException, 
    NoSuchKeyException {
        if(getProperties() == null){
            String cfg = getConfigFile();
            if(cfg == null || cfg.length() == 0)
                throw new FactoryException("Factory not configured yet");
            try{
                reloadConfiguration();
            } catch(Exception e){
                throw new FactoryException(e.getMessage());
            }
        }
        if(getProperties() == null){
            throw new FactoryException("Factory not configured yet");
        }
        String className = properties.getProperty(key);
        if(className == null)
            throw new NoSuchKeyException("No matching key could be found");
        try{
            return Class.forName(className).newInstance();
        } catch (Exception e){
            throw new FactoryException(e.getMessage());
        }
    }
    
    /** resets the configuration
     *
     */
    public void resetConfig() {
        try{
            reloadConfiguration();
        } catch(IOException ie){
            System.err.println("Could not reset config of " + this);
            ie.printStackTrace();
        }
    }
    
    /** Setter for property configFile.
     * @param configFile New value of property configFile.
     */
    public void setConfigFile(String configFile) throws IllegalArgumentException,
    FileNotFoundException{
        this.configFile = configFile;
        try{
            reloadConfiguration();
        } catch(IOException i){
            throw new IllegalArgumentException("A problem occured during "
            + "reading config file " + configFile );
        }
    }
    
    /**
     * reloads the configuration from the configuration file
     */
    private void reloadConfiguration() throws IOException, FileNotFoundException{
        synchronized(configFile){
            properties.load(new FileInputStream(getConfigFile()));
        }
    }
    
    /** Getter for property properties.
     * @return Value of property properties.
     */
    public Properties getProperties() {
        return properties;
    }
    
    /** Setter for property properties.
     * @param properties New value of property properties.
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}