View Javadoc

1   /*
2    * $Id: JbiMessageDispatcher.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.jbi;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.providers.AbstractMessageDispatcher;
15  import org.mule.umo.UMOEvent;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.endpoint.UMOImmutableEndpoint;
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.messaging.NormalizedMessage;
26  
27  /**
28   * <code>TcpMessageDispatcher</code> will send transformed mule events over tcp.
29   * 
30   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
31   * @version $Revision: 7976 $
32   */
33  
34  public class JbiMessageDispatcher extends AbstractMessageDispatcher
35  {
36      private JbiConnector connector;
37  
38      public JbiMessageDispatcher(UMOImmutableEndpoint endpoint)
39      {
40          super(endpoint);
41          this.connector = (JbiConnector)endpoint.getConnector();
42      }
43  
44      protected void doDispose()
45      {
46          // nothing to do
47      }
48  
49      protected void doDispatch(UMOEvent event) throws Exception
50      {
51          InOnly exchange = connector.getExchangeFactory().createInOnlyExchange();
52          NormalizedMessage message = exchange.createMessage();
53          exchange.setInMessage(message);
54          JbiUtils.populateNormalizedMessage(event.getMessage(), message);
55          done(exchange);
56      }
57  
58      protected UMOMessage doSend(UMOEvent event) throws Exception
59      {
60          InOut exchange = connector.getExchangeFactory().createInOutExchange();
61          NormalizedMessage message = exchange.createMessage();
62          exchange.setInMessage(message);
63          JbiUtils.populateNormalizedMessage(event.getMessage(), message);
64          NormalizedMessage nm = exchange.getOutMessage();
65          UMOMessage response = null;
66          if (nm != null)
67          {
68              response = new MuleMessage(connector.getMessageAdapter(nm));
69          }
70          done(exchange);
71          return response;
72      }
73  
74      /**
75       * Make a specific request to the underlying transport
76       * 
77       * @param endpoint the endpoint to use when connecting to the resource
78       * @param timeout the maximum time the operation should block before returning.
79       *            The call should return immediately if there is data available. If
80       *            no data becomes available before the timeout elapses, null will be
81       *            returned
82       * @return the result of the request wrapped in a UMOMessage object. Null will be
83       *         returned if no data was avaialable
84       * @throws Exception if the call to the underlying protocal cuases an exception
85       */
86      protected UMOMessage doReceive(long timeout) throws Exception
87      {
88          throw new UnsupportedOperationException("doReceive");
89      }
90  
91      protected void error(MessageExchange me, Fault fault) throws MessagingException
92      {
93          me.setFault(fault);
94          me.setStatus(ExchangeStatus.ERROR);
95          connector.getDeliveryChannel().send(me);
96      }
97  
98      protected void done(MessageExchange me) throws MessagingException
99      {
100         me.setStatus(ExchangeStatus.DONE);
101         connector.getDeliveryChannel().send(me);
102     }
103 
104     protected void doConnect() throws Exception
105     {
106         // no op
107     }
108 
109     protected void doDisconnect() throws Exception
110     {
111         // no op
112     }
113 
114 }