01: package com.mockrunner.example.connector;
02:
03: import javax.naming.InitialContext;
04: import javax.naming.NamingException;
05: import javax.resource.ResourceException;
06: import javax.resource.cci.Connection;
07: import javax.resource.cci.ConnectionFactory;
08: import javax.resource.cci.Interaction;
09: import javax.resource.cci.InteractionSpec;
10:
11: /**
12: * A DAO that communicates with the CICS backend to find a person
13: * by id.
14: * Please note that the inner class <code>ECIInteractionSpec</code> is
15: * only necessary because we cannot ship the suitable IBM classes
16: * along with this simple example. In reality, the class
17: * <code>com.ibm.connector2.cics.ECIInteractionSpec</code>
18: * should be used.
19: */
20: public class PersonSearchDAO {
21: private ConnectionFactory connectionFactory;
22:
23: public PersonSearchDAO() {
24: try {
25: InitialContext context = new InitialContext();
26: connectionFactory = (ConnectionFactory) context
27: .lookup("java:ra/cics/ConnectionFactory");
28: } catch (NamingException exc) {
29: throw new RuntimeException(
30: "Failed to create ConnectionFactory "
31: + exc.getMessage());
32: }
33: }
34:
35: public Person findPersonById(String id) {
36: Connection connection = null;
37: Person request = new Person();
38: request.setId(id);
39: Person response = new Person();
40: try {
41: connection = connectionFactory.getConnection();
42: Interaction interaction = connection.createInteraction();
43: ECIInteractionSpec interactionSpec = new ECIInteractionSpec();
44: interactionSpec.setFunctionName("PER3AC");
45: interactionSpec
46: .setInteractionVerb(ECIInteractionSpec.SYNC_SEND_RECEIVE);
47: interactionSpec.setCommareaLength(32);
48: interaction.execute(interactionSpec, request, response);
49: } catch (ResourceException exc) {
50: exc.printStackTrace();
51: } finally {
52: try {
53: if (null != connection)
54: connection.close();
55: } catch (ResourceException exc) {
56: exc.printStackTrace();
57: }
58: }
59: return response;
60: }
61:
62: /*
63: * Replacement for com.ibm.connector2.cics.ECIInteractionSpec.
64: * Only exists for the sake of this simple demonstration.
65: */
66: private class ECIInteractionSpec implements InteractionSpec {
67: private String functionName;
68: private int commareaLength;
69: private int interactionVerb;
70:
71: public int getCommareaLength() {
72: return commareaLength;
73: }
74:
75: public void setCommareaLength(int commareaLength) {
76: this .commareaLength = commareaLength;
77: }
78:
79: public String getFunctionName() {
80: return functionName;
81: }
82:
83: public void setFunctionName(String functionName) {
84: this .functionName = functionName;
85: }
86:
87: public int getInteractionVerb() {
88: return interactionVerb;
89: }
90:
91: public void setInteractionVerb(int interactionVerb) {
92: this.interactionVerb = interactionVerb;
93: }
94: }
95: }
|