/*
    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: ConfigAdmin.java,v 1.1.1.1 2002/12/27 23:49:43 pcs_org Exp $
 
 */

package de.spieleck.pcs.conf;

import java.util.Vector;
import java.util.Iterator;

/**
 * This class manages ConfigUsers and acts as a Observer to ConfigUsers. It 
 * is implemented as a Singleton.<br>
 * By calling resetConfig on every registered ConfigUser resetConfig is 
 * called.
 * @author Patrick Carl
 */
public class ConfigAdmin {
    
    private static Vector surveyors;
    private static ConfigAdmin me;
    
    /** Creates a new instance of ConfigAdmin */
    protected ConfigAdmin() {
        surveyors = new Vector();
    }
    
    /**
     * returns an instance of ConfigAdmin, used for implementing the Singleton pattern
     * @return an instance of ConfigAdmin
     */
    public static ConfigAdmin getInstance() {
        if(me == null)
            me = new ConfigAdmin();
        return me;
    }
    
    /**
     * registers a ConfigUser to the ConfigAdmin
     * @param user the ConfigUser to register
     */
    public void addConfigUser(ConfigUser user) {
        if(!surveyors.contains(user))
            surveyors.add(user);
    }
    
    /**
     * unregisters a ConfigUser from the ConfigAdmin
     * @param user the ConfigUser to unregister
     */
    public void removeConfigUser(ConfigUser user) {
        surveyors.remove(user);
    }
    
    
    /**
     * resets the configuration of all registered ConfigUsers
     */
    public int resetConfig() {
        Iterator it = surveyors.iterator();
        Object o;
        while(it.hasNext()) {
            o = it.next();
            if(o != null)
                ((ConfigUser)o).resetConfig();
        }
        return surveyors.size();
    }
    
    public String[] getConfigUserNames(){
        Iterator it = surveyors.iterator();
        String[] ret = new String[surveyors.size()];
        int i = 0;
        while(it.hasNext()){
            ret[i++] = it.next().getClass().getName();
        }
        return ret;
    }
}