// ConnectorCASLR.java
// Stateless Session bean

package org.objectweb.jonas.jtests.beans.jca15;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionMetaData;
import javax.resource.spi.ManagedConnection;
import ersatz.resourceadapter.*;
import javax.resource.spi.ConnectionEvent;

/**
 *  -
 */
public class ConnectorCASLR implements SessionBean {

    SessionContext ejbContext;
    private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
    private ManagedConnectionFactoryImpl mcf = null; //Managed Connection Factory
    private ConnectionSpecImpl csp = null; //ConnectionSpec
    private ConnectionImpl conn = null;
    InitialContext ic=null;
    InteractionImpl i = null;
    ConnectionMetaDataImpl cMetaData = null;
    String cName = "ConnectorCASLR";

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------


    public void setSessionContext(SessionContext ctx) {
        Utility.log(cName+".setSessionContext");
        ejbContext = ctx;
    }
        

    public void ejbRemove() {
        Utility.log(cName+".ejbRemove");
    }
        

    public void ejbCreate() throws CreateException {
        Utility.log(cName+".ejbCreate");
    }

    public void ejbPassivate() {
        Utility.log(cName+".ejbPassivate");
    }

    public void ejbActivate() {
        Utility.log(cName+".ejbActivate");
    }
    
    // ------------------------------------------------------------------
    // ConnectorCA implementation
    // ------------------------------------------------------------------
    /**
     * method1
     */
    public void method1(String rar_jndi_name, String testName) 
            throws Exception
    {
        Utility.log("============================ "+testName);
        try {
            ic = new InitialContext();
        } catch (Exception e1) {
            Utility.log(cName+".method1 error: InitialContext failed");
            throw e1;
        }
        try {
            cccf = (ConnectionFactoryImpl)ic.lookup(rar_jndi_name);
            Utility.log(cName+".method1 : found "+rar_jndi_name);
        } catch (Exception e2) {
            Utility.log(cName+".method1 error: lookup failed for "+rar_jndi_name);
            throw e2;
        }
        
        try {
            csp = new ConnectionSpecImpl(); // get a new ConnectionSpec
                Utility.log(cName
                  +".method1 : ConnectionSpec o.k.");
            
        } catch (Exception e3) {
            Utility.log(cName+".method1 : new connection spec failed");
            throw e3;
        }
        try {
            conn = (ConnectionImpl)cccf.getConnection();
            Utility.log(cName+".method1 : getConnection conn="+conn);
            if (conn==null) {
                Utility.log(cName+".method1 error: getConnection returned null connection.");
                throw new Exception("");
            }
        } catch (Exception e4) {
            Utility.log(cName+".method1 error: getConnection failed "
                    +e4.toString());
            throw e4;
        }
        try {
            i = (InteractionImpl)conn.createInteraction();
        } catch (Exception e5) {
            Utility.log(cName+".method1 error: create interaction failed");
            throw e5;
        }
        try {
            cMetaData = (ConnectionMetaDataImpl)conn.getMetaData();
        } catch (Exception e6) {
            Utility.log(cName+".method1 : get ConnectionMetaData failed");
            throw e6;
        }

    }

    /**
     * closeUp
     */
    public void closeUp(int w) {
        try {
            if (w>0) {
                // The CONNECTION_ERROR_OCCURRED indicates that the associated 
                // ManagedConnection instance is now invalid and unusable.
                conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
                Utility.log(cName+".closeUp : closed physical connection");
            } else {
                // The CONNECTION_CLOSED indicates that connection handle
                // is closed, but physical connection still exists
                conn.close();
                Utility.log(cName+".closeUp : closed connection");
            }
        } catch (Exception e) {
            Utility.log(cName+".closeUp error: close handle/physical connection failed");
        }
    }
    /**
     * JUnit tests
     */
    public boolean isConnectionSpec() {
        if (csp==null) return false;
        else return true;
    }
    public boolean isConnection() {
        if (conn==null) return false;
        else return true;
    }
    public boolean isInteraction() {
        if (i==null) return false;
        else return true;
    }
    public String getConnectionProduct() 
            throws Exception
    {
        String s;
        try {
            s = cMetaData.getEISProductName();
            Utility.log(cName+".getConnectionProduct : ConnectionMetaData.product="+s);
        } catch (Exception e) {
            Utility.log(cName+".getConnectionProduct error: ConnectionMetaData.getEISProductName() failed");
            throw e;
        }
        return s;
    }
    /**
     * The application server calls setter methods on the ManagedConnectionFactory instance
     * to set various configuration properties on this instance. These properties provide
     * information required by the ManagedConnectionFactory instance to create physical
     * connections to the underlying EIS. The application server uses an existing property set
     * (configured during the deployment of a resource adapter) to set the required properties on
     * the ManagedConnectionFactory instance.
     * 
     */    
    public String getServerName()
            throws Exception
    {
        Utility.log(cName+".getServerName");
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactoryImpl    
        String cp1 = "null";
        if (mcf==null) return cp1;
        try {
            cp1 = mcf.getServerName();
        } catch (Exception e) {
            Utility.log(cName+".getConfigurationProperty error: failed");
            throw e;
        }
        return cp1;
    }
    public String getProtocolProperty()
            throws Exception
    {
        Utility.log(cName+".getProtocolProperty");
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory    
        String p = "null";
        if (mcf==null) return p;
        try {
            p = mcf.getProtocol();
        } catch (Exception e) {
            Utility.log(cName+".getProtocolProperty error: failed");
            throw e;
        }
        return p;
    }
    /**
     * After the newly created ManagedConnectionFactory instance has been configured with
     * its property set, the application server creates a new ConnectionManager instance.
     * true returned if ConnectionManager is valid
     */
    public boolean getCMInstance()
    {
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf();  // ManagedConnectionFactory    
        if (mcf.getCM()==null) {                     // ConnectionManager not null
            Utility.log(cName+".getCMInstance error: ConnectionManager is null");
            return false;
        }
        else {
            Utility.log(cName+".getCMInstance ConnectionManager is o.k.");
            return true;
        }
    }
    public int getMCF_Pwriter()
    {
        int here=2;
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf();  // ManagedConnectionFactory    
        try {
            if (mcf.getLogWriter()==null) {              // PrintWriter not null
                Utility.log(cName+".getMCF_Pwriter No PrintWriter registered");
                here=0;
            }
            else {
                Utility.log(cName+".getMCF_Pwriter PrintWriter is o.k.");
                here=1;
            }
        } catch (Exception e) {
            Utility.log(cName+
                ".getMCF_Pwriter error: "+e.toString());
        }
        return here;
    }
    public int getMC_Pwriter()
    {
        int here=2;
        ConnectionImpl conni = (ConnectionImpl)conn;
        try {
            ManagedConnectionImpl mc = (ManagedConnectionImpl)conni.getMC(); //get ManagedConnection        
            if (mc.getLogWriter()==null) {              // PrintWriter not null
                Utility.log(cName+
                    ".getMC_Pwriter No PrintWriter registered in ManagedConnection");
                here=0;
            }
            else {
                Utility.log(cName+
                    ".getMC_Pwriter PrintWriter in ManagedConnection is o.k.");
                here=1;
            }
        } catch (Exception e) {
            Utility.log(cName+
                ".getMC_Pwriter error: "+e.toString());
        }
        return here;
    }
    public void setMatchNull(boolean b) {
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory    
        mcf.setMatchNull(b);        
    }
    public int cntListeners()
    {
        int i = 0;
        ConnectionImpl conni = (ConnectionImpl)conn;
        try {
             ManagedConnectionImpl mc = (ManagedConnectionImpl)conni.getMC(); //get ManagedConnection        
             i = mc.cntListeners();
            Utility.log(cName+".cntListeners cnt="+i);
        } catch (Exception e) {
            Utility.log(cName+".cntListeners error: failed to count Listeners");
        }
        return i;
    }
}

