package com.gwvas.manager;

import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.logger.Loggable;
import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.ConfigurationException;

import com.gwvas.core.*;

import org.w3c.dom.Document;

public class DBCoreManager extends AbstractLoggable implements
    Manager, Composable, Configurable, Initializable, Disposable
{
    private boolean initialized = false;
    private boolean disposed = false;
    private ComponentManager manager = null;
    private String dbResource = null;

    public final void configure(Configuration conf)
        throws ConfigurationException
    {
		System.out.println("Configure : check in");
        if (initialized || disposed) {
            throw new IllegalStateException ("Illegal call");
        }

        if (null == this.dbResource) {
		    this.dbResource = conf.getChild("boolbool").getValue();
            getLogger().error("Using database pool: " + this.dbResource);
        }
        System.out.println("Configure : check out");
    }


    public final void compose(ComponentManager cmanager)
    throws ComponentException {
		System.out.println("Compose : check in");
        if (initialized || disposed) {
            throw new IllegalStateException ("Illegal call");
        }

        if (null == this.manager) {
            this.manager = cmanager;
        }
        System.out.println("Compose : check out");
    }

    public final void initialize() throws Exception {
		System.out.println("Init : check in");
	     if (null == this.manager) throw new IllegalStateException("Not Composed");
        if (null == this.dbResource)
		     throw new IllegalStateException("Not Configured");

		 if (disposed) throw new IllegalStateException("Already disposed");

        this.initialized = true;
        System.out.println("Init : check out");
    }

    public final void dispose() {
		System.out.println("Dispose : check in");
        this.disposed = true;
        System.out.println("Dispose : check out");
    }

    // ---------------------------------------------------------------------------

}
