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