CICS Program Call with Commarea

Sample EJB accessing a CICS resource adapter using CommArea with extended LUW.
Note: To enable SSL, include the SSL code snippet shown in bold in the CICS Program Call with Commarea Using SSL sample.
File CommAreaEJB.java:
package com.mypackage.commareaEJB ;
import javax.ejb.Remote;
import javax.ejb.EJBException;
@Remote
public interface CommareaEJB {
    public byte[] First(byte[] commarea) throws javax.resource.ResourceException;
    public int getCobolReturnCode();
    public void removeSF() throws EJBException;
    public void passivate() throws EJBException;
    public void activate() throws EJBException;
}
File CommAreaEJBBean.java:
package com.mypackage.commareaEJB ;
import javax.ejb.Stateless;
import javax.ejb.Stateful;
import javax.ejb.EJB;
import javax.ejb.Init;
import javax.ejb.Remove;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.EJBException;
import javax.annotation.Resource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.Remote;
import com.microfocus.cics.connector.ccl.CCLExtendMode;
import com.microfocus.cics.connector.ccl.CommArea;
import com.microfocus.cics.connector.ccl.AbendException;
import com.microfocus.cics.connector.ccl.AbendCodeType;
import com.microfocus.cics.connector.ccl.CommAreaSizeException;


@Stateful(name="commareaEJB", mappedName="ejb/commareaEJB")
@Remote(com.mypackage.commareaEJB.CommareaEJB.class)

public class CommareaEJBBean implements CommareaEJB, java.io.Serializable {

    @Resource (name="CCIMFCICS_v1.5", authenticationType=Resource.AuthenticationType.CONTAINER, shareable=false)
    private transient javax.resource.cci.ConnectionFactory cf;
    private boolean disposed = false;
    private String sessionId = null;
    private transient javax.resource.cci.Connection lastConnectionHandle = null;
    private boolean doInitialize = true;
    public static final int DEFAULT_COBOL_RETURN_CODE = 2147483647;
    private int cobolReturnCode = DEFAULT_COBOL_RETURN_CODE;
    com.microfocus.cics.connector.cci.MFECIInteractionSpec interactionSpec = new com.microfocus.cics.connector.cci.MFECIInteractionSpec();


    public javax.resource.cci.ConnectionFactory getConnectionFactory() {
          return cf ;
    }

    @PostConstruct
    public void create() {           
	    disposed = false;
            sessionId = null;
	    interactionSpec.setFunctionName("CASA_ECI_Function");
            interactionSpec.setArgument(0, com.microfocus.cobol.lang.Pointer.class,
		        	 com.microfocus.cobol.RuntimeProperties.OUTPUT_ONLY);
            interactionSpec.setArgument(1, com.microfocus.cobol.lang.Pointer.class,
		        	com.microfocus.cobol.RuntimeProperties.BY_VALUE);
	    interactionSpec.setUserName("SYSAD");
            interactionSpec.setPassword("SYSAD");
	    interactionSpec.setProgramName("EC01");
    }

     @TransactionAttribute(TransactionAttributeType.NEVER)
    public byte[] First(byte[] commarea) throws javax.ejb.EJBException {
        javax.resource.cci.Connection con = null;
        disposed = false;
        con = getCobolConnection();
        javax.resource.cci.RecordFactory rf = null;
        javax.resource.cci.Interaction ix = null;
        try {
            if(doInitialize) {
                initialize(false, con);
                doInitialize = false;
            }
            ix = con.createInteraction();

	    interactionSpec.setExtendMode(CCLExtendMode.CCL_EXTENDED);
	    interactionSpec.setCommArea(new CommArea(commarea));
            rf = cf.getRecordFactory();
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            javax.resource.cci.IndexedRecord iRec = interactionSpec.getInputRecord();	    
            iRec.add(new com.microfocus.cobol.lang.Pointer(commarea));
            ix.execute(interactionSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = interactionSpec.getReturnCode();
            CommArea returnCommArea = interactionSpec.getReturnCommArea();
	    byte[] retCommArea = returnCommArea.getCommArea();
	    return retCommArea;

        } catch(javax.resource.ResourceException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
	    Exception linkedEx = ex.getLinkedException();
	    if(linkedEx instanceof AbendException) {
		AbendException aex = (AbendException) linkedEx;    
		System.out.println("AbendCode = " + aex.getAbendCode());
		System.out.println("Abend Code Type = " + aex.getAbendCodeType());
	    }
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(java.io.UnsupportedEncodingException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(CommAreaSizeException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        }
        finally {
            closeCobolConnection(con);
            ix = null; rf = null;
        }
    }

         @TransactionAttribute(TransactionAttributeType.NEVER)
    public byte[] commit() throws javax.ejb.EJBException {
        javax.resource.cci.Connection con = null;
        disposed = false;
        con = getCobolConnection();
        javax.resource.cci.RecordFactory rf = null;
        javax.resource.cci.Interaction ix = null;
        try {
            if(doInitialize) {
                initialize(false, con);
                doInitialize = false;
            }
            ix = con.createInteraction();
	    interactionSpec.setExtendMode(CCLExtendMode.CCL_COMMIT);
	    rf = cf.getRecordFactory();
            javax.resource.cci.IndexedRecord iRec = interactionSpec.getInputRecord();	    
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            ix.execute(interactionSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = interactionSpec.getReturnCode();
            CommArea returnCommArea = interactionSpec.getReturnCommArea();
	    byte[] retCommArea = returnCommArea.getCommArea();
	    return retCommArea;

        } catch(javax.resource.ResourceException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
	    Exception linkedEx = ex.getLinkedException();
	    if(linkedEx instanceof AbendException) {
		AbendException aex = (AbendException) linkedEx;    
		System.out.println("AbendCode = " + aex.getAbendCode());
		System.out.println("Abend Code Type = " + aex.getAbendCodeType());
	    }
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(java.io.UnsupportedEncodingException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        }
        finally {
            closeCobolConnection(con);
            interactionSpec = null; ix = null; rf = null;
        }
    }

             @TransactionAttribute(TransactionAttributeType.NEVER)
    public byte[] rollback() throws javax.ejb.EJBException {
        javax.resource.cci.Connection con = null;
        disposed = false;
        con = getCobolConnection();
        javax.resource.cci.RecordFactory rf = null;
        javax.resource.cci.Interaction ix = null;
        try {
            if(doInitialize) {
                initialize(false, con);
                doInitialize = false;
            }
            ix = con.createInteraction();
	    interactionSpec.setExtendMode(CCLExtendMode.CCL_BACKOUT);
            rf = cf.getRecordFactory();
            javax.resource.cci.IndexedRecord iRec = interactionSpec.getInputRecord();
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            ix.execute(interactionSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = interactionSpec.getReturnCode();
            CommArea returnCommArea = interactionSpec.getReturnCommArea();
	    byte[] retCommArea = returnCommArea.getCommArea();
	    return retCommArea;

        } catch(javax.resource.ResourceException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
	    Exception linkedEx = ex.getLinkedException();
	    if(linkedEx instanceof AbendException) {
		AbendException aex = (AbendException) linkedEx;    
		System.out.println("AbendCode = " + aex.getAbendCode());
		System.out.println("Abend Code Type = " + aex.getAbendCodeType());
	    }
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(java.io.UnsupportedEncodingException ex) {
            cobolReturnCode = interactionSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } 
        finally {
            closeCobolConnection(con);
            interactionSpec = null; ix = null; rf = null;
        }
    }

    @Remove
    public void removeSF() {
            cleanup();
    }

    void cleanup() throws javax.ejb.EJBException {
       try {
            if(!disposed) {
                if(lastConnectionHandle != null) {
                  com.microfocus.cics.connector.cci.MFECIConnection.dispose(sessionId);
                  closeCobolConnection(lastConnectionHandle);
                }
                else {
                  com.microfocus.cics.connector.cci.MFECIConnection.dispose(sessionId);
                }
              disposed = true ;
            }
       } catch(javax.resource.ResourceException ex) {
            throw new javax.ejb.EJBException("Error disposing a Cobol Connection: ", ex);
       }
       finally {
           lastConnectionHandle = null;
           cf = null;
       }
    }

    public int getCobolReturnCode() throws javax.ejb.EJBException {
         return cobolReturnCode;
    }

    @PrePassivate
    public void passivate() throws javax.ejb.EJBException {
    }

    @PostActivate
    public void activate() throws javax.ejb.EJBException {
    }

    private void initialize(boolean isInitial, javax.resource.cci.Connection con) throws javax.ejb.EJBException {
        try {
            javax.resource.cci.Interaction ix = con.createInteraction();
            com.microfocus.cics.connector.cci.MFECIInteractionSpec iSpec = new com.microfocus.cics.connector.cci.MFECIInteractionSpec();
            iSpec.setFunctionName("initialize");
            javax.resource.cci.RecordFactory rf = cf.getRecordFactory();
            javax.resource.cci.IndexedRecord irec = rf.createIndexedRecord("beanArgs");
            irec.add(new Boolean(isInitial));
            javax.resource.cci.Record orec = ix.execute(iSpec, irec);
            ix.close();
            rf = null ; ix = null ; iSpec = null ; irec = null ;
        } catch(javax.resource.ResourceException ex) {
            throw new javax.ejb.EJBException("initialize threw ResourceException: ", ex);
        }
    }

    private javax.resource.cci.Connection getCobolConnection() throws javax.ejb.EJBException {

            com.microfocus.cics.connector.cci.MFECIConnection con = null;
        try {
            con = (com.microfocus.cics.connector.cci.MFECIConnection) cf.getConnection(new com.microfocus.cics.connector.cci.MFECIConnectionSpec(true, true, sessionId));
            lastConnectionHandle = con; 

        } catch (javax.resource.ResourceException ex) {
             throw new javax.ejb.EJBException("Error getting a Cobol Connection: ", ex);
        }
        return con ;
    }

    private void closeCobolConnection(javax.resource.cci.Connection con) throws javax.ejb.EJBException {
        try {
            if(con != null) {
             if(sessionId == null) {
                 sessionId = ((com.microfocus.cics.connector.cci.MFECIConnection)con).getSessionId();
             }
              con.close();
            }
        } catch (javax.resource.ResourceException ex) {
            throw new javax.ejb.EJBException("Error closing a Cobol Connection: ", ex);
        }
    }