/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.deployment.application;

import org.jfox.deployment.ejb.EJBDeployer;
import org.jfox.deployment.ejb.NestedEJBModule;
import org.jfox.deployment.web.WebDeployer;
import org.jfox.deployment.web.WebModule;
import org.jfox.framework.deploy.DeployException;
import org.jfox.framework.deploy.Deployable;
import org.jfox.framework.deploy.Deployer;
import org.jfox.system.AbstractComponent;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EARDeployer extends AbstractComponent implements Deployer {
//  private static EARDeployer instance = new EARDeployer();
//  private static EJBDeployer ejbDeployer = EJBDeployer.getInstance();
//  private static WebDeployer webDeployer = WebDeployer.getInstance();

    private EJBDeployer ejbDeployer = null;
    private WebDeployer webDeployer = null;

    public EARDeployer(EJBDeployer ejbDeployer, WebDeployer webDeployer) {
        this.ejbDeployer = ejbDeployer;
        this.webDeployer = webDeployer;
    }

/*
  public static EARDeployer getInstance(){
    return instance;
  }
*/

    public void deploy(Deployable comp) throws DeployException {
        if(comp instanceof EARModule) {
            try {
                EARModule earModule = (EARModule) comp;

                NestedEJBModule[] ejbModules = earModule.getNestedEJBModules();
                for(int i = 0; i < ejbModules.length; i++) {
                    ejbModules[i].deploy(ejbDeployer);
                }

                WebModule[] webModules = earModule.getNestedWebModules();
                for(int i = 0; i < webModules.length; i++) {
                    webModules[i].deploy(webDeployer);
                }

            }
            catch(Exception e) {
                throw new DeployException("deploy Module " + comp + " error", e);
            }
        }
        else {
            throw new DeployException("can not deploy " + comp);
        }
    }

    //TODO: implements EARDeploy.undeploy
    public void undeploy(Deployable comp) throws DeployException {

    }

    protected void doInit() throws Exception {
    }

    protected void doDestroy() throws Exception {
    }

    public EJBDeployer getEjbDeployer() {
        return ejbDeployer;
    }

    public WebDeployer getWebDeployer() {
        return webDeployer;
    }

    public static void main(String[] args) {

    }
}

