/* Generated by Together */

package cobalt.deployment.descriptor.dist;

import java.util.Vector;
import java.util.HashMap;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.jar.*;

import cobalt.deployment.descriptor.ApplicationDescriptor;
import cobalt.deployment.descriptor.EjbBundleDescriptor;
import cobalt.deployment.descriptor.ArchiveEntry;
import cobalt.deployment.descriptor.xml.ElementNode;
import cobalt.deployment.descriptor.xml.J2eeApplicationNode;
import cobalt.deployment.util.CommonUtil;
import cobalt.deployment.util.EnvironmentManager;
import cobalt.deployment.util.J2eeXmlUtil;

/**
 * Title:        J2EE Enterprise application descriptor
 * Description:  Represents J2EE-specific enterprise application descriptor for standard enterprise application descriptor
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author Yeon Jun Choi
 * @version 1.0
 */

public class J2eeApplicationDescriptor implements BasicDescriptorIF {
    public J2eeApplicationDescriptor() {
        this(null);
    }

    public J2eeApplicationDescriptor(ApplicationDescriptor appDesc) {
        setDeployableDirectory(EnvironmentManager.getDeployableDirectory());//CommonUtil.getTempDir();
        deployablePrefix = "DEPLOYABLE_";
        if ( appDesc != null )
        	setDescriptor(appDesc);
    }
    public void setDescriptor(ApplicationDescriptor appDesc) {
        this.descriptor = appDesc;
        roles = new HashMap();
        //ejbBundles = new Vector();
        organize();
    }

    public String getName() {
        return descriptor.getName();
    }

    public void addRole(J2eeRoleMap role) {
        roles.put(role.getName(), role);
    }

    public void removeRole(J2eeRoleMap role) {
        roles.remove(role.getName());
    }

    public void removeRole(String roleName) {
        roles.remove(roleName);
    }

    public ApplicationDescriptor getDescriptor(){ return descriptor; }

    //public void setDescriptor(ApplicationDescriptor descriptor){ this.descriptor = descriptor; }

    public J2eeEjbBundleDescriptor getEjbBundles(){ return ejbBundles; }

    public void setEjbBundles(J2eeEjbBundleDescriptor ejbBundles){ this.ejbBundles = ejbBundles; }

    public HashMap getRoles(){ return roles; }

    public J2eeRoleMap getRole(String name) {
        return (J2eeRoleMap)roles.get(name);
    }

    public boolean containsRole(String name) {
        return roles.containsKey(name);
    }

    public void setRoles(HashMap roles){ this.roles = roles; }

	public Set getApplicationRoles() {
        return descriptor.getSecurityRoles().keySet();
    }

    public boolean isReturnClientJar() {
        return returnClientJar;
    }

    public void setReturnClientJar(boolean returnClientJar){
      this.returnClientJar = returnClientJar;
    }

    public String getReturnClientJarFileName() {
        return returnClientJarFileName;
    }

    public void setReturnClientJarFileName(String fileName){
      returnClientJarFileName = fileName;
    }

    void organize() {
        ejbBundles = new J2eeEjbBundleDescriptor(descriptor.getAllEjbDescriptors());
    }

    public String getDeployableDirectory(){
            return deployableDirectory;
        }

    public void setDeployableDirectory(String deployableDirectory){
            this.deployableDirectory = deployableDirectory;
            if (!(this.deployableDirectory.endsWith(CommonUtil.getDirSep()))) {
                this.deployableDirectory += CommonUtil.getDirSep();
            }
        }

    public String getDeployablePrefix(){
            return deployablePrefix;
        }

    public void setDeployablePrefix(String deployablePrefix){
            this.deployablePrefix = deployablePrefix;
        }

    public String getDeployableFileName() {
        return deployableDirectory + deployablePrefix + CommonUtil.getFileName(descriptor.getFileName());
    }

    public ElementNode getDocument()
    {
        //if ( doc == null )
        //{
        //    doc = J2eeXmlUtil.getJ2eeApplicationNode(this);
        //}
        //return doc;
        return J2eeXmlUtil.getJ2eeApplicationNode(this);
    }

    public void archive(/*ElementNode doc*/) {
		CommonUtil.copyFile(descriptor.getFileName(), getDeployableFileName());
        archiveToJar((J2eeApplicationNode)getDocument(), "sun-j2ee-ri.xml", getDeployableFileName());
    }

    public void archiveToJar(J2eeApplicationNode doc, String xmlFile, String fileName) {
        File jarFile = null, tempJarFile = null;
        JarOutputStream jos = null;
        FileInputStream jfis = null;
		JarInputStream jis = null;
		JarEntry e = null;
        try {
            jarFile = new File(fileName);
            tempJarFile = CommonUtil.createTempFile(fileName);
            jfis = new FileInputStream(jarFile);
            jis = new JarInputStream(jfis);
            jos = ArchiveEntry.getJarOutput(tempJarFile);
			while ((e=jis.getNextJarEntry()) != null) {
				jos.putNextEntry(e);
				byte[] data = new byte[1024];
				int len = 0;
				while ((len=jis.read(data)) >= 0)
					jos.write(data, 0, len);
			}
            ArchiveEntry.writeDD(jos, doc, xmlFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
	        try { jis.close(); } catch (Exception e2) { }
	        try { jfis.close(); } catch (Exception e2) { }
	        try { jos.flush(); jos.close(); } catch (Exception e2) { }
        }
        if (jarFile.exists())
            jarFile.delete();
        tempJarFile.renameTo(jarFile);
    }

    public int getType(){
            return type;
        }

    public void setType(int type){
            this.type = type;
        }
    public Object[] getComponents() {
        //return null;
        if ( !returnClientJar )
            return null;
        return new Object[]{ returnClientJarFileName };
    }

    private ApplicationDescriptor descriptor;
    private J2eeEjbBundleDescriptor ejbBundles;
    private HashMap roles;
    private String deployableDirectory;
    private String deployablePrefix;
    private boolean returnClientJar = false;
    private String returnClientJarFileName;
    private int type;
    //private ElementNode doc;
}
