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.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123 }
124 }