View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 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.jbi4corba.jbi.component;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.jbi.Messages;
13  import it.imolinfo.jbi4corba.jbi.component.runtime.AbstractMessageExchangeHandler;
14  import it.imolinfo.jbi4corba.jbi.component.runtime.RuntimeHelper;
15  import it.imolinfo.jbi4corba.jbi.endpoint.Jbi4CorbaEndpoint;
16  import it.imolinfo.jbi4corba.jbi.endpoint.Jbi4CorbaSFServiceEndpoint;
17  import it.imolinfo.jbi4corba.jbi.processor.ExchangeProcessor;
18  
19  import javax.jbi.messaging.ExchangeStatus;
20  import javax.jbi.messaging.Fault;
21  import javax.jbi.messaging.InOnly;
22  import javax.jbi.messaging.InOut;
23  import javax.jbi.messaging.MessageExchange;
24  import javax.jbi.messaging.MessagingException;
25  import javax.jbi.servicedesc.ServiceEndpoint;
26  
27  
28  /**
29   * Message Exchange handler. Some of this code is taken from the <code>InOutProviderMessageExchangeHandler</code>
30   * class.
31   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a> 
32   */
33  public class Jbi4CorbaMessageExchangeHandler extends AbstractMessageExchangeHandler {       
34      
35      /** The Constant SEND_SYNC_TIMEOUT. */
36      public static final long SEND_SYNC_TIMEOUT = 60000;
37  
38      /** The logger. */
39      private static final Logger LOG = LoggerFactory.getLogger(Jbi4CorbaMessageExchangeHandler.class);
40      private static final Messages MESSAGES = 
41      	Messages.getMessages(Jbi4CorbaMessageExchangeHandler.class);
42      
43      /** The service unit manager. */
44      private Jbi4CorbaSUManager suManager = null;
45      
46      /**
47       * Instantiates a new jbi4 ejb message exchange handler.
48       * 
49       * @param suManager
50       *          The service unit manager
51       */
52      public Jbi4CorbaMessageExchangeHandler(Jbi4CorbaSUManager suManager) {
53          super();
54          this.suManager = suManager;
55      }
56          
57      /**
58       * Process the message.
59       */
60      protected final void processMessage() {
61          
62          MessageExchange me = this.getMessageExchange();
63                               
64          //me.setProperty("EPR_IOR", "");
65          ServiceEndpoint sep =  me.getEndpoint();
66          
67          Jbi4CorbaSFServiceEndpoint ep;
68          
69          if(sep instanceof Jbi4CorbaSFServiceEndpoint){
70              ep= (Jbi4CorbaSFServiceEndpoint) sep;
71              me.setProperty("EPR_IOR", ep.getIor());
72              String msg=MESSAGES.getString("CRB000230_Received_message_invocation_for_endpoint");
73              LOG.debug(msg);
74          }
75             
76          Jbi4CorbaEndpoint corbaEndpoint = suManager.getStartedEndpoint(me.getEndpoint());
77          
78         
79          
80          java.util.logging.Logger.getLogger("com.sun.EnterContext").fine("JBI4Corba-" + corbaEndpoint.getSuName());
81          
82          LOG.debug("CRB000208_Received_message_invocation_for_endpoint", 
83          		new Object[]{corbaEndpoint.getEndpointName()});
84          
85          ExchangeProcessor processor = corbaEndpoint.getExchangeProcessor();
86          
87          // process the message        
88          processor.process(me);                
89          java.util.logging.Logger.getLogger("com.sun.ExitContext").fine("JBI4Corba-"+corbaEndpoint.getSuName());
90      }
91            
92      /**
93       * Validates the <code>MessageExchange</code>.
94       * 
95       * @throws MessagingException
96       *          If something go wrong.
97       */
98      protected void validateMessageExchange() throws MessagingException {
99          
100         MessageExchange msgExchange = this.getMessageExchange();
101         LOG.debug("begin validation of exchange: "+msgExchange);
102 
103 
104         if ( this.getMessageExchange() == null ) {
105         	String msg=MESSAGES.getString("CRB000209_MessageExchange_Object_is_null_in_MessageExchageHandler");
106             LOG.error(msg);
107             throw new MessagingException(msg);
108 
109         }
110         
111         if ( MessageExchange.Role.CONSUMER.equals(msgExchange.getRole()) && !(msgExchange.getStatus()==ExchangeStatus.DONE) ) {
112         	String msg=MESSAGES.getString("CRB000210_Message_Exchange_Handler_can_not_have_MessageExchange_with_CONSUMER_Role");
113             LOG.error(msg);
114             throw new MessagingException(msg);
115         }        
116         
117         if ((!(msgExchange instanceof InOut)) && (!(msgExchange instanceof InOnly) )) {
118         	String msg=MESSAGES.getString("CRB000211_MessageExchange_shoul_be_instance_of_javax.jbi.messaging.InOut_or_of_javax.jbi.messaging.InOnly");
119             LOG.error(msg);
120             throw new MessagingException(msg);
121         }
122     }
123     
124     /**
125      * Process a <code>Fault</code>.
126      * 
127      * @param fault
128      *          The fault
129      */
130     protected void processFault(Fault fault) {
131 	Jbi4CorbaEndpoint corbaEndpoint = suManager.getStartedEndpoint(this.getMessageExchange().getEndpoint());
132         corbaEndpoint.getEndpointStatus().incrementReceivedErrors();
133         String msg=MESSAGES.getString("CRB000212_InOut_Message_exchange_provider_received_FAULT");
134         LOG.error(msg);
135     }
136     
137     /**
138      * Process Done.
139      */
140     protected void processDone() {
141         Jbi4CorbaEndpoint corbaEndpoint = suManager.getStartedEndpoint(this.getMessageExchange().getEndpoint());
142         corbaEndpoint.getEndpointStatus().incrementReceivedDones();    
143         LOG.debug("InOut Message Exchange Provider handler received DONE : END of service invocation");        
144     }
145             
146     /**
147      * Process an Error.
148      * 
149      * @param ex
150      *          The eception to process
151      */
152     protected void processError(Exception ex) {
153     	String msg=MESSAGES.getString("CRB000213_InOut_Message_Exchange_Provider_handler_received_Error");
154         LOG.error(msg,ex);
155 		RuntimeHelper.logError(msg + ex);
156 
157     }
158     
159 }