1 package net.sf.tacos.cometd.components;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.servlet.ServletContext;
7
8 import org.apache.tapestry.BaseComponent;
9 import org.apache.tapestry.IScript;
10 import org.apache.tapestry.annotations.InjectObject;
11 import org.apache.tapestry.annotations.InjectScript;
12 import org.apache.tapestry.web.WebRequest;
13 import org.mortbay.cometd.AbstractBayeux;
14
15 import dojox.cometd.Bayeux;
16 import dojox.cometd.Client;
17
18 public abstract class Cometd extends BaseComponent
19 {
20 public abstract String getTopic();
21 public abstract void setTopic(String topic);
22
23 public abstract String getClickComponent();
24 public abstract void setClickComponent(String comp);
25
26 @InjectObject("service:tapestry.globals.ServletContext")
27 public abstract ServletContext getServletContext();
28
29 @InjectObject("service:tapestry.globals.WebRequest")
30 public abstract WebRequest getWebRequest();
31
32 @InjectScript("Cometd.script")
33 public abstract IScript getScript();
34
35 private AbstractBayeux _bayeux = null;
36
37 private Client _client = null;
38
39 public void publish(String information)
40 {
41 if(_bayeux == null)
42 {
43 _bayeux = (AbstractBayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
44 }
45
46
47
48 if(_bayeux != null)
49 {
50
51 if(_client == null)
52 {
53 _client=_bayeux.newClient("alarms", new dojox.cometd.Listener()
54 {
55 public void removed(String id, boolean test)
56 {}
57 public void deliver(Client fromClient, String toChannel, Object data,String id)
58 {
59 }
60 });
61 }
62
63 if(_client != null)
64 {
65 _bayeux.publish(_client, getTopic(),"TEST", null);
66 }
67 }
68 }
69
70 public Map getScriptSymbols()
71 {
72 String cometdPath = getWebRequest().getContextPath() + "/cometd";
73
74 Map mapSymbols = new HashMap();
75 mapSymbols.put("topic", getTopic());
76 mapSymbols.put("cometdPath", cometdPath);
77 mapSymbols.put("clickComponent", getClickComponent());
78 return mapSymbols;
79 }
80
81 }