View Javadoc

1   // ========================================================================
2   // Copyright 2007-2008 Mort Bay Consulting Pty. Ltd.
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   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  //========================================================================
14  
15  package org.cometd.oort;
16  
17  
18  import java.io.IOException;
19  
20  import javax.servlet.Servlet;
21  import javax.servlet.ServletConfig;
22  import javax.servlet.ServletException;
23  import javax.servlet.ServletRequest;
24  import javax.servlet.ServletResponse;
25  import javax.servlet.UnavailableException;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.cometd.Bayeux;
29  import org.mortbay.cometd.AbstractCometdServlet;
30  
31  /**
32   * Oort Servlet.
33   * <p>
34   * This servlet initializes and configures and instance of the {@link Oort}
35   * comet cluster manager.  The servlet must be initialized after an instance
36   * of {@link AbstractCometdServlet}, which creates the {@link Bayeux} instance
37   * used.
38   * <p>
39   * The following servlet init parameters are used to configure Oort:<dl>
40   * <dt>oort.url</dt><dd>The absolute public URL to the cometd servlet.</dd>
41   * <dt>oort.cloud</dt><dd>A comma separated list of the oort.urls of other 
42   * known oort comet servers that are passed to {@link Oort#observeComet(String)}
43   * on startup.</dd>
44   * <dt>oort.channels</dt><dd>A comma separated list of channels that will be 
45   * passed to {@link Oort#observeChannel(String)}</dd>
46   * </dl>
47   * @author gregw
48   *
49   */
50  public class OortServlet implements Servlet
51  {    
52      private ServletConfig _config;
53  
54      public void destroy()
55      {
56      }
57  
58      public ServletConfig getServletConfig()
59      {
60          return _config;
61      }
62  
63      public String getServletInfo()
64      {
65          return OortServlet.class.toString();
66      }
67  
68      public void init(ServletConfig config) throws ServletException
69      {
70          System.err.println("INIT "+config);
71          _config=config;
72          
73          Bayeux bayeux = (Bayeux)config.getServletContext().getAttribute(Bayeux.ATTRIBUTE);
74          if (bayeux==null)
75          {
76              _config.getServletContext().log("No "+Bayeux.ATTRIBUTE+" initialized");
77              throw new UnavailableException(Bayeux.ATTRIBUTE);
78          }
79  
80          String url=_config.getInitParameter(Oort.OORT_URL);
81          if (url==null)
82          {
83              _config.getServletContext().log("No "+Oort.OORT_URL+" init parameter");
84              throw new UnavailableException(Oort.OORT_URL);
85          }
86          
87          Oort oort= new Oort(url,bayeux);
88          _config.getServletContext().setAttribute(Oort.OORT_ATTRIBUTE,oort);
89  
90          String channels=_config.getInitParameter(Oort.OORT_CHANNELS);
91          if (channels!=null)
92          {
93              String[] patterns=channels.split("[, ]");
94              for (String channel : patterns)
95                  oort.observeChannel(channel);
96              
97          }
98          
99          try
100         {
101             oort.start();
102         }
103         catch(Exception e)
104         {
105             throw new ServletException(e);
106         }
107         
108         String cloud = _config.getInitParameter(Oort.OORT_CLOUD);
109         if (cloud!=null)
110         {
111             String[] urls=cloud.split("[, ]");
112             for (String comet : urls)
113                 oort.observeComet(comet);
114             
115         }
116     }
117 
118     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
119     {
120         HttpServletResponse response = (HttpServletResponse)res;
121         response.sendError(503);        
122     }
123 }