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   
9   package it.imolinfo.jbi4corba.jbi.component.runtime;
10  
11  import java.io.StringReader;
12  import javax.jbi.messaging.DeliveryChannel;
13  import javax.jbi.messaging.ExchangeStatus;
14  import javax.jbi.messaging.Fault;
15  import javax.jbi.messaging.MessageExchange;
16  import javax.jbi.messaging.MessagingException;
17  
18  /**
19   * This class is an abstract implemenation of the MessageExchangeHandler which
20   * provides the base implemenation of the ME processing and provides hooks to
21   * extended classes to implement component specific processing.
22   *
23   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
24   */
25  public abstract class AbstractMessageExchangeHandler implements MessageExchangeHandler {
26      
27      public static String IN_MESSAGE = "in";
28      public static String OUT_MESSAGE = "out";
29      
30      private MessageExchange mMessageExchange;
31      
32      /** Creates a new instance of AbstractMessageExchangeHandler */
33      protected AbstractMessageExchangeHandler() {
34          this.mMessageExchange = null;
35      }
36      
37      protected final DeliveryChannel getDeliveryChannel() {
38          return RuntimeHelper.getDeliveryChannel();
39      }
40      
41      public final MessageExchange getMessageExchange() {
42          return this.mMessageExchange;
43      }
44      
45      public final void setMessageExchange(MessageExchange msgExchange) {
46          this.mMessageExchange = msgExchange;
47      }
48      
49      protected final void send() throws MessagingException {
50          this.getDeliveryChannel().send(this.mMessageExchange);
51      }
52      
53      protected final void sendDone() throws MessagingException {
54          this.mMessageExchange.setStatus(ExchangeStatus.DONE);
55          this.getDeliveryChannel().send(this.mMessageExchange);
56      }
57      
58      protected final void sendFault() throws MessagingException {
59          Fault fault = this.mMessageExchange.createFault();
60          sendFault(fault);
61      }
62      
63      protected final void sendFault(Exception ex) throws MessagingException {
64          Fault fault = this.mMessageExchange.createFault();
65          if ( ex != null ) {
66              String xmlText = RuntimeHelper.getExceptionAsXmlText(ex);
67              fault.setContent(RuntimeHelper.createDOMSource(new StringReader(xmlText)));
68          }
69          sendFault(fault);
70      }
71      
72      protected void sendFault(Fault fault) throws MessagingException {
73          this.mMessageExchange.setFault(fault);
74          this.getDeliveryChannel().send(this.mMessageExchange);
75      }
76      
77      
78      protected final void sendError(Exception ex) {
79          try {
80              this.mMessageExchange.setError(ex);
81              this.getDeliveryChannel().send(this.mMessageExchange);
82          } catch (MessagingException msgEx) {
83              msgEx.printStackTrace();
84          }
85      }
86      
87      protected abstract void validateMessageExchange() throws MessagingException;
88      protected abstract void processError(Exception ex);
89      protected abstract void processDone();
90      protected abstract void processMessage();
91      protected abstract void processFault(Fault fault);
92      
93      private void processActive() {
94          Fault fault = this.getMessageExchange().getFault();
95          if ( fault != null ) {
96              processFault(fault);
97          } else {
98              processMessage();
99          }
100     }
101     
102     public final void processMessageExchange() {
103         try {
104             validateMessageExchange();
105         } catch (MessagingException ex) {
106             ex.printStackTrace();
107             if ( this.getMessageExchange() != null ) {
108                 sendError(ex);
109             }
110             return;
111         }
112         
113         MessageExchange msgExchange = this.getMessageExchange();
114         ExchangeStatus status = msgExchange.getStatus();
115         
116         if (ExchangeStatus.ACTIVE.equals(status) ) {
117             processActive();
118         } else if (ExchangeStatus.DONE.equals(status) ) {
119             processDone();
120         } else if (ExchangeStatus.ERROR.equals(status) ) {
121             processError(msgExchange.getError());
122         }
123     }
124     
125     public final void run() {
126         try {
127             this.processMessageExchange();
128         } catch (Exception ex) {
129             ex.printStackTrace();
130         }
131     }
132     
133 }