1
2
3
4
5
6
7
8
9 package it.imolinfo.jbi4cics.connection.jca.cics;
10
11 import it.imolinfo.jbi4cics.connection.jca.JCACommareaBasedConnectionManager;
12 import it.imolinfo.jbi4cics.exception.ConnectionException;
13 import it.imolinfo.jbi4cics.exception.FormatException;
14 import it.imolinfo.jbi4cics.messageformat.commarea.CommareaBeanMappingDescriptor;
15 import it.imolinfo.jbi4cics.service.ServiceContext;
16
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20
21 import javax.resource.cci.ConnectionSpec;
22 import javax.resource.cci.InteractionSpec;
23
24 import it.imolinfo.jbi4cics.Logger;
25 import it.imolinfo.jbi4cics.LoggerFactory;
26 import it.imolinfo.jbi4cics.jbi.Messages;
27
28
29
30
31
32
33 public class CICSConnectionManager extends JCACommareaBasedConnectionManager {
34
35
36
37
38 private static final Logger LOG
39 = LoggerFactory.getLogger(CICSConnectionManager.class);
40
41
42
43
44 private static final Messages MESSAGES
45 = Messages.getMessages(CICSConnectionManager.class);
46
47
48
49
50 public CICSConnectionManager() {
51 super();
52 }
53
54
55
56
57
58
59
60 protected ConnectionSpec createConnectionSpec(ServiceContext serviceContext)
61 throws ConnectionException {
62
63 ConnectionSpec eciConnectionSpec = null;
64
65 try {
66 eciConnectionSpec = (ConnectionSpec)
67 this.getConnectorClassLoader().loadClass("com.ibm.connector2.cics.ECIConnectionSpec").newInstance();
68 } catch (InstantiationException e) {
69 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
70 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
71 } catch (IllegalAccessException e) {
72 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
73 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
74 } catch (ClassNotFoundException e) {
75 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
76 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
77 }
78
79
80
81
82
83
84
85
86
87 if (eciConnectionSpec != null) {
88
89
90 invokeMethodWithOneStringParam(eciConnectionSpec, "setUserName", serviceContext.getAccount().getUsername());
91 invokeMethodWithOneStringParam(eciConnectionSpec, "setPassword", serviceContext.getAccount().getPassword());
92
93 } else {
94 LOG.error("CIC000404_Connection_spec_not_found");
95 throw new ConnectionException(MESSAGES.getString("CIC000404_Connection_spec_not_found"));
96 }
97 return eciConnectionSpec;
98
99 }
100
101
102
103
104
105
106
107 protected InteractionSpec createInteractionSpec(ServiceContext serviceContext) throws ConnectionException {
108
109 if (!(serviceContext.getInteractionDescription() instanceof CICSInteractionDescription)) {
110 LOG.error("CIC000405_Cics_interaction_description_not_found", serviceContext.getInteractionDescription().getClass());
111 throw new ConnectionException(MESSAGES.getString("CIC000405_Cics_interaction_description_not_found", new Object[] {serviceContext.getInteractionDescription().getClass()}));
112 }
113 CICSInteractionDescription cicsInteractionDescription=(CICSInteractionDescription)serviceContext.getInteractionDescription();
114
115 InteractionSpec eciInteractionSpec = null;
116
117 try {
118 eciInteractionSpec = (InteractionSpec)
119 this.getConnectorClassLoader().loadClass("com.ibm.connector2.cics.ECIInteractionSpec").newInstance();
120 } catch (InstantiationException e) {
121 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
122 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
123 } catch (IllegalAccessException e) {
124 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
125 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
126 } catch (ClassNotFoundException e) {
127 LOG.error("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
128 throw new ConnectionException("CIC000400_Error_istantiating_connection_spec", new Object[] {e.getMessage()}, e);
129 }
130
131
132
133
134
135
136
137
138
139
140
141 invokeMethodWithOneIntParam(eciInteractionSpec, "setExecuteTimeout", cicsInteractionDescription.getTimeout());
142
143
144 invokeMethodWithOneStringParam(eciInteractionSpec, "setFunctionName", cicsInteractionDescription.getProgramName());
145
146 if (cicsInteractionDescription.isTpn()){
147
148 invokeMethodWithOneStringParam(eciInteractionSpec, "setTPNName", cicsInteractionDescription.getTransactionName());
149 } else {
150
151 invokeMethodWithOneStringParam(eciInteractionSpec, "setTranName", cicsInteractionDescription.getTransactionName());
152 }
153
154
155
156 invokeMethodWithOneIntParam(eciInteractionSpec, "setInteractionVerb", InteractionSpec.SYNC_SEND_RECEIVE);
157
158
159 if (!(serviceContext.getInputMappingDescriptor() instanceof CommareaBeanMappingDescriptor)){
160 LOG.error("CIC000401_Expected_commarea_bean_mapping_descriptor", new Object[] {serviceContext.getInputMappingDescriptor().getClass()});
161 throw new ConnectionException("CIC000401_Expected_commarea_bean_mapping_descriptor", new Object[] {serviceContext.getInputMappingDescriptor().getClass()});
162 }
163 if (!(serviceContext.getOutputMappingDescriptor() instanceof CommareaBeanMappingDescriptor)){
164 LOG.error("CIC000401_Expected_commarea_bean_mapping_descriptor", new Object[] {serviceContext.getOutputMappingDescriptor().getClass()});
165 throw new ConnectionException("CIC000401_Expected_commarea_bean_mapping_descriptor", new Object[] {serviceContext.getOutputMappingDescriptor().getClass()});
166 }
167 CommareaBeanMappingDescriptor inputMappingDescriptor=(CommareaBeanMappingDescriptor)serviceContext.getInputMappingDescriptor();
168 CommareaBeanMappingDescriptor outputMappingDescriptor=(CommareaBeanMappingDescriptor)serviceContext.getOutputMappingDescriptor();
169 try {
170
171
172
173 if (inputMappingDescriptor.getBufferedLength()>=outputMappingDescriptor.getBufferedLength()){
174
175
176
177 invokeMethodWithOneIntParam(eciInteractionSpec, "setCommareaLength", inputMappingDescriptor.getBufferedLength());
178
179 }
180 else {
181
182
183 invokeMethodWithOneIntParam(eciInteractionSpec, "setCommareaLength", outputMappingDescriptor.getBufferedLength());
184 }
185 }
186 catch (FormatException e){
187 LOG.error("CIC000402_Error_setting_commarea_lengths", new Object[] {e.getMessage()}, e);
188 throw new ConnectionException("CIC000402_Error_setting_commarea_lengths", new Object[] {e.getMessage()}, e);
189 }
190 return eciInteractionSpec;
191 }
192
193
194
195
196
197
198
199
200 protected void invokeMethodWithOneStringParam(Object obj, String methodName, String methodParam)
201 throws ConnectionException {
202
203 Class[] parameterTypes = new Class[] {String.class};
204 Method method;
205 Object[] arguments = new Object[] {methodParam};
206 Class objClass = obj.getClass();
207 try {
208 method = objClass.getMethod(methodName, parameterTypes);
209 method.invoke(obj, arguments);
210 } catch (NoSuchMethodException e) {
211 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
212 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
213 } catch (IllegalAccessException e) {
214 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
215 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
216 } catch (InvocationTargetException e) {
217
218 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e.getCause());
219 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e.getCause());
220 }
221 }
222
223
224
225
226
227
228
229
230 protected void invokeMethodWithOneIntParam(Object obj, String methodName, int methodParam)
231 throws ConnectionException {
232
233 Class[] parameterTypes = new Class[] {int.class};
234 Method method;
235 Object[] arguments = new Object[] {methodParam};
236 Class objClass = obj.getClass();
237 try {
238 method = objClass.getMethod(methodName, parameterTypes);
239 method.invoke(obj, arguments);
240 } catch (NoSuchMethodException e) {
241 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
242 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
243 } catch (IllegalAccessException e) {
244 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
245 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e);
246 } catch (InvocationTargetException e) {
247
248 LOG.error("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e.getCause());
249 throw new ConnectionException("CIC000403_Method_invocation_error", new Object[] {methodName, objClass, methodParam}, e.getCause());
250 }
251 }
252
253 }