Coverage Report - org.mule.providers.jbi.JbiMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
JbiMessageReceiver
0%
0/34
0%
0/3
1.769
JbiMessageReceiver$MessageExchangeWorker
0%
0/20
0%
0/2
1.769
 
 1  
 /*
 2  
  * $Id: JbiMessageReceiver.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.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  0
 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  0
         super(connector, component, endpoint);
 54  0
         name = component.getDescriptor().getName() + ".jbiReceiver";
 55  0
         this.connector = (JbiConnector)connector;
 56  0
         context = this.connector.getComponentContext();
 57  0
         deliveryChannel = this.connector.getDeliveryChannel();
 58  0
     }
 59  
 
 60  
     protected void doDispose()
 61  
     {
 62  
         // template method
 63  0
     }
 64  
 
 65  
     public void doConnect() throws Exception
 66  
     {
 67  
         // nothing to do
 68  0
     }
 69  
 
 70  
     public void doDisconnect() throws Exception
 71  
     {
 72  
         // nothing to do
 73  0
     }
 74  
 
 75  
     public void doStart() throws UMOException
 76  
     {
 77  
         try
 78  
         {
 79  0
             getWorkManager().scheduleWork(this);
 80  
         }
 81  0
         catch (WorkException e)
 82  
         {
 83  0
             throw new LifecycleException(CoreMessages.failedToStart(name), e, this);
 84  0
         }
 85  0
     }
 86  
 
 87  
     public void doStop() throws UMOException
 88  
     {
 89  
         // nothing to do
 90  0
     }
 91  
 
 92  
     public void release()
 93  
     {
 94  
         // nothing to do
 95  0
     }
 96  
 
 97  
     // TODO This receive code should be separated out to pluggable invocation
 98  
     // strategies
 99  
 
 100  
     public void run()
 101  
     {
 102  0
         while (connector.isStarted())
 103  
         {
 104  
             try
 105  
             {
 106  0
                 final MessageExchange me = deliveryChannel.accept();
 107  0
                 if (me != null)
 108  
                 {
 109  0
                     getWorkManager().scheduleWork(new MessageExchangeWorker(me));
 110  
                 }
 111  
             }
 112  0
             catch (Exception e)
 113  
             {
 114  0
                 handleException(e);
 115  0
             }
 116  
         }
 117  0
     }
 118  
 
 119  
     private class MessageExchangeWorker implements Work
 120  
     {
 121  
         private MessageExchange me;
 122  
 
 123  
         public MessageExchangeWorker(MessageExchange me)
 124  0
         {
 125  0
             this.me = me;
 126  0
         }
 127  
 
 128  
         public void release()
 129  
         {
 130  
             // nothing to do
 131  0
         }
 132  
 
 133  
         public void run()
 134  
         {
 135  
             try
 136  
             {
 137  
                 try
 138  
                 {
 139  0
                     NormalizedMessage nm = me.getMessage("IN");
 140  0
                     if (nm != null)
 141  
                     {
 142  0
                         UMOMessage response = routeMessage(new MuleMessage(connector.getMessageAdapter(nm)));
 143  0
                         if (response != null)
 144  
                         {
 145  0
                             NormalizedMessage nmResposne = me.createMessage();
 146  0
                             JbiUtils.populateNormalizedMessage(response, nmResposne);
 147  0
                             me.setMessage(nmResposne, "OUT");
 148  
                         }
 149  
                     }
 150  
                     else
 151  
                     {
 152  0
                         logger.debug("'IN' message on exchange was not set");
 153  
                     }
 154  
 
 155  0
                     done(me);
 156  
                 }
 157  0
                 catch (MessagingException e)
 158  
                 {
 159  0
                     error(me, e);
 160  0
                 }
 161  
             }
 162  0
             catch (Exception e)
 163  
             {
 164  0
                 handleException(e);
 165  0
             }
 166  0
         }
 167  
     }
 168  
 
 169  
     protected void error(MessageExchange me, Exception e) throws MessagingException
 170  
     {
 171  0
         if (e instanceof Fault)
 172  
         {
 173  0
             me.setFault((Fault)e);
 174  
         }
 175  
         else
 176  
         {
 177  0
             me.setError(e);
 178  
         }
 179  0
         me.setStatus(ExchangeStatus.ERROR);
 180  0
         deliveryChannel.send(me);
 181  0
     }
 182  
 
 183  
     protected void done(MessageExchange me) throws MessagingException
 184  
     {
 185  0
         me.setStatus(ExchangeStatus.DONE);
 186  0
         deliveryChannel.send(me);
 187  0
     }
 188  
 }