View Javadoc

1   /*
2    * The contents of this file are subject to the terms
3    * of the Common Development and Distribution License
4    * (the "License").  You may not use this file except
5    * in compliance with the License.
6    *
7    * You can obtain a copy of the license at
8    * https://open-esb.dev.java.net/public/CDDLv1.0.html.
9    * See the License for the specific language governing
10   * permissions and limitations under the License.
11   *
12   * When distributing Covered Code, include this CDDL
13   * HEADER in each file and include the License file at
14   * https://open-esb.dev.java.net/public/CDDLv1.0.html.
15   * If applicable add the following below this CDDL HEADER,
16   * with the fields enclosed by brackets "[]" replaced with
17   * your own identifying information: Portions Copyright
18   * [year] [name of copyright owner]
19   */
20  
21  /*
22   * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
23   */
24  
25  /*
26   * InOutProviderMessageExchangeHandler.java
27   *
28   */
29  
30  package it.imolinfo.jbi4ejb.jbi.component.runtime;
31  
32  import it.imolinfo.jbi4ejb.Logger;
33  import it.imolinfo.jbi4ejb.LoggerFactory;
34  import it.imolinfo.jbi4ejb.jbi.Messages;
35  
36  import javax.jbi.messaging.Fault;
37  import javax.jbi.messaging.InOut;
38  import javax.jbi.messaging.MessageExchange;
39  import javax.jbi.messaging.MessagingException;
40  import javax.jbi.messaging.NormalizedMessage;
41  
42  /**
43   * This class implements the InOut Message exchange handler implemenation for the
44   * Service Provider.
45   *
46   * It does a synchronous message exchange processing where it invokes the service
47   * and wait until the service processes the message and returns the response message.
48   * It then return out or fault message to consumer and also waits until the consumer
49   * returns the DONE status.
50   *
51   * Extended class should implement the actual invocation of the service operation by
52   * implementing the abstract method invokeInOutOperation of this class.
53   *
54   * @author Sun Microsystems, Inc.
55   */
56  public abstract class InOutProviderMessageExchangeHandler extends AbstractMessageExchangeHandler {
57      
58      public static final long SEND_SYNC_TIMEOUT = 60000;
59     
60      /** The logger. */
61      private static final Logger LOG = LoggerFactory.getLogger(InOutProviderMessageExchangeHandler.class);    
62      private static final Messages MESSAGES = Messages.getMessages(InOutProviderMessageExchangeHandler.class);   
63      
64      /** Creates a new instance of InOutProviderMessageExchangeHandler */
65      public InOutProviderMessageExchangeHandler() {
66          super();
67      }
68      
69      protected void validateMessageExchange() throws MessagingException {
70          
71          MessageExchange msgExchange = this.getMessageExchange();
72          
73          if ( this.getMessageExchange() == null ) {
74          	String msg=MESSAGES.getString("EJB000215_MessageExchange_Object_null_in_DefaultMessageExchageHandler");
75              LOG.error(msg);
76              throw new MessagingException(msg);  
77          }
78          
79          if ( MessageExchange.Role.CONSUMER.equals(msgExchange.getRole()) ) {
80  
81          	String msg=MESSAGES.getString("EJB000217_MessageExchange_Handler_can_not_have_MessageExchange_with_CONSUMER_Role");
82              LOG.error(msg);
83              throw new MessagingException(msg);  
84          }
85          
86          if (!(msgExchange instanceof InOut) ) {
87  
88          	String msg=MESSAGES.getString("EJB000218_InOut_Message_Exchange_Handler_MessageExchange_object_should_be_instance");
89              LOG.error(msg);
90              throw new MessagingException(msg);
91          }
92      }
93      
94      protected void processFault(Fault fault) {
95  
96      	LOG.error("EJB000219_processFault");
97      }
98      
99      protected void processDone() {
100         RuntimeHelper.logVerbose("InOut Message Exchange Provider handler received DONE : END of service invocation");
101     }
102     
103     protected void processError(Exception ex) {
104 
105     	LOG.error("EJB000220_InOut_Message_Exchange_Provider_handler_received_Error");
106 
107     	LOG.error("EJB000221_processError", new Object[] {ex});
108     }
109     
110     protected final void processMessage() {
111         
112         InOut inOutMX = (InOut) this.getMessageExchange();
113         
114         NormalizedMessage inMsg = inOutMX.getInMessage();
115         if ( inMsg == null ) {
116             this.sendError(new MessagingException("InOut Provider MessageExchange received null In Message"));
117             return;
118         }
119         
120         handleInMessage(inMsg);
121     }
122     
123     private void handleInMessage(NormalizedMessage inMsg) {
124         
125         Fault fault = null;
126         Exception processingEx = null;
127         NormalizedMessage outMsg = null;
128         boolean result = false;
129         
130         InOut inOutExchange = (InOut) this.getMessageExchange();
131         try {
132             // create out and fault messages
133             outMsg = inOutExchange.createMessage();
134             fault = inOutExchange.createFault();
135             // invoke operation
136             result = invokeOperation(inMsg, outMsg, fault);
137         } catch (Exception ex) {
138             processingEx = ex;
139             ex.printStackTrace();
140         } finally {
141             try {
142                 if ( result == true ) {
143                     // operation success
144                     this.sendOutMessage(outMsg);
145                 } else {
146                     // operation failed or some exception happened.
147                     if ( processingEx != null ) {
148                         // set fault content with exception msg
149                         this.sendFault(processingEx);
150                     } else {
151                         // fault content has been set by the operation.
152                         this.sendFault(fault);
153                     }
154                 }
155             } catch (MessagingException ex) {
156                 ex.printStackTrace();
157                 this.sendError(ex);
158                 return;
159             }
160         }
161         // process the message exchange again after sending the fault or out message to
162         // process the done or error received.
163         this.processMessageExchange();
164     }
165     
166     public void sendFault(Fault fault) throws MessagingException {
167         InOut inOutMX = (InOut) this.getMessageExchange();
168         inOutMX.setFault(fault);
169         boolean sent = false;
170         try {
171             sent = this.getDeliveryChannel().sendSync(inOutMX, SEND_SYNC_TIMEOUT);
172             if (!sent) {
173                 inOutMX.setError(new MessagingException("InOutProvider ME Handler unable to send out message"));
174             }
175         } catch (Exception ex) {
176         	
177         	LOG.error("EJB000222_sendFault");
178             inOutMX.setError(ex);
179         }
180     }
181     
182     private void sendOutMessage(NormalizedMessage outMsg) throws MessagingException {
183         
184         InOut inOutMX = (InOut) this.getMessageExchange();
185         inOutMX.setOutMessage(outMsg);
186         boolean sent = false;
187         try {
188             sent = this.getDeliveryChannel().sendSync(inOutMX, SEND_SYNC_TIMEOUT);
189             if (!sent) {
190                 inOutMX.setError(new MessagingException("InOutProvider ME Handler unable to send out message"));
191             }
192         } catch (Exception ex) {
193         	LOG.error("EJB000223_sendOutMessage");
194         	inOutMX.setError(ex);
195         }
196     }
197     
198     protected abstract boolean invokeOperation(NormalizedMessage inMsg, NormalizedMessage outMsg, Fault fault) throws MessagingException;
199     
200 }