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    * 
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   * @author raffaele, marcopiraccini
33   */
34  public abstract class JCAAbstractConnectionManager implements ConnectionManager {
35  	   
36  
37      /**
38       * The logger for this class and its instances.
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  	/* (non-Javadoc)
50  	 * 	@see it.imolinfo.jbi4cics.connection.ConnectionManager#handleCall(it.imolinfo.jbi4cics.service.ServiceContext)
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              // creo record di input e output
64              Record inputRecord=createInputRecord(serviceContext);
65              Record outputRecord=createOutputRecord(serviceContext);      
66              // TODO outputRecord.setCommareaLength(interactionSpec.getReplyLength());
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                  //TODO probably, we should manage the exception...
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         // per prima cosa ottengo la service location
117         if (connectionFactory == null) {
118             lookupConnectionFactory(serviceContext);    
119         }
120         try {
121             // TODO vedere se servono le connectio properties
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      * loads the <code>ConnectionFactory</code> using the configured JNDI name.  
133      * @param serviceContext    The service context
134      * @throws ConnectionException    The connection exception
135      */
136     private void lookupConnectionFactory(ServiceContext serviceContext) throws ConnectionException {
137         ServiceLocation serviceLocation=serviceContext.getServiceLocation();
138 
139         if (connectionFactory == null) {
140             // Gets the JNDI name
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      * Return the <code>ConnectionFactory</code> class loader.
161      * @return    The class loader
162      * @throws ConnectionException    The connection exception
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      * @param serviceContext    The service context
174      * @param outputRecord    The output record
175      * @return    The created output message
176      * @throws ConnectionException    The connection exception
177      */
178     protected abstract Object createOutputMessage(ServiceContext serviceContext, Record outputRecord) throws ConnectionException;
179 
180     /**
181      * @param serviceContext    The service context
182      * @throws ConnectionException    The connection exception
183      * @return InteractionSpec Iteraction spec
184      */  
185     protected abstract InteractionSpec createInteractionSpec(ServiceContext serviceContext) throws ConnectionException;
186 
187     /**
188      * @param serviceContext    The service context
189      * @throws ConnectionException    The connection exception
190      * @return Record    The output record created
191      */  
192     protected abstract Record createOutputRecord(ServiceContext serviceContext) throws ConnectionException;
193 
194     /**
195      * @param serviceContext    The service context
196      * @throws ConnectionException    The connection exception
197      * @return Record    The input record created
198      */  
199     protected abstract Record createInputRecord(ServiceContext serviceContext) throws ConnectionException;
200 
201     /**
202      * @param serviceContext    The service context
203      * @throws ConnectionException    The connection exception
204      * @return ConnectionSpec     The connection spec
205      */  
206     protected abstract ConnectionSpec createConnectionSpec(ServiceContext serviceContext) throws ConnectionException; 
207 
208 }