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   package it.imolinfo.jbi4ejb.jbi.component;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  import it.imolinfo.jbi4ejb.jbi.Messages;
13  import it.imolinfo.jbi4ejb.jbi.component.runtime.AbstractMessageExchangeHandler;
14  import it.imolinfo.jbi4ejb.jbi.component.runtime.RuntimeHelper;
15  import it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint;
16  import it.imolinfo.jbi4ejb.processor.ExchangeProcessor;
17  
18  import javax.jbi.messaging.Fault;
19  import javax.jbi.messaging.InOut;
20  import javax.jbi.messaging.MessageExchange;
21  import javax.jbi.messaging.MessagingException;
22  
23  /**
24   * Message Exchange handler. Some of this code is taken from the <code>InOutProviderMessageExchangeHandler</code>
25   * class.
26   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a> 
27   */
28  public class Jbi4EjbMessageExchangeHandler extends AbstractMessageExchangeHandler {       
29      
30      /** The Constant SEND_SYNC_TIMEOUT. */
31      public static final long SEND_SYNC_TIMEOUT = 60000;
32  
33      /** The logger. */
34      private static final Logger LOG = 
35      	LoggerFactory.getLogger(Jbi4EjbMessageExchangeHandler.class);
36      private static final Messages MESSAGES = 
37      	Messages.getMessages(Jbi4EjbMessageExchangeHandler.class);
38      
39      /** The service unit manager. */
40      private Jbi4EjbSUManager suManager = null;
41      
42      /**
43       * Instantiates a new jbi4 ejb message exchange handler.
44       * 
45       * @param suManager
46       *          The service unit manager
47       */
48      public Jbi4EjbMessageExchangeHandler(Jbi4EjbSUManager suManager) {
49          super();
50          this.suManager = suManager;
51      }
52          
53      /**
54       * Process the message.
55       */
56      protected final void processMessage() {
57          
58          InOut inOutMX = (InOut) this.getMessageExchange();        
59                               
60          Jbi4EjbEndpoint ejbEndpoint = 
61          	suManager.getStartedEndpoint(inOutMX.getEndpoint());
62                  
63          LOG.info("EJB000102_Received_message_invocation_for_endpoint", 
64          		new Object[]{ejbEndpoint.getEndpointName()});
65          ExchangeProcessor processor = ejbEndpoint.getExchangeProcessor();
66          
67          // process the message
68          processor.process(inOutMX);                
69      }
70            
71      /**
72       * Validates the <code>MessageExchange</code>.
73       * 
74       * @throws MessagingException
75       *          If something go wrong.
76       */
77      protected void validateMessageExchange() throws MessagingException {
78          
79          MessageExchange msgExchange = this.getMessageExchange();
80          
81          if ( this.getMessageExchange() == null ) {
82          	String msg=
83          		MESSAGES.getString("EJB000103_MessageExchange_Object_null_in_MessageExchageHandler");
84              LOG.error(msg);
85              throw new MessagingException(msg);   
86          }
87          
88          if ( MessageExchange.Role.CONSUMER.equals(msgExchange.getRole()) ) {
89          	String msg=
90          		MESSAGES.getString("EJB000104_Provider_Message_Exchange_Handler_can_not_have_MessageExchange_with_ONSUMER_Role");
91              LOG.error(msg);
92              throw new MessagingException(msg); 
93          }        
94          if (!(msgExchange instanceof InOut) ) {
95          	String msg=
96          		MESSAGES.getString("EJB000105_InOut_Message_Exchange_Handler_MessageExchange_object_should_be_instance_of_javax.jbi.messaging.InOut");
97              LOG.error(msg);
98              throw new MessagingException(msg);
99          }
100     }
101     
102     /**
103      * Process a <code>Fault</code>.
104      * 
105      * @param fault
106      *          The fault
107      */
108     protected void processFault(Fault fault) {
109     	LOG.error("EJB000106_InOut_Message_Exchange_Provider_received_FAULT");
110     }
111     
112     /**
113      * Process Done.
114      */
115     protected void processDone() {
116         RuntimeHelper.logVerbose("InOut Message Exchange Provider handler received DONE : END of service invocation");
117     }
118             
119     /**
120      * Process an Error.
121      * 
122      * @param ex
123      *          The eception to process
124      */
125     protected void processError(Exception ex) {
126     	LOG.error("EJB000107_InOut_Message_Exchange_Provider_received_error");
127     	LOG.error("EJB000108_Error_during_Process", ex);
128     }
129     
130 }