View Javadoc

1   /*
2    * Copyright 2007-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springbyexample.cometd.continuation;
18  
19  import java.util.Map;
20  
21  import javax.servlet.ServletException;
22  
23  import org.mortbay.cometd.AbstractBayeux;
24  import org.mortbay.cometd.continuation.ContinuationCometdServlet;
25  import org.springframework.web.context.support.WebApplicationContextUtils;
26  
27  /**
28   * A continuation comet servlet meant to be used along with 
29   * {@link SpringContinuationBayeux}.  This servlet overrides the 
30   * default intialization of the bayeux instance because the 
31   * {@link SpringContinuationBayeux} fully initializes itself using 
32   * a Spring configuration. 
33   * 
34   * @author David Winterfeldt
35   */
36  public class SpringContinuationCometdServlet extends ContinuationCometdServlet {
37  
38      private static final long serialVersionUID = 6186673799799707142L;
39  
40      /**
41       * <p>Gets bayeux instance configured by Spring
42       * and sets <code>asyncDeliver</code> from the servlet init parameters.</p>
43       * 
44       * <p>Throws a <code>ServletException</code> if it can't find 
45       * a unique Spring bean of the type <code>AbstractBayeux</code>.</p>
46       * 
47       * <p>Overrides <code>AbstractCometdServlet.init()</code> because 
48       * {@link SpringContinuationBayeux} handles it's own initialization.</p>
49       */
50      @Override
51      @SuppressWarnings("unchecked")
52      public void init() throws ServletException {
53          synchronized (SpringContinuationCometdServlet.class) {
54              Map<String, AbstractBayeux> hBayeuxBeans = (Map<String, AbstractBayeux>)WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBeansOfType(AbstractBayeux.class);
55              
56              if (hBayeuxBeans == null || hBayeuxBeans.size() != 1) {
57                  throw new ServletException("Unable to find a unique Spring bean of the type AbstractBayeux.");
58              }
59              
60              for (AbstractBayeux bayeux : hBayeuxBeans.values()) {
61                  _bayeux = bayeux;
62                  break;
63              }            
64          }
65          
66          String async = getInitParameter("asyncDeliver");
67  
68          if (async!=null) {
69              _asyncDeliver = Boolean.parseBoolean(async);
70          }
71      }
72      
73  }