// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Interaction;
import javax.resource.cci.InteractionSpec;
import javax.resource.cci.MappedRecord;
import javax.resource.cci.RecordFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jcatest.outboundtcp.OutboundTCPConnection;
import jcatest.outboundtcp.OutboundTCPConnectionFactory;
import jcatest.outboundtcp.OutboundTCPConnectionManager;
import jcatest.outboundtcp.OutboundTCPManagedConnectionFactory;
import jcatest.outboundtcp.OutboundTCPInteractionSpec;

public class TestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	try {
			System.out.println("managed non-CCI interface");
			Context ctx = new InitialContext();
			OutboundTCPConnectionFactory cf = (OutboundTCPConnectionFactory)ctx.lookup("java:OutboundTCP");
			OutboundTCPConnection con = (OutboundTCPConnection)cf.getConnection();
			byte[] req = new byte[] { 1, 2, 3 };
			byte[] resp = con.execute(req);
			for(int i = 0; i < resp.length; i++) {
				if(resp[i] != req[i] + 1) {
					throw new RuntimeException("Corrupt data");
				}
			}
			con.close();
	        response.getWriter().println("managed non-CCI interface OK");
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (ResourceException e) {
			e.printStackTrace();
		}
		try {
			System.out.println("managed CCI interface");
			Context ctx = new InitialContext();
			ConnectionFactory cf = (ConnectionFactory)ctx.lookup("java:OutboundTCP");
			Connection con = cf.getConnection();
			Interaction iact = con.createInteraction();
			InteractionSpec iactspec = new OutboundTCPInteractionSpec("execute", InteractionSpec.SYNC_SEND_RECEIVE, 0);
			byte[] req = new byte[] { 1, 2, 3 };
			RecordFactory rf = cf.getRecordFactory();
			MappedRecord reqrec = rf.createMappedRecord("standard");
			reqrec.put("data", req);
			MappedRecord resprec = (MappedRecord)iact.execute(iactspec, reqrec);
			byte[] resp = (byte[])resprec.get("data");
			for(int i = 0; i < resp.length; i++) {
				if(resp[i] != req[i] + 1) {
					throw new RuntimeException("Corrupt data");
				}
			}
			iact.close();
			con.close();
	        response.getWriter().println("managed CCI interface OK");
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (ResourceException e) {
			e.printStackTrace();
		}
    	try {
			System.out.println("unmanaged non-CCI interface");
            OutboundTCPManagedConnectionFactory mcf = new OutboundTCPManagedConnectionFactory();
            mcf.setServerName("localhost");
            mcf.setPortNumber(1234);
            mcf.setDebug(true);
			OutboundTCPConnectionFactory cf = (OutboundTCPConnectionFactory)mcf.createConnectionFactory(new OutboundTCPConnectionManager());
			OutboundTCPConnection con = (OutboundTCPConnection)cf.getConnection();
			byte[] req = new byte[] { 1, 2, 3 };
			byte[] resp = con.execute(req);
			for(int i = 0; i < resp.length; i++) {
				if(resp[i] != req[i] + 1) {
					throw new RuntimeException("Corrupt data");
				}
			}
			con.close();
	        response.getWriter().println("unmanaged non-CCI interface OK");
		} catch (ResourceException e) {
			e.printStackTrace();
		}
    	try {
			System.out.println("unmanaged CCI interface");
            OutboundTCPManagedConnectionFactory mcf = new OutboundTCPManagedConnectionFactory();
            mcf.setServerName("localhost");
            mcf.setPortNumber(1234);
            mcf.setDebug(true);
			OutboundTCPConnectionFactory cf = (OutboundTCPConnectionFactory)mcf.createConnectionFactory(new OutboundTCPConnectionManager());
			OutboundTCPConnection con = (OutboundTCPConnection)cf.getConnection();
			Interaction iact = con.createInteraction();
			InteractionSpec iactspec = new OutboundTCPInteractionSpec("execute", InteractionSpec.SYNC_SEND_RECEIVE, 0);
			byte[] req = new byte[] { 1, 2, 3 };
			RecordFactory rf = cf.getRecordFactory();
			MappedRecord reqrec = rf.createMappedRecord("standard");
			reqrec.put("data", req);
			MappedRecord resprec = (MappedRecord)iact.execute(iactspec, reqrec);
			byte[] resp = (byte[])resprec.get("data");
			for(int i = 0; i < resp.length; i++) {
				if(resp[i] != req[i] + 1) {
					throw new RuntimeException("Corrupt data");
				}
			}
			iact.close();
			con.close();
	        response.getWriter().println("unmanaged CCI interface OK");
		} catch (ResourceException e) {
			e.printStackTrace();
		}
    }
}
                                                      
