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