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 _config=config;
71
72 Bayeux bayeux = (Bayeux)config.getServletContext().getAttribute(Bayeux.ATTRIBUTE);
73 if (bayeux==null)
74 {
75 _config.getServletContext().log("No "+Bayeux.ATTRIBUTE+" initialized");
76 throw new UnavailableException(Bayeux.ATTRIBUTE);
77 }
78
79 String url=_config.getInitParameter(Oort.OORT_URL);
80 if (url==null)
81 {
82 _config.getServletContext().log("No "+Oort.OORT_URL+" init parameter");
83 throw new UnavailableException(Oort.OORT_URL);
84 }
85
86 Oort oort= new Oort(url,bayeux);
87 _config.getServletContext().setAttribute(Oort.OORT_ATTRIBUTE,oort);
88
89 String channels=_config.getInitParameter(Oort.OORT_CHANNELS);
90 if (channels!=null)
91 {
92 String[] patterns=channels.split("[, ]");
93 for (String channel : patterns)
94 oort.observeChannel(channel);
95
96 }
97
98 try
99 {
100 oort.start();
101 }
102 catch(Exception e)
103 {
104 throw new ServletException(e);
105 }
106
107 String cloud = _config.getInitParameter(Oort.OORT_CLOUD);
108 if (cloud!=null)
109 {
110 String[] urls=cloud.split("[, ]");
111 for (String comet : urls)
112 oort.observeComet(comet);
113
114 }
115 }
116
117 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
118 {
119 HttpServletResponse response = (HttpServletResponse)res;
120 response.sendError(503);
121 }
122 }