Coverage Report - org.mule.transport.ajax.AjaxMessageDispatcherFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AjaxMessageDispatcherFactory
0%
0/18
0%
0/8
7
 
 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.MuleException;
 10  
 import org.mule.api.endpoint.OutboundEndpoint;
 11  
 import org.mule.api.transport.MessageDispatcher;
 12  
 import org.mule.transport.AbstractMessageDispatcherFactory;
 13  
 import org.mule.transport.ajax.container.AjaxServletConnector;
 14  
 import org.mule.transport.ajax.embedded.AjaxConnector;
 15  
 
 16  
 import org.mortbay.cometd.AbstractBayeux;
 17  
 
 18  
 /**
 19  
  * Creates a {@link AjaxMessageDispatcher}
 20  
  */
 21  0
 public class AjaxMessageDispatcherFactory extends AbstractMessageDispatcherFactory
 22  
 {
 23  
 
 24  
     public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
 25  
     {
 26  0
         AjaxMessageDispatcher dispatcher = new AjaxMessageDispatcher(endpoint);
 27  
 
 28  0
         if (endpoint.getConnector() instanceof AjaxConnector)
 29  
         {
 30  
             //We're running in embedded mode (i.e. using a Jetty servlet container created by the connector)
 31  
             //so we need to register the endpoint
 32  0
             dispatcher.setBayeux(((AjaxConnector) endpoint.getConnector()).getBayeux());
 33  
         }
 34  
         else
 35  
         {
 36  
             //We're bound to an existing servlet container, just grab the Bayeux object from the connector, which  would have been
 37  
             //set by the {@link MuleAjaxServlet}
 38  0
             AbstractBayeux b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
 39  0
             if (b == null)
 40  
             {
 41  0
                 long start = System.currentTimeMillis();
 42  
                 //Not the correct use for response time out but if fine for this purpose
 43  0
                 long timeout = start + endpoint.getResponseTimeout();
 44  0
                 while (start < timeout)
 45  
                 {
 46  
                     try
 47  
                     {
 48  0
                         Thread.sleep(1000);
 49  0
                         b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
 50  0
                         if (b != null)
 51  
                         {
 52  0
                             break;
 53  
                         }
 54  
                     }
 55  0
                     catch (InterruptedException e)
 56  
                     {
 57  
                         //ignore
 58  0
                     }
 59  
                 }
 60  0
                 throw new IllegalArgumentException("Bayeux is null: " + endpoint.getConnector() + ". Waited for " +
 61  
                                                    endpoint.getResponseTimeout() + " for object to become availble, this usually caused if the servlet container takes a long time to start up");
 62  
             }
 63  0
             dispatcher.setBayeux(b);
 64  
         }
 65  
 
 66  
 
 67  0
         return dispatcher;
 68  
     }
 69  
 }