View Javadoc

1   /*
2    * $Id: AjaxMessageDispatcherFactory.java 20321 2010-11-24 15:21:24Z dfeist $
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  public class AjaxMessageDispatcherFactory extends AbstractMessageDispatcherFactory
25  {
26  
27      public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
28      {
29          AjaxMessageDispatcher dispatcher = new AjaxMessageDispatcher(endpoint);
30  
31          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              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              AbstractBayeux b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
42              if (b == null)
43              {
44                  long start = System.currentTimeMillis();
45                  //Not the correct use for response time out but if fine for this purpose
46                  long timeout = start + endpoint.getResponseTimeout();
47                  while (start < timeout)
48                  {
49                      try
50                      {
51                          Thread.sleep(1000);
52                          b = ((AjaxServletConnector) endpoint.getConnector()).getBayeux();
53                          if (b != null)
54                          {
55                              break;
56                          }
57                      }
58                      catch (InterruptedException e)
59                      {
60                          //ignore
61                      }
62                  }
63                  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              dispatcher.setBayeux(b);
67          }
68  
69  
70          return dispatcher;
71      }
72  }