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.demo;
16  
17  import java.io.IOException;
18  import javax.servlet.GenericServlet;
19  import javax.servlet.ServletException;
20  import javax.servlet.ServletRequest;
21  import javax.servlet.ServletResponse;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.cometd.Bayeux;
25  import org.cometd.Client;
26  import org.cometd.Message;
27  import org.mortbay.cometd.BayeuxService;
28  import org.mortbay.cometd.ext.AcknowledgedMessagesExtension;
29  import org.mortbay.cometd.ext.TimesyncExtension;
30  import org.mortbay.log.Log;
31  
32  public class CometdDemoServlet extends GenericServlet
33  {
34      public CometdDemoServlet()
35      {
36      }
37  
38  
39      @Override
40      public void init() throws ServletException
41      {
42          super.init();
43          Bayeux bayeux=(Bayeux)getServletContext().getAttribute(Bayeux.ATTRIBUTE);
44          new EchoRPC(bayeux);
45          new Monitor(bayeux);
46          new ChatService(bayeux);
47          bayeux.addExtension(new TimesyncExtension());
48          bayeux.addExtension(new AcknowledgedMessagesExtension());
49  /*
50          StatisticsExtension se = new StatisticsExtension();
51          se.setStatsRequestKeys(new String[]{"chat"});
52          se.setStatsRequestSentinel("//stats");
53          se.setStatsRequestChannel("/chat/demo");
54          se.setStatsResultSentinel("//stats-results");
55          se.setStatsConfirmSentinel("//stats-confirm");
56          se.setProbeChannel("/chat/demo");
57          se.setProbeSentinel("//stats-probe");
58          se.setProbeReplySentinel("//stats-reply");
59          se.setProbeReplyChannel("/chat/demo");
60          se.setProbeReplyKeys(new String[]{"chat"});
61          bayeux.addExtension(se);
62  */
63      }
64  
65      public static class EchoRPC extends BayeuxService
66      {
67          public EchoRPC(Bayeux bayeux)
68          {
69              super(bayeux,"echo");
70              subscribe("/service/echo","doEcho");
71          }
72  
73          public Object doEcho(Client client, Object data)
74          {
75              Log.info("ECHO from "+client+" "+data);
76              return data;
77          }
78      }
79  
80      public static class Monitor extends BayeuxService
81      {
82          public Monitor(Bayeux bayeux)
83          {
84              super(bayeux,"monitor");
85              subscribe("/meta/subscribe","monitorSubscribe");
86              subscribe("/meta/unsubscribe","monitorUnsubscribe");
87              subscribe("/meta/*","monitorMeta");
88              // subscribe("/**","monitorVerbose");
89          }
90  
91          public void monitorSubscribe(Client client, Message message)
92          {
93              Log.info("Subscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
94          }
95  
96          public void monitorUnsubscribe(Client client, Message message)
97          {
98              Log.info("Unsubscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
99          }
100 
101         public void monitorMeta(Client client, Message message)
102         {
103             if (Log.isDebugEnabled())
104                 Log.debug(message.toString());
105         }
106 
107         /*
108         public void monitorVerbose(Client client, Message message)
109         {
110             System.err.println(message);
111             try
112             {
113                 Thread.sleep(5000);
114             }
115             catch(Exception e)
116             {
117                 Log.warn(e);
118             }
119         }
120         */
121     }
122 
123     @Override
124     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
125     {
126         ((HttpServletResponse)res).sendError(503);
127     }
128 }