Coverage Report - org.mule.transport.ajax.AjaxMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
AjaxMessageDispatcher
0%
0/49
0%
0/34
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.ajax;
 8  
 
 9  
 import org.mule.api.MuleEvent;
 10  
 import org.mule.api.MuleMessage;
 11  
 import org.mule.api.endpoint.OutboundEndpoint;
 12  
 import org.mule.api.lifecycle.CreateException;
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.transport.AbstractMessageDispatcher;
 15  
 import org.mule.transport.NullPayload;
 16  
 import org.mule.transport.ajax.i18n.AjaxMessages;
 17  
 import org.mule.util.MapUtils;
 18  
 import org.mule.util.StringUtils;
 19  
 
 20  
 import org.apache.commons.collections.Buffer;
 21  
 import org.apache.commons.collections.buffer.BoundedFifoBuffer;
 22  
 import org.cometd.Channel;
 23  
 import org.cometd.Client;
 24  
 import org.mortbay.cometd.AbstractBayeux;
 25  
 
 26  
 /**
 27  
  * Will dispatch Mule events to ajax clients available in Bayeux that are listening to this endpoint.
 28  
  */
 29  
 public class AjaxMessageDispatcher extends AbstractMessageDispatcher implements BayeuxAware
 30  
 {
 31  
     protected AbstractBayeux bayeux;
 32  
 
 33  0
     protected boolean cacheMessages = false;
 34  
 
 35  0
     protected int messageCacheSize = 500;
 36  
 
 37  
     protected Buffer messageCache;
 38  
 
 39  
     protected String channel;
 40  
 
 41  
     protected Client client;
 42  
 
 43  
     public AjaxMessageDispatcher(OutboundEndpoint endpoint) throws CreateException
 44  
     {
 45  0
         super(endpoint);
 46  0
         cacheMessages = MapUtils.getBoolean(endpoint.getProperties(), "cacheMessages", false);
 47  0
         messageCacheSize = MapUtils.getInteger(endpoint.getProperties(), "messageCacheSize", 500);
 48  0
         channel = endpoint.getEndpointURI().getPath();
 49  0
         if(StringUtils.isEmpty(channel) || channel.equals("/"))
 50  
         {
 51  
             //TODO i18n
 52  0
             throw new CreateException(AjaxMessages.createStaticMessage("The subscription path cannot be empty or equal '/'"), this);
 53  
         }
 54  0
     }
 55  
 
 56  
     public AbstractBayeux getBayeux()
 57  
     {
 58  0
         return bayeux;
 59  
     }
 60  
 
 61  
     public void setBayeux(AbstractBayeux bayeux)
 62  
     {
 63  0
         this.bayeux = bayeux;
 64  0
     }
 65  
 
 66  
     @Override
 67  
     protected void doInitialise() throws InitialisationException
 68  
     {
 69  0
         if (cacheMessages)
 70  
         {
 71  0
             messageCache = new BoundedFifoBuffer(messageCacheSize);
 72  
         }
 73  0
     }
 74  
 
 75  
     protected Client getClient()
 76  
     {
 77  0
         if(client == null)
 78  
         {
 79  0
             client = bayeux.newClient(channel);
 80  
         }
 81  0
         return client;
 82  
     }
 83  
 
 84  
     protected void doDispatch(MuleEvent event) throws Exception
 85  
     {
 86  
         //We have no need for Null messages to be sent to the browser
 87  0
         if(NullPayload.getInstance().equals(event.getMessage().getPayload()))
 88  
         {
 89  0
             return;
 90  
         }
 91  0
         if (!connector.isStarted())
 92  
         {
 93  
             //TODO MULE-4320
 94  0
             logger.warn("Servlet container has not yet initialised, ignoring event: " + event.getMessage().getPayload());
 95  0
             return;
 96  
         }
 97  
 
 98  0
         Channel chan = bayeux.getChannel(channel);
 99  0
         if(chan!=null)
 100  
         {
 101  0
             if (chan.getSubscribers().size() > 0 && cacheMessages && !messageCache.isEmpty())
 102  
             {
 103  0
                 while (!messageCache.isEmpty())
 104  
                 {
 105  0
                     for (Client client : chan.getSubscribers())
 106  
                     {
 107  0
                         deliver(client, channel, messageCache.remove());
 108  
                     }
 109  
                     //deliver(getClient(), channel, messageCache.remove());
 110  
                 }
 111  
             }
 112  
 
 113  0
             Object data = event.getMessage().getPayload();
 114  
             //deliver(getClient(), channel, data);
 115  0
             for (Client client : chan.getSubscribers())
 116  
             {
 117  0
                 deliver(client, channel, data);
 118  
             }
 119  0
         }
 120  0
         else if (cacheMessages)
 121  
         {
 122  0
             Object data = event.getMessage().getPayload();
 123  0
             if (logger.isTraceEnabled())
 124  
             {
 125  0
                 logger.trace("There are no clients waiting, adding message to cache: " + data);
 126  
             }
 127  0
             messageCache.add(data);
 128  
         }
 129  0
     }
 130  
 
 131  
     protected void deliver(Client client, String channel, Object data)
 132  
     {
 133  0
         if (logger.isTraceEnabled())
 134  
         {
 135  0
             logger.trace("Delivering to client id: " + client.getId() + " channel:" + channel);
 136  
         }
 137  0
         client.deliver(client, channel, data, null);
 138  0
     }
 139  
 
 140  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 141  
     {
 142  0
         doDispatch(event);
 143  0
         return null;
 144  
     }
 145  
 
 146  
     @Override
 147  
     protected void doDispose()
 148  
     {
 149  0
         if (messageCache != null)
 150  
         {
 151  0
             messageCache.clear();
 152  0
             messageCache = null;
 153  
         }
 154  0
     }
 155  
 }