CICS Program Call with Commarea

Sample EJB accessing the WebSphere or JBoss EAP CICS resource adapter using CommArea without extended LUW.
Note: For a list of supported WebSphere and JBoss application server versions, see the Java Application Servers section of the Supported Operating Systems and Third-party Software topic.
File CommAreaEJB.java:
package com.mypackage.commareaEJB ;
import javax.ejb.Remote;
@Remote
public interface CommareaEJB {
    public byte[] callMyCICSProgram(byte[] commarea);
    public int getCobolReturnCode();
}
WebSphere - 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;

@Stateless(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=true)
    private transient javax.resource.cci.ConnectionFactory cf;
    private boolean disposed = false;
    private String sessionId = null;
    public static final int DEFAULT_COBOL_RETURN_CODE = 2147483647;
    private int cobolReturnCode = DEFAULT_COBOL_RETURN_CODE;

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

    @PostConstruct
    public void create() {            
       sessionId = null;
    }

     @TransactionAttribute(TransactionAttributeType.NEVER)
    public byte[] callMyCICSProgram(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;
        com.microfocus.cics.connector.cci.MFECIInteractionSpec iSpec = null;
        try {
            initialize(true, con);
            ix = con.createInteraction();
            iSpec = new com.microfocus.cics.connector.cci.MFECIInteractionSpec();
            iSpec.setFunctionName("CASA_ECI_Function");
            iSpec.setArgument(0, com.microfocus.cobol.lang.Pointer.class,
		        	 com.microfocus.cobol.RuntimeProperties.OUTPUT_ONLY);
            iSpec.setArgument(1, com.microfocus.cobol.lang.Pointer.class,
		        	com.microfocus.cobol.RuntimeProperties.BY_VALUE);
	    iSpec.setExtendMode(CCLExtendMode.CCL_NO_EXTEND);
	    iSpec.setUserName("SYSAD");
          iSpec.setPassword("SYSAD");
	    iSpec.setProgramName("EC01");
	    iSpec.setCommArea(new CommArea(commarea));

          rf = cf.getRecordFactory();
          javax.resource.cci.IndexedRecord iRec = iSpec.getInputRecord();
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            ix.execute(iSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = iSpec.getReturnCode();
            CommArea returnCommArea = iSpec.getReturnCommArea();
	    byte[] retCommArea = returnCommArea.getCommArea();
	    return retCommArea;

	} catch(javax.resource.ResourceException ex) {
            cobolReturnCode = iSpec.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 = iSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(CommAreaSizeException ex) {
            cobolReturnCode = iSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        }

        finally {
            closeCobolConnection(con);
            con = null; iSpec = null; ix = null; rf = null;
            sessionId = null;
        }
    }

    void cleanup() throws javax.ejb.EJBException {
    }

    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(false, sessionId));

        } 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) {
              con.close();
            }
        } catch (javax.resource.ResourceException ex) {
            throw new javax.ejb.EJBException("Error closing a Cobol Connection: ", ex);
        }
    }

}
JBoss EAP - 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;

@Stateless(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=true, mappedName="java:/eis/MFCICS_v1.5")
    private transient javax.resource.cci.ConnectionFactory cf;
    private boolean disposed = false;
    private String sessionId = null;
    public static final int DEFAULT_COBOL_RETURN_CODE = 2147483647;
    private int cobolReturnCode = DEFAULT_COBOL_RETURN_CODE;

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

    @PostConstruct
    public void create() {            
       sessionId = null;
    }

     @TransactionAttribute(TransactionAttributeType.NEVER)
    public byte[] callMyCICSProgram(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;
        com.microfocus.cics.connector.cci.MFECIInteractionSpec iSpec = null;
        try {
            initialize(true, con);
            ix = con.createInteraction();
            iSpec = new com.microfocus.cics.connector.cci.MFECIInteractionSpec();
            iSpec.setFunctionName("CASA_ECI_Function");
            iSpec.setArgument(0, com.microfocus.cobol.lang.Pointer.class,
		        	 com.microfocus.cobol.RuntimeProperties.OUTPUT_ONLY);
            iSpec.setArgument(1, com.microfocus.cobol.lang.Pointer.class,
		        	com.microfocus.cobol.RuntimeProperties.BY_VALUE);
	    iSpec.setExtendMode(CCLExtendMode.CCL_NO_EXTEND);
	    iSpec.setUserName("SYSAD");
          iSpec.setPassword("SYSAD");
	    iSpec.setProgramName("EC01");
	    iSpec.setCommArea(new CommArea(commarea));

          rf = cf.getRecordFactory();
          javax.resource.cci.IndexedRecord iRec = iSpec.getInputRecord();
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            ix.execute(iSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = iSpec.getReturnCode();
            CommArea returnCommArea = iSpec.getReturnCommArea();
	    byte[] retCommArea = returnCommArea.getCommArea();
	    return retCommArea;

	} catch(javax.resource.ResourceException ex) {
            cobolReturnCode = iSpec.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 = iSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        } catch(CommAreaSizeException ex) {
            cobolReturnCode = iSpec.getReturnCode();
            throw new javax.ejb.EJBException("First threw a resource Exception", ex);
        }

        finally {
            closeCobolConnection(con);
            con = null; iSpec = null; ix = null; rf = null;
            sessionId = null;
        }
    }

    void cleanup() throws javax.ejb.EJBException {
    }

    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(false, sessionId));

        } 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) {
              con.close();
            }
        } catch (javax.resource.ResourceException ex) {
            throw new javax.ejb.EJBException("Error closing a Cobol Connection: ", ex);
        }
    }

}