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.TimesyncExtension;
27  import org.mortbay.log.Log;
28  
29  public class BayeuxServicesListener implements ServletContextAttributeListener
30  {
31      public void initialize(Bayeux bayeux)
32      {
33          synchronized(bayeux)
34          {
35              if (!bayeux.hasChannel("/service/echo"))
36              {
37                  new EchoRPC(bayeux);
38                  new Monitor(bayeux);
39                  new ChatService(bayeux);
40  		bayeux.addExtension(new TimesyncExtension());
41              }
42          }
43      }
44      
45      public void attributeAdded(ServletContextAttributeEvent scab)
46      {
47          if (scab.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX))
48          {
49              Bayeux bayeux=(Bayeux)scab.getValue();
50              initialize(bayeux);
51          }
52      }
53  
54      public void attributeRemoved(ServletContextAttributeEvent scab)
55      {
56  
57      }
58  
59      public void attributeReplaced(ServletContextAttributeEvent scab)
60      {
61  
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 }