/* Generated by Together */

package cobalt.deployment.descriptor.dist;

import java.util.Vector;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
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.Websphere30EjbBundleNode;
import cobalt.deployment.util.CommonUtil;
import cobalt.deployment.util.Websphere30XmlUtil;
import cobalt.deployment.util.EnvironmentManager;

public class Websphere30ApplicationDescriptor implements BasicDescriptorIF {
    public Websphere30ApplicationDescriptor() {
        this(null);
    }
    public Websphere30ApplicationDescriptor(ApplicationDescriptor appDesc) {
        if ( appDesc != null )
            setDescriptor(appDesc);
    }
    public void setDescriptor(ApplicationDescriptor appDesc) {
        this.descriptor = appDesc;
        ejbBundles = new Vector();
        //deployableDirectory = CommonUtil.getTempDir();
        //deployablePrefix = "DEPLOYABLE_";
        organize();
    }

    public ApplicationDescriptor getDescriptor(){ return descriptor; }

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

    public Vector getEjbBundles(){ return ejbBundles; }

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

    void organize() {
        //Iterator iterator = descriptor.getEjbBundleFileNames().iterator();
        Iterator iterator = descriptor.getEjbBundleDescriptors().values().iterator();
        Websphere30EjbBundleDescriptor ejbBundle = null;
        while ( iterator.hasNext() ) {
            ejbBundle = new Websphere30EjbBundleDescriptor((EjbBundleDescriptor)iterator.next());
            ejbBundle.setAppDescriptor(this);
            ejbBundles.addElement(ejbBundle);
        }
        /*
        Object bundle = descriptor.getEjbBundleDescriptorValues().elementAt(0);
        if (bundle != null) {
            ejbBundles = new Websphere30EjbBundleDescriptor((EjbBundleDescriptor)bundle/ *getAllEjbDescriptors()* /);
            ejbBundles.setAppDescriptor(this);
        }
        */
    }

    public String getDeployableDirectory(){
            return deployableDirectory;
        }

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

    public String getDeployablePrefix(){
            return deployablePrefix;
        }

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

    public String getDeployableFileName() {
        if (type == CommonUtil.EAR || type == CommonUtil.EJB_JAR_11 || type == CommonUtil.EJB_JAR_20)
			return ((Websphere30EjbBundleDescriptor)ejbBundles.elementAt(0)).getDeployableFileName();
        else
            return deployableDirectory + deployablePrefix + CommonUtil.getFileName(descriptor.getFileName());
    }

    public ElementNode getDocument()
    {
        return null;
    }

    public void archive(/*ElementNode doc*/) {
        /*
        if (type == CommonUtil.EAR) {
            //ejbBundles.archive(Weblogic60XmlUtil.getNewWeblogicEjbBundleNode(ejbBundles));
            archiveEjbComponents(doc);
			CommonUtil.copyFile(descriptor.getFileName(), getDeployableFileName());
	        archiveToJar(/ *(Weblogic60ApplicationNode)doc, null, * /getDeployableFileName());
        }
    	*/
        if (type == CommonUtil.EAR || type == CommonUtil.EJB_JAR_11 || type == CommonUtil.EJB_JAR_20) {
            //ejbBundles.archive(Websphere30XmlUtil.getNewWeblogicEjbBundleNode(ejbBundles));
            archiveEjbComponents(/*doc*/);
        }
    }
    void archiveEjbComponents(/*ElementNode doc*/) {
        Websphere30EjbBundleDescriptor ejbBundle = null;
        for ( int i=0 ; i<ejbBundles.size() ; i++) {
            ejbBundle =  (Websphere30EjbBundleDescriptor)ejbBundles.elementAt(i);
            ejbBundle.archive(/*Websphere30XmlUtil.getNewWebsphereEjbBundleNode(ejbBundle/*, EnvironmentManager.getSelectedServer()* /)*/);
        }
    }

    Websphere30EjbBundleDescriptor getEjbBundleDescriptor(String fileName){
        Websphere30EjbBundleDescriptor ejbBundle;
        for ( int i=0 ; i<ejbBundles.size() ; i++) {
            ejbBundle = (Websphere30EjbBundleDescriptor)ejbBundles.elementAt(i);
            if ( ejbBundle.getDeployableFileName().endsWith(fileName) )
              return ejbBundle;
        }
        return null;
    }
     public void archiveToJar(/*Weblogic60ApplicationNode doc, String xmlFile, */String fileName) {
        File jarFile = null, tempJarFile = null;
        JarOutputStream jos = null;
        FileInputStream jfis = null;
		JarInputStream jis = null;
        //String bundleName = ejbBundles.getDeployableFileName();
        //String deployablePrefix = getDeployableDirectory() + CommonUtil.getDirSep() + getDeployablePrefix();
		JarEntry e = null;
        try {
            jarFile = new File(fileName);
            tempJarFile = CommonUtil.createTempFile(fileName);
            jfis = new FileInputStream(jarFile);
            jis = new JarInputStream(jfis);
            jos = ArchiveEntry.getJarOutput(tempJarFile);
            Websphere30EjbBundleDescriptor ejbBundle = null;
            while ((e=jis.getNextJarEntry()) != null) {
                ejbBundle = getEjbBundleDescriptor(e.getName());
                if ( ejbBundle != null) {
                    //-ejbBundle.archive(Websphere30XmlUtil.getNewWeblogicEjbBundleNode(ejbBundle, EnvironmentManager.getSelectedServer()));
                    ArchiveEntry.putJarEntry(jos, e.getName()/*CommonUtil.getFileName(bundleName)*/, ejbBundle.getDeployableFileName());
                }
                else {
					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() {
        /*
        Object[] ret = new Object[ejbBundles.size()];
        for ( int i=0 ; i<ret.length ; i++) {
            ret[i] = ((Websphere30EjbBundleDescriptor)ejbBundles.elementAt(i)).getName();
        }
        return ret;
        */
        return getDescriptor().getAllEjbDescriptorNames().toArray();
    }
    public String getName() {
      return descriptor.getName();
    }
/*
    public void writeDD(String fileName)
    {
        Websphere30EjbBundleNode node = Websphere30XmlUtil.getNewWebsphereEjbBundleNode((Websphere30EjbBundleDescriptor)getEjbBundles().elementAt(0));
        node.writeNodeToFile(new File(fileName));
    }
*/
    private ApplicationDescriptor descriptor;
    //private Websphere30EjbBundleDescriptor ejbBundles;
    private Vector ejbBundles;
    private String deployableDirectory = EnvironmentManager.getDeployableDirectory();
    private String deployablePrefix = "DEPLOYABLE_";
    private int type = CommonUtil.EJB_JAR_11;
}
