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.ServletContextAttributeEvent;
23  import javax.servlet.ServletContextAttributeListener;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  import javax.servlet.UnavailableException;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.cometd.Bayeux;
31  import org.mortbay.cometd.AbstractCometdServlet;
32  import org.mortbay.log.Log;
33  
34  /**
35   * Oort Servlet.
36   * <p>
37   * This servlet initializes and configures and instance of the {@link Oort}
38   * comet cluster manager.  The servlet must be initialized after an instance
39   * of {@link AbstractCometdServlet}, which creates the {@link Bayeux} instance
40   * used.
41   * <p>
42   * The following servlet init parameters are used to configure Oort:<dl>
43   * <dt>oort.url</dt><dd>The absolute public URL to the cometd servlet.</dd>
44   * <dt>oort.cloud</dt><dd>A comma separated list of the oort.urls of other 
45   * known oort comet servers that are passed to {@link Oort#observeComet(String)}
46   * on startup.</dd>
47   * <dt>oort.channels</dt><dd>A comma separated list of channels that will be 
48   * passed to {@link Oort#observeChannel(String)}</dd>
49   * </dl>
50   * @author gregw
51   *
52   */
53  public class OortServlet implements Servlet
54  {    
55      private ServletConfig _config;
56  
57      public void destroy()
58      {
59      }
60  
61      public ServletConfig getServletConfig()
62      {
63          return _config;
64      }
65  
66      public String getServletInfo()
67      {
68          return OortServlet.class.toString();
69      }
70  
71      public void init(ServletConfig config) throws ServletException
72      {
73          System.err.println("INIT "+config);
74          _config=config;
75          
76          Bayeux bayeux = (Bayeux)config.getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
77          if (bayeux==null)
78          {
79              _config.getServletContext().log("No "+Bayeux.DOJOX_COMETD_BAYEUX+" initialized");
80              throw new UnavailableException(Bayeux.DOJOX_COMETD_BAYEUX);
81          }
82  
83          String url=_config.getInitParameter(Oort.OORT_URL);
84          if (url==null)
85          {
86              _config.getServletContext().log("No "+Oort.OORT_URL+" init parameter");
87              throw new UnavailableException(Oort.OORT_URL);
88          }
89          
90          Oort oort= new Oort(url,bayeux);
91          _config.getServletContext().setAttribute(Oort.OORT_ATTRIBUTE,oort);
92  
93          String channels=_config.getInitParameter(Oort.OORT_CHANNELS);
94          if (channels!=null)
95          {
96              String[] patterns=channels.split("[, ]");
97              for (String channel : patterns)
98                  oort.observeChannel(channel);
99              
100         }
101         
102         try
103         {
104             oort.start();
105         }
106         catch(Exception e)
107         {
108             throw new ServletException(e);
109         }
110         
111         String cloud = _config.getInitParameter(Oort.OORT_CLOUD);
112         if (cloud!=null)
113         {
114             String[] urls=cloud.split("[, ]");
115             for (String comet : urls)
116                 oort.observeComet(comet);
117             
118         }
119     }
120 
121     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
122     {
123         HttpServletResponse response = (HttpServletResponse)res;
124         response.sendError(503);        
125     }
126 }