/* Generated by Together */

package cobalt.deployment.descriptor.dist;

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

import cobalt.deployment.descriptor.EjbBundleDescriptor;
import cobalt.deployment.descriptor.ArchiveEntry;
import cobalt.deployment.descriptor.EjbDescriptor;
import cobalt.deployment.descriptor.SessionEjbDescriptor;
import cobalt.deployment.descriptor.EntityEjbDescriptor;
import cobalt.deployment.descriptor.EjbReferenceDescriptor;
import cobalt.deployment.descriptor.ResourceReferenceDescriptor;
import cobalt.deployment.descriptor.xml.ElementNode;
import cobalt.deployment.descriptor.xml.Weblogic51EjbBundleNode;
import cobalt.deployment.util.CommonUtil;
import cobalt.deployment.util.Weblogic51XmlUtil;

/**
 * Title:        WebLogic 5.1 ejb components descriptor
 * Description:  Represents WebLogic 5.1-specific packaging module of ejb components for standard ejb components
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author Yeon Jun Choi
 * @version 1.0
 */

public class Weblogic51EjbBundleDescriptor {
    public Weblogic51EjbBundleDescriptor(EjbBundleDescriptor bundles) {
        this.descriptor = bundles;
        ejbs = new Vector();
        roles = new HashMap();
        //cmp = new Weblogic51ContainerManaged();
        organize();
    }
    public EjbBundleDescriptor getDescriptor(){ return descriptor; }

    public void setDescriptor(EjbBundleDescriptor descriptor){
        this.descriptor = descriptor;
        organize();
    }

    public Vector getEjbs(){ return ejbs; }

    public void setEjbs(Vector ejbs){ this.ejbs = ejbs; }

    void organize() {
        ejbs.clear();
        roles.clear();
        Vector ejbvalues = descriptor.getEjbDescriptorValues();
        for ( int i=0 ; i<ejbvalues.size()  ; i++) {
            EjbDescriptor ejb = (EjbDescriptor)ejbvalues.elementAt(i);
            if ( ejb.getEjbType().equals("Session") ) {
                if ( ((SessionEjbDescriptor)ejb).getSessionType().equals(SessionEjbDescriptor.STATELESS) )
                	ejbs.addElement(new Weblogic51StatelessEjbDescriptor(ejb));
                else
                    ejbs.addElement(new Weblogic51StatefulEjbDescriptor(ejb));
            }
            else {
                Weblogic51EntityEjbDescriptor entityEjb = new Weblogic51EntityEjbDescriptor((EntityEjbDescriptor)ejb);
                ejbs.addElement(entityEjb);
                /*
                if (((EntityEjbDescriptor)entityEjb.getDescriptor()).isCmp()) {
                    if (cmp == null)
                    	cmp = new Weblogic51ContainerManaged();
                    cmp.addEjb(entityEjb.getDbms());
                }
                */
            }
        }
    }

    public String getDescription(){
            return description;
        }

    public void setDescription(String description){
            this.description = description;
        }

    public HashMap getRoles(){
            return roles;
        }

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

    public void addRole(String name, String principal) {
        if ( principal == null || principal.equals("") || name == null || name.equals("") )
            return;
        Weblogic60RoleEntry entry = (Weblogic60RoleEntry)roles.get(name);
        if (entry == null)
            entry = new Weblogic60RoleEntry(name);
        entry.addPrincipal(principal);
        /*
        if (getRole(name) == null)
            roles.addElement(new Weblogic60RoleEntry(name, principal));
        */
    }
    public Weblogic60RoleEntry getRole(String name){
        return (Weblogic60RoleEntry)roles.get(name);
    }
    public boolean hasRole(String name, String principal) {
        Weblogic60RoleEntry entry = (Weblogic60RoleEntry)roles.get(name);
        if ( entry == null)	return false;
        return entry.contains(principal);
    }
    public void removeRole(String name, String principal) {
        Weblogic60RoleEntry entry = (Weblogic60RoleEntry)roles.get(name);
        entry.removePrincipal(principal);
        if (entry.isEmpty())
            roles.remove(name);
    }

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

    public Weblogic51ApplicationDescriptor getAppDescriptor(){
            return appDescriptor;
        }

    public void setAppDescriptor(Weblogic51ApplicationDescriptor appDescriptor){
            this.appDescriptor = appDescriptor;
        }

    public String getDeployableFileName() {
        return appDescriptor.getDeployableDirectory() + appDescriptor.getDeployablePrefix() + CommonUtil.getFileName(descriptor.getFileName());
    }

    public ElementNode getDocument()
    {
        return Weblogic51XmlUtil.getNewWeblogicEjbBundleNode(this/*, EnvironmentManager.getSelectedServer()*/);
    }

    public void archive(/*ElementNode doc*/) {
		CommonUtil.copyFile(descriptor.getFileName(), getDeployableFileName());
        archiveToJar((Weblogic51EjbBundleNode)getDocument(), "weblogic-ejb-jar.xml", getDeployableFileName());
    }

    public void archiveToJar(Weblogic51EjbBundleNode 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);
            //if ( cmp != null )
            Weblogic51EjbDescriptor ejb;
            Weblogic51DbmsBeanDescriptor dbms;
            for ( int i=0 ; i<ejbs.size() ; i++) {
                ejb = (Weblogic51EjbDescriptor)ejbs.elementAt(i);
                if ( ( ejb instanceof Weblogic51EntityEjbDescriptor ) && ((Weblogic51EntityEjbDescriptor)ejb).isCmp()) {
                    dbms = ((Weblogic51EntityEjbDescriptor)ejb).getDbms();
            		ArchiveEntry.writeDD(jos, Weblogic51XmlUtil.getNewWeblogicCMPNode(dbms), CommonUtil.cutFirst(getFirstEntityEjbDescriptor().getPersistentTypeStorage(), "META-INF/"));
                }
            }
        } 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 Weblogic51ContainerManaged getCmp(){
            return cmp;
        }

    public void setCmp(Weblogic51ContainerManaged cmp){
            this.cmp = cmp;
        }
*/
    Weblogic51EntityEjbDescriptor getFirstEntityEjbDescriptor(){
        for ( int i=0 ; i < ejbs.size() ; i++ )
            if ( ejbs.elementAt(i) instanceof Weblogic51EntityEjbDescriptor )
            	return (Weblogic51EntityEjbDescriptor)ejbs.elementAt(i);
        return null;
    }

    private EjbBundleDescriptor descriptor;
    private Vector ejbs;
    private String description;
    private HashMap roles;
    private Weblogic51ApplicationDescriptor appDescriptor;
    //private Weblogic51ContainerManaged cmp;
}
