1
2
3
4
5
6
7
8
9
10
11
12
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121 }
122 }