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