/* 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.ejb;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import org.jfox.deployment.J2EEClassLoader;
import org.jfox.deployment.J2EEModule;
import org.jfox.ejb.meta.EJBBundle;
import org.jfox.ejb.meta.XmlUtil;
import org.jfox.framework.deploy.DeployException;
import org.jfox.framework.deploy.Deployer;

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

public class EJBModule extends J2EEModule {
    private String baseDeployDirectory = ".." + File.separator + "temp" + File.separator + "apps" + File.separator;
    private File jarFile;

    private J2EEClassLoader loader = null;

    private EJBBundle ejbBundle;

    public EJBModule(File jarFile) throws DeployException {
        this.jarFile = jarFile;
        init();
    }

    private void init() throws DeployException {
        try {
            this.loader = new J2EEClassLoader(new URL[]{jarFile.toURL()}, this.getClass().getClassLoader());
            ejbBundle = new EJBBundle(XmlUtil.getDocument(getDescriptorURL()));
        }
        catch(Exception e) {
            throw new DeployException(e);
        }

    }

    public URL getDescriptorURL() {
        return loader.findResource("META-INF/ejb-jar.xml");
    }

    public File getModuleFile() {
        return jarFile;
    }

    public URLClassLoader getClassLoader() {
        return loader;
    }

    public void deploy(Deployer deployer) throws DeployException {
        deployer.deploy(this);
    }

    public File getTempDeployDirectory() {
        File dir = new File(baseDeployDirectory);
        if(!dir.exists()) {
            dir.mkdirs();
        }
        return dir;
    }

    public EJBBundle getEJBBundle() {
        return ejbBundle;
    }

    public static void main(String[] args) {

    }
}

