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  
18  
19  import javax.servlet.ServletContextAttributeEvent;
20  import javax.servlet.ServletContextAttributeListener;
21  
22  import org.cometd.Bayeux;
23  import org.cometd.Client;
24  import org.cometd.Message;
25  import org.mortbay.cometd.BayeuxService;
26  import org.mortbay.cometd.ext.AcknowledgedMessagesExtension;
27  import org.mortbay.cometd.ext.TimesyncExtension;
28  import org.mortbay.log.Log;
29  
30  public class BayeuxServicesListener implements ServletContextAttributeListener
31  {
32      public void initialize(Bayeux bayeux)
33      {
34          new EchoRPC(bayeux);
35          new Monitor(bayeux);
36          new ChatService(bayeux);
37          bayeux.addExtension(new TimesyncExtension());
38          bayeux.addExtension(new AcknowledgedMessagesExtension());
39      }
40      
41      public void attributeAdded(ServletContextAttributeEvent scab)
42      {
43          if (scab.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX))
44          {
45              Bayeux bayeux=(Bayeux)scab.getValue();
46              initialize(bayeux);
47          }
48      }
49  
50      public void attributeRemoved(ServletContextAttributeEvent scab)
51      {
52  
53      }
54  
55      public void attributeReplaced(ServletContextAttributeEvent scab)
56      {
57  
58      }
59  
60      
61      public static class EchoRPC extends BayeuxService
62      {
63          public EchoRPC(Bayeux bayeux)
64          {
65              super(bayeux,"echo");
66              subscribe("/service/echo","doEcho");
67          }
68          
69          public Object doEcho(Client client, Object data)
70          {
71  	    Log.info("ECHO from "+client+" "+data);
72  	    return data;
73          }
74      }
75      
76      public static class Monitor extends BayeuxService
77      {
78          public Monitor(Bayeux bayeux)
79          {
80              super(bayeux,"monitor");
81              subscribe("/meta/subscribe","monitorSubscribe");
82              subscribe("/meta/unsubscribe","monitorUnsubscribe");
83              subscribe("/meta/*","monitorMeta");
84              // subscribe("/**","monitorVerbose");
85          }
86          
87          public void monitorSubscribe(Client client, Message message)
88          {
89              Log.info("Subscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
90          }
91          
92          public void monitorUnsubscribe(Client client, Message message)
93          {
94              Log.info("Unsubscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
95          }
96          
97          public void monitorMeta(Client client, Message message)
98          {
99              if (Log.isDebugEnabled())
100                 Log.debug(message.toString());
101         }
102         
103         /*
104         public void monitorVerbose(Client client, Message message)
105         {
106             System.err.println(message);
107             try 
108             {
109                 Thread.sleep(5000);
110             }
111             catch(Exception e)
112             {
113                 Log.warn(e);
114             }
115         }
116         */
117     }
118 }