1
2
3
4
5
6
7
8
9
10
11 package it.imolinfo.jbi4cics.connection.jca;
12
13 import it.imolinfo.jbi4cics.connection.ConnectionManager;
14 import it.imolinfo.jbi4cics.exception.ConnectionException;
15 import it.imolinfo.jbi4cics.locator.ServiceLocation;
16 import it.imolinfo.jbi4cics.service.ServiceContext;
17
18 import javax.naming.InitialContext;
19 import javax.naming.NamingException;
20 import javax.resource.ResourceException;
21 import javax.resource.cci.Connection;
22 import javax.resource.cci.ConnectionFactory;
23 import javax.resource.cci.ConnectionSpec;
24 import javax.resource.cci.Interaction;
25 import javax.resource.cci.InteractionSpec;
26 import javax.resource.cci.Record;
27 import it.imolinfo.jbi4cics.Logger;
28 import it.imolinfo.jbi4cics.LoggerFactory;
29
30
31
32
33
34 public abstract class JCAAbstractConnectionManager implements ConnectionManager {
35
36
37
38
39
40 private static final Logger LOG
41 = LoggerFactory.getLogger(JCAAbstractConnectionManager.class);
42
43 ConnectionFactory connectionFactory = null;
44
45 public JCAAbstractConnectionManager() {
46 super();
47 }
48
49
50
51
52 public void handleCall(ServiceContext serviceContext) throws ConnectionException{
53 Connection connection=null;
54 Interaction interaction=null;
55 if (connectionFactory == null) {
56 lookupConnectionFactory(serviceContext);
57 }
58 try{
59 ConnectionSpec connectionSpec = createConnectionSpec(serviceContext);
60 connection = createConnection(serviceContext,connectionSpec);
61 interaction=createInteraction(connection);
62 InteractionSpec interactionSpec=createInteractionSpec(serviceContext);
63
64 Record inputRecord=createInputRecord(serviceContext);
65 Record outputRecord=createOutputRecord(serviceContext);
66
67 try{
68 boolean result=interaction.execute(interactionSpec,inputRecord,outputRecord);
69 if (!result){
70 LOG.error("CIC000302_Wrong_execution_of_the_request");
71 throw new ConnectionException("CIC000302_Wrong_execution_of_the_request");
72 }
73 }
74 catch(ResourceException e){
75
76 LOG.error("CIC000303_Error_executing_the_request", new Object[] {e.getMessage()}, e);
77 throw new ConnectionException("CIC000303_Error_executing_the_request", new Object[] {e.getMessage()}, e);
78 }
79 Object outputMessage=createOutputMessage(serviceContext,outputRecord);
80 serviceContext.setOutputMessage(outputMessage);
81 }
82 finally {
83 releaseResources(connection,interaction);
84 }
85 }
86
87
88
89 protected void releaseResources(Connection connection, Interaction interaction) throws ConnectionException {
90 try {
91 if (interaction!=null){
92 interaction.close();
93 }
94 if (connection!=null){
95 connection.close();
96 }
97 }
98 catch (ResourceException e){
99 LOG.error("CIC000304_Error_releasing_resources", new Object[] {e.getMessage()}, e);
100 throw new ConnectionException("CIC000304_Error_releasing_resources", new Object[] {e.getMessage()}, e);
101 }
102 }
103
104 protected Interaction createInteraction(Connection connection) throws ConnectionException {
105 try {
106 Interaction interaction = connection.createInteraction();
107 return interaction;
108 }
109 catch (ResourceException e) {
110 LOG.error("CIC000305_Error_getting_interaction_from_JCA", new Object[] {e.getMessage()}, e);
111 throw new ConnectionException("CIC000305_Error_getting_interaction_from_JCA", new Object[] {e.getMessage()}, e);
112 }
113 }
114
115 protected Connection createConnection(ServiceContext serviceContext, ConnectionSpec connectionSpec) throws ConnectionException{
116
117 if (connectionFactory == null) {
118 lookupConnectionFactory(serviceContext);
119 }
120 try {
121
122 Connection connection = connectionFactory.getConnection(connectionSpec);
123 return connection;
124 }
125 catch (ResourceException e) {
126 LOG.error("CIC000306_Error_getting_connection_from_JCA", new Object[] {e.getMessage()}, e);
127 throw new ConnectionException("CIC000306_Error_getting_connection_from_JCA", new Object[] {e.getMessage()}, e);
128 }
129 }
130
131
132
133
134
135
136 private void lookupConnectionFactory(ServiceContext serviceContext) throws ConnectionException {
137 ServiceLocation serviceLocation=serviceContext.getServiceLocation();
138
139 if (connectionFactory == null) {
140
141 String jndiConnectionName = serviceLocation.getLocationName();
142
143 try {
144 InitialContext initialContext = new javax.naming.InitialContext();
145 connectionFactory = (ConnectionFactory)initialContext.lookup(jndiConnectionName);
146 }
147 catch (NamingException e) {
148 LOG.error("CIC000307_Error_retrieving_JCA_connection_factory", new Object[] {e.getMessage()}, e);
149 throw new ConnectionException("CIC000307_Error_retrieving_JCA_connection_factory", new Object[] {e.getMessage()}, e);
150 }
151 if (connectionFactory == null) {
152 LOG.error("CIC000308_Lookup_failed", new Object[] {jndiConnectionName});
153 throw new ConnectionException("CIC000308_Lookup_failed", new Object[] {jndiConnectionName});
154 }
155 }
156 }
157
158
159
160
161
162
163
164 public ClassLoader getConnectorClassLoader() throws ConnectionException {
165
166 if (connectionFactory == null) {
167 throw new ConnectionException("CIC000309_Null_connection_factory");
168 }
169 return connectionFactory.getClass().getClassLoader();
170 }
171
172
173
174
175
176
177
178 protected abstract Object createOutputMessage(ServiceContext serviceContext, Record outputRecord) throws ConnectionException;
179
180
181
182
183
184
185 protected abstract InteractionSpec createInteractionSpec(ServiceContext serviceContext) throws ConnectionException;
186
187
188
189
190
191
192 protected abstract Record createOutputRecord(ServiceContext serviceContext) throws ConnectionException;
193
194
195
196
197
198
199 protected abstract Record createInputRecord(ServiceContext serviceContext) throws ConnectionException;
200
201
202
203
204
205
206 protected abstract ConnectionSpec createConnectionSpec(ServiceContext serviceContext) throws ConnectionException;
207
208 }