View Javadoc

1   /*
2    * Copyright 2007-2014 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  package org.springbyexample.cometd.continuation;
17  
18  import java.io.IOException;
19  import java.io.StringReader;
20  import java.util.Map;
21  
22  import javax.annotation.PostConstruct;
23  import javax.servlet.ServletContext;
24  
25  import org.cometd.DataFilter;
26  import org.mortbay.cometd.continuation.ContinuationBayeux;
27  import org.mortbay.cometd.continuation.ContinuationCometdServlet;
28  import org.mortbay.cometd.filter.JSONDataFilter;
29  import org.mortbay.util.ajax.JSON;
30  import org.springframework.util.ClassUtils;
31  import org.springframework.web.context.ServletContextAware;
32  
33  /**
34   * A subclass of {@link ContinuationBayeux} that allows
35   * full initialization of bayeux outside the {@link ContinuationCometdServlet}.
36   *
37   * @author David Winterfeldt
38   */
39  public class SpringContinuationBayeux extends ContinuationBayeux implements ServletContextAware {
40  
41      protected ServletContext servletContext = null;
42  
43      protected String filters = null;
44      protected long maxInterval  = 10000;
45  
46      /**
47       * Implementation of <code>ServletContextAware</code>.
48       */
49      @Override
50      public void setServletContext(ServletContext servletContext) {
51          this.servletContext = servletContext;
52      }
53  
54      /**
55       * Gets filters as JSON objects.
56       */
57      public String getFilters() {
58          return filters;
59      }
60  
61      /**
62       * Sets filters as JSON objects.
63       */
64      public void setFilters(String filters) {
65          this.filters = filters;
66      }
67  
68      /**
69       * <p>Sets max interval.</p>
70       *
71       * <p><strong>Note</strong>: Overriding parent because it sets value on
72       * internal instance before initialization.</p>
73       */
74      @Override
75      public void setMaxInterval(long maxInterval) {
76          this.maxInterval = maxInterval;
77      }
78  
79  
80      /**
81       * Initializes bayeux.
82       */
83      @PostConstruct
84      public void init() {
85          this.initialize(servletContext);
86  
87          super.setMaxInterval(maxInterval);
88  
89          // based on AbstractCometdServlet.init()
90          if (filters != null) {
91              try {
92                  Object[] objects = (Object[]) JSON.parse(new StringReader(filters));
93  
94                  for (int i = 0; objects != null && i < objects.length; i++) {
95                      Map<?, ?> filter_def = (Map<?, ?>) objects[i];
96  
97                      String className = (String) filter_def.get("filter");
98                      DataFilter filter = (DataFilter) ClassUtils.forName(className, ClassUtils.getDefaultClassLoader()).newInstance();
99  
100                     if (filter instanceof JSONDataFilter)
101                         ((JSONDataFilter) filter).init(filter_def.get("init"));
102 
103                     getChannel((String) filter_def.get("channels"), true).addDataFilter(filter);
104                 }
105             } catch (IOException e) {
106                 throw new IllegalArgumentException("Unable to initialize bayuex filters.  " + e.getMessage());
107             } catch (InstantiationException e) {
108                 throw new IllegalArgumentException("Unable to initialize bayuex filters.  " + e.getMessage());
109             } catch (IllegalAccessException e) {
110                 throw new IllegalArgumentException("Unable to initialize bayuex filters.  " + e.getMessage());
111             } catch (ClassNotFoundException e) {
112                 throw new IllegalArgumentException("Unable to initialize bayuex filters.  " + e.getMessage());
113             } catch (LinkageError e) {
114                 throw new IllegalArgumentException("Unable to initialize bayuex filters.  " + e.getMessage());
115             }
116         }
117     }
118 
119 }