View Javadoc

1   /*
2    * $Id: JbiMessageDispatcher.java 7963 2007-08-21 08:53:15Z 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  public class JbiMessageDispatcher extends AbstractMessageDispatcher
28  {
29      private JbiConnector connector;
30  
31      public JbiMessageDispatcher(UMOImmutableEndpoint endpoint)
32      {
33          super(endpoint);
34          this.connector = (JbiConnector)endpoint.getConnector();
35      }
36  
37      protected void doDispose()
38      {
39          // nothing to do
40      }
41  
42      protected void doDispatch(UMOEvent event) throws Exception
43      {
44          InOnly exchange = connector.getExchangeFactory().createInOnlyExchange();
45          NormalizedMessage message = exchange.createMessage();
46          exchange.setInMessage(message);
47          JbiUtils.populateNormalizedMessage(event.getMessage(), message);
48          done(exchange);
49      }
50  
51      protected UMOMessage doSend(UMOEvent event) throws Exception
52      {
53          InOut exchange = connector.getExchangeFactory().createInOutExchange();
54          NormalizedMessage message = exchange.createMessage();
55          exchange.setInMessage(message);
56          JbiUtils.populateNormalizedMessage(event.getMessage(), message);
57          NormalizedMessage nm = exchange.getOutMessage();
58          UMOMessage response = null;
59          if (nm != null)
60          {
61              response = new MuleMessage(connector.getMessageAdapter(nm));
62          }
63          done(exchange);
64          return response;
65      }
66  
67      /**
68       * Make a specific request to the underlying transport
69       * 
70       * @param endpoint the endpoint to use when connecting to the resource
71       * @param timeout the maximum time the operation should block before returning.
72       *            The call should return immediately if there is data available. If
73       *            no data becomes available before the timeout elapses, null will be
74       *            returned
75       * @return the result of the request wrapped in a UMOMessage object. Null will be
76       *         returned if no data was avaialable
77       * @throws Exception if the call to the underlying protocal cuases an exception
78       */
79      protected UMOMessage doReceive(long timeout) throws Exception
80      {
81          throw new UnsupportedOperationException("doReceive");
82      }
83  
84      protected void error(MessageExchange me, Fault fault) throws MessagingException
85      {
86          me.setFault(fault);
87          me.setStatus(ExchangeStatus.ERROR);
88          connector.getDeliveryChannel().send(me);
89      }
90  
91      protected void done(MessageExchange me) throws MessagingException
92      {
93          me.setStatus(ExchangeStatus.DONE);
94          connector.getDeliveryChannel().send(me);
95      }
96  
97      protected void doConnect() throws Exception
98      {
99          // no op
100     }
101 
102     protected void doDisconnect() throws Exception
103     {
104         // no op
105     }
106 
107 }