CICS Program Call with Channels and Containers

Sample EJB accessing a CICS resource adapter using Channel, without 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 Container.java:
package com.mypackage.channelEJB ;

public class Container extends com.microfocus.cics.connector.cci.MFECICustomRecord {

    private static String EMPTY_STRING = "";
    private java.lang.String containerData = EMPTY_STRING;

    public Container() {
    }

    public java.lang.String getContainerData() {
        return containerData;
    }

    public void setContainerData(java.lang.String ___p) {
        containerData = ___p ;
    }


    public Object[] getParameters() {        Object[] objs = new Object[1];

        objs[0] = containerData;
        return objs;
    }

    public void setParameters(Object[] objs) {
        containerData = (java.lang.String) objs[0];
    }

}
File Channel.java:
package com.mypackage.channelEJB ;

public class Channel extends com.microfocus.cics.connector.cci.MFECICustomRecord {

    private static String EMPTY_STRING = "";
    private Container2 container2;
    private Container1 container1;

    public class Container2 extends com.microfocus.cics.connector.cci.MFECICustomRecord {

        private java.lang.String containerData = EMPTY_STRING;

        public Container2() {
        }

        public java.lang.String getContainerData() {
            return containerData;
        }

        public void setContainerData(java.lang.String ___p) {
            containerData = ___p ;
        }


        public Object[] getParameters() {
            Object[] objs = new Object[1];
            objs[0] = containerData;
            return objs;
        }

        public void setParameters(Object[] objs) {
            containerData = (java.lang.String) objs[0];
        }

    }
    public class Container1 extends com.microfocus.cics.connector.cci.MFECICustomRecord {

        private java.lang.String containerData = EMPTY_STRING;

        public Container1() {
        }

        public java.lang.String getContainerData() {
            return containerData;
        }

        public void setContainerData(java.lang.String ___p) {
            containerData = ___p ;
        }


        public Object[] getParameters() {
            Object[] objs = new Object[1];
            objs[0] = containerData;
            return objs;
        }

        public void setParameters(Object[] objs) {
            containerData = (java.lang.String) objs[0];
        }

    }
    public Channel() {
        container2 = new Container2();
        container1 = new Container1();
    }

    public Container2 getContainer2() {
        return container2;
    }

    public void setContainer2(Container2 ___p) {
        container2 = ___p ;
    }

    public Container1 getContainer1() {
        return container1;
    }

    public void setContainer1(Container1 ___p) {
        container1 = ___p ;
    }


    public Object[] getParameters() {        Object[] objs = new Object[2];

        objs[0] = container2;
        objs[1] = container1;
        return objs;
    }

    public void setParameters(Object[] objs) {
        container2.setParameters(((Container2)objs[0]).getParameters());
        container1.setParameters(((Container1)objs[1]).getParameters());
    }

}
File ChannelEJB.java:
package com.mypackage.channelEJB ;


import javax.ejb.Remote;
@Remote
public interface ChannelEJB {
    public com.mypackage.channelEJB.Channel First(com.mypackage.channelEJB.Channel channel);
    public int getCobolReturnCode();
}
File ChannelEJBBean.java

package com.mypackage.channelEJB ;

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.CCLChannel;
import com.microfocus.cics.connector.ccl.CCLContainer;
import com.microfocus.cics.connector.ccl.ContainerData;
import com.microfocus.cics.connector.ccl.AbendException;
import com.microfocus.cics.connector.ccl.AbendCodeType;

import java.util.List;


@Stateless(name="channelEJB", mappedName="ejb/channelEJB")
@Remote(com.mypackage.channelEJB.ChannelEJB.class)

public class ChannelEJBBean implements ChannelEJB, 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 com.mypackage.channelEJB.Channel First(com.mypackage.channelEJB.Channel channel) 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("channel.First");
            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("EC03");
	    
            CCLChannel cclChannel = new CCLChannel("GenChannel");
	    CCLContainer cclContainer1 = new CCLContainer("GenCon1");
	    CCLContainer cclContainer2 = new CCLContainer("GenCon2");
	    byte[] cData1 = channel.getContainer1().getContainerData().getBytes();
	    byte[] cData2 = channel.getContainer2().getContainerData().getBytes();
	    ContainerData cd1 = new ContainerData(cData1);
            ContainerData cd2 = new ContainerData(cData2);
	    cclContainer1.setContainerData(cd1);
            cclContainer2.setContainerData(cd2);
            cclChannel.addContainer(cclContainer1);
            cclChannel.addContainer(cclContainer2);
	    iSpec.setChannel(cclChannel);
            rf = cf.getRecordFactory();
            javax.resource.cci.IndexedRecord oRec = rf.createIndexedRecord("FirstOut");
            javax.resource.cci.IndexedRecord iRec = iSpec.getInputRecord();	    
            ix.execute(iSpec, iRec, oRec);
            ix.close();
            cobolReturnCode = iSpec.getReturnCode();
             CCLChannel returnChannel = iSpec.getReturnChannel();
	    com.mypackage.channelEJB.Channel retFromEJBChannel = new com.mypackage.channelEJB.Channel();
	    if(returnChannel != null) {
		    List<CCLContainer> containers = returnChannel.getContainers();
		    int count = 0;
		    for(CCLContainer container: containers) {
                        if(count == 0) {
				retFromEJBChannel.getContainer1().setContainerData(new String(container.getContainerData().getContainerData()));
				count++;
			}
			if(count == 1) {
				retFromEJBChannel.getContainer2().setContainerData(new String(container.getContainerData().getContainerData()));
				count++;
			}
			System.out.println(container.getContainerName());
			System.out.println(container.getContainerData().getContainerData());
	            }
	    }
	    return retFromEJBChannel;
		    

        } 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);
        }
        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);
        }
    }

}