View Javadoc

1   /*
2    * $Id: JbiMessageReceiver.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.config.i18n.CoreMessages;
14  import org.mule.impl.MuleMessage;
15  import org.mule.providers.AbstractMessageReceiver;
16  import org.mule.umo.UMOComponent;
17  import org.mule.umo.UMOException;
18  import org.mule.umo.UMOMessage;
19  import org.mule.umo.endpoint.UMOEndpoint;
20  import org.mule.umo.lifecycle.InitialisationException;
21  import org.mule.umo.lifecycle.LifecycleException;
22  import org.mule.umo.provider.UMOConnector;
23  
24  import javax.jbi.component.ComponentContext;
25  import javax.jbi.messaging.DeliveryChannel;
26  import javax.jbi.messaging.ExchangeStatus;
27  import javax.jbi.messaging.Fault;
28  import javax.jbi.messaging.MessageExchange;
29  import javax.jbi.messaging.MessagingException;
30  import javax.jbi.messaging.NormalizedMessage;
31  import javax.resource.spi.work.Work;
32  import javax.resource.spi.work.WorkException;
33  
34  /**
35   * This is a JBI component that can receive events over Mule transports. It is an
36   * independent JBI component implementation that can be used in any JBI container,
37   * including but not limited to Mule JBI.
38   */
39  public class JbiMessageReceiver extends AbstractMessageReceiver implements Work
40  {
41  
42      protected ComponentContext context;
43  
44      protected JbiConnector connector;
45  
46      protected String name;
47  
48      private DeliveryChannel deliveryChannel;
49  
50      public JbiMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
51          throws InitialisationException
52      {
53          super(connector, component, endpoint);
54          name = component.getDescriptor().getName() + ".jbiReceiver";
55          this.connector = (JbiConnector)connector;
56          context = this.connector.getComponentContext();
57          deliveryChannel = this.connector.getDeliveryChannel();
58      }
59  
60      protected void doDispose()
61      {
62          // template method
63      }
64  
65      public void doConnect() throws Exception
66      {
67          // nothing to do
68      }
69  
70      public void doDisconnect() throws Exception
71      {
72          // nothing to do
73      }
74  
75      public void doStart() throws UMOException
76      {
77          try
78          {
79              getWorkManager().scheduleWork(this);
80          }
81          catch (WorkException e)
82          {
83              throw new LifecycleException(CoreMessages.failedToStart(name), e, this);
84          }
85      }
86  
87      public void doStop() throws UMOException
88      {
89          // nothing to do
90      }
91  
92      public void release()
93      {
94          // nothing to do
95      }
96  
97      // TODO This receive code should be separated out to pluggable invocation
98      // strategies
99  
100     public void run()
101     {
102         while (connector.isStarted())
103         {
104             try
105             {
106                 final MessageExchange me = deliveryChannel.accept();
107                 if (me != null)
108                 {
109                     getWorkManager().scheduleWork(new MessageExchangeWorker(me));
110                 }
111             }
112             catch (Exception e)
113             {
114                 handleException(e);
115             }
116         }
117     }
118 
119     private class MessageExchangeWorker implements Work
120     {
121         private MessageExchange me;
122 
123         public MessageExchangeWorker(MessageExchange me)
124         {
125             this.me = me;
126         }
127 
128         public void release()
129         {
130             // nothing to do
131         }
132 
133         public void run()
134         {
135             try
136             {
137                 try
138                 {
139                     NormalizedMessage nm = me.getMessage("IN");
140                     if (nm != null)
141                     {
142                         UMOMessage response = routeMessage(new MuleMessage(connector.getMessageAdapter(nm)));
143                         if (response != null)
144                         {
145                             NormalizedMessage nmResposne = me.createMessage();
146                             JbiUtils.populateNormalizedMessage(response, nmResposne);
147                             me.setMessage(nmResposne, "OUT");
148                         }
149                     }
150                     else
151                     {
152                         logger.debug("'IN' message on exchange was not set");
153                     }
154 
155                     done(me);
156                 }
157                 catch (MessagingException e)
158                 {
159                     error(me, e);
160                 }
161             }
162             catch (Exception e)
163             {
164                 handleException(e);
165             }
166         }
167     }
168 
169     protected void error(MessageExchange me, Exception e) throws MessagingException
170     {
171         if (e instanceof Fault)
172         {
173             me.setFault((Fault)e);
174         }
175         else
176         {
177             me.setError(e);
178         }
179         me.setStatus(ExchangeStatus.ERROR);
180         deliveryChannel.send(me);
181     }
182 
183     protected void done(MessageExchange me) throws MessagingException
184     {
185         me.setStatus(ExchangeStatus.DONE);
186         deliveryChannel.send(me);
187     }
188 }