View Javadoc
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  public class AjaxMessageDispatcherFactory extends AbstractMessageDispatcherFactory
22  {
23  
24      public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
25      {
26          AjaxMessageDispatcher dispatcher = new AjaxMessageDispatcher(endpoint);
27  
28          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              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              AbstractBayeux b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
39              if (b == null)
40              {
41                  long start = System.currentTimeMillis();
42                  //Not the correct use for response time out but if fine for this purpose
43                  long timeout = start + endpoint.getResponseTimeout();
44                  while (start < timeout)
45                  {
46                      try
47                      {
48                          Thread.sleep(1000);
49                          b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
50                          if (b != null)
51                          {
52                              break;
53                          }
54                      }
55                      catch (InterruptedException e)
56                      {
57                          //ignore
58                      }
59                  }
60                  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              dispatcher.setBayeux(b);
64          }
65  
66  
67          return dispatcher;
68      }
69  }