View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
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   * @author raffaele, marcopiraccini
31   *
32   */
33  public class CICSConnectionManager extends JCACommareaBasedConnectionManager {  
34  
35      /**
36       * The logger for this class and its instances.
37       */
38      private static final Logger LOG
39              = LoggerFactory.getLogger(CICSConnectionManager.class);
40  
41      /**
42       * The responsible to translate localized messages.
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       * Inits and retruns a inited ConnectionSpec.
56       * @param serviceContext    The service context
57       * @throws ConnectionException    The connection exception
58       * @return ConnectionSpec    The connection spec
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          // ECIConnectionSpec eciConnectionSpec = new ECIConnectionSpec();
80  
81          /*
82           * property to set:
83           * username
84           * password
85           * see http://publib.boulder.ibm.com/infocenter/cicstg60/index.jsp 
86           */
87          if (eciConnectionSpec != null) {
88              // ((ECIConnectionSpec)eciConnectionSpec).setUserName(serviceContext.getAccount().getUsername());
89              // ((ECIConnectionSpec)eciConnectionSpec).setPassword(serviceContext.getAccount().getPassword());		
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      * Returns a correctly configured <code>InteractionSpce</code>.
103      * @param serviceContext    The service context
104      * @throws ConnectionException    The connection exception
105      * @return InteractionSpec    The interaction spec
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          * The properties to set are:
133          * timeout
134          * function name / program name
135          * tpn name
136          * tran name
137          * see http://publib.boulder.ibm.com/infocenter/cicstg60/index.jsp
138          */
139 
140         // eciInteractionSpec.setExecuteTimeout(cicsInteractionDescription.getTimeout());
141         invokeMethodWithOneIntParam(eciInteractionSpec, "setExecuteTimeout", cicsInteractionDescription.getTimeout());
142 
143         // eciInteractionSpec.setFunctionName(cicsInteractionDescription.getProgramName());
144         invokeMethodWithOneStringParam(eciInteractionSpec, "setFunctionName", cicsInteractionDescription.getProgramName());
145 
146         if (cicsInteractionDescription.isTpn()){
147             // eciInteractionSpec.setTPNName(cicsInteractionDescription.getTransactionName());
148             invokeMethodWithOneStringParam(eciInteractionSpec, "setTPNName", cicsInteractionDescription.getTransactionName());
149         } else {
150             // eciInteractionSpec.setTranName(cicsInteractionDescription.getTransactionName());
151             invokeMethodWithOneStringParam(eciInteractionSpec, "setTranName", cicsInteractionDescription.getTransactionName());        
152         }
153 
154         // le interazioni jbi4cics sono sono tutte sync send receive
155         // eciInteractionSpec.setInteractionVerb(InteractionSpec.SYNC_SEND_RECEIVE);
156         invokeMethodWithOneIntParam(eciInteractionSpec, "setInteractionVerb", InteractionSpec.SYNC_SEND_RECEIVE);      
157 
158         // setting della lunghezza delle commarea scambiate"Expected CommareaBeanMappingDescriptor, found: "+serviceContext.getInputMappingDescriptor().getClass()
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             //il CTG alloca solo una area di scambio quindi devo capire quale è la più grande e usare solo quella
171             //in realtà se si usa sync_send_receive o sync_send vale la CommareaLength, altrimenti con sync_receive vale la ReplyLength, 
172             //quindi commento il setting del reply length visto che siamo smepre in sync_send_receive 
173             if (inputMappingDescriptor.getBufferedLength()>=outputMappingDescriptor.getBufferedLength()){
174 
175                 // eciInteractionSpec.setCommareaLength(inputMappingDescriptor.getBufferedLength());
176                 //eciInteractionSpec.setReplyLength(inputMappingDescriptor.getBufferedLength());
177                 invokeMethodWithOneIntParam(eciInteractionSpec, "setCommareaLength", inputMappingDescriptor.getBufferedLength());
178 
179             }
180             else {
181                 // eciInteractionSpec.setCommareaLength(outputMappingDescriptor.getBufferedLength());
182                 //eciInteractionSpec.setReplyLength(outputMappingDescriptor.getBufferedLength());    
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      * Use reflection to invoke a void method with one <code>String</code> parameter.
195      * @param obj    The object
196      * @param methodName    The method name
197      * @param methodParam    The method param
198      * @throws ConnectionException    The connection exception
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             // On a InvocationTarget Exception, we should extract the internal Exception
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      * Use reflection to invoke a void method with one <code>int</code> parameter.
225      * @param obj    The object
226      * @param methodName    The method name
227      * @param methodParam    The method param
228      * @throws ConnectionException    The connection exception
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             // On a InvocationTarget Exception, we should extract the internal Exception
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 }