// InboundCASLR.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.spi.ManagedConnection;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ResourceAdapter;
import ersatz.resourceadapter.ManagedConnectionFactoryImpl;
import ersatz.resourceadapter.ConnectionImpl;
import ersatz.resourceadapter.ConnectionFactoryImpl;
import ersatz.resourceadapter.ResourceAdapterImpl;


/**
 *
 */
public class InboundCASLR implements SessionBean {

    SessionContext ejbContext;
    private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
    private ManagedConnectionFactoryImpl mcf = null;
    private ConnectionImpl conn = null;
    InitialContext ic=null;
    String cName = "InboundCASLR";

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


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

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

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

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

    public void ejbActivate() {
        Utility.log("");
    }
    
    // ------------------------------------------------------------------
    // InboundCA 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 {
            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 e3) {
            Utility.log(cName+".method1 error: getConnection failed "
                    +e3.toString());
            throw e3;
        }

    }

    /**
     * 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
     */

    /**
     *  ra.xml contains value, used by Application Server to set EIS_URL in ra
     */
    public String getEIS_URL()
            throws Exception
    {
        Utility.log(cName+".getEIS_URL");
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf();
        ResourceAdapterImpl ra = (ResourceAdapterImpl)mcf.getResourceAdapter();
        
        String cp1 = "none";
        if (ra==null) return cp1;
        try {
            cp1 = ra.getEIS_URL();
        } catch (Exception e) {
            Utility.log(cName+".getEIS_URL get <configuration-property> error: failed");
            throw e;
        }
        return cp1;
    }
    public boolean deliverMsg(String msg) throws Exception
    {
        boolean success;
        
        Utility.log(cName+".deliverMsg to resource adapter");
        mcf = (ManagedConnectionFactoryImpl) cccf.getMcf();
        ResourceAdapterImpl ra = (ResourceAdapterImpl)mcf.getResourceAdapter();
        try {
            success = ra.deliverMsg(msg);
            Utility.log(cName+".deliverMsg o.k. msg="+msg);
        } catch (Exception e) {
            Utility.log(cName+".deliverMsg error: e="+e.toString());
            success=false;
        }
        return success;
    }
}

