001    package net.sf.tacos.cometd.components;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import javax.servlet.ServletContext;
007    
008    import org.apache.tapestry.BaseComponent;
009    import org.apache.tapestry.IScript;
010    import org.apache.tapestry.annotations.InjectObject;
011    import org.apache.tapestry.annotations.InjectScript;
012    import org.apache.tapestry.web.WebRequest;
013    import org.mortbay.cometd.AbstractBayeux;
014    
015    import dojox.cometd.Bayeux;
016    import dojox.cometd.Client;
017    
018    public abstract class Cometd extends BaseComponent
019    {
020        public abstract String getTopic();
021        public abstract void setTopic(String topic);
022        
023        public abstract String getClickComponent();
024        public abstract void setClickComponent(String comp);
025        
026        @InjectObject("service:tapestry.globals.ServletContext")
027        public abstract ServletContext getServletContext();
028        
029        @InjectObject("service:tapestry.globals.WebRequest")
030        public abstract WebRequest getWebRequest();
031        
032        @InjectScript("Cometd.script")
033        public abstract IScript getScript();
034        
035        private AbstractBayeux _bayeux = null;
036        
037        private Client _client = null;
038        
039        public void publish(String information)
040        {
041            if(_bayeux == null)
042            {
043                _bayeux = (AbstractBayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
044            }
045            
046            
047            
048            if(_bayeux != null)
049            {
050                
051                if(_client == null)
052                {
053                    _client=_bayeux.newClient("alarms", new dojox.cometd.Listener()
054                    {
055                        public void removed(String id, boolean test)
056                        {}
057                        public void deliver(Client fromClient, String toChannel, Object data,String id)
058                        {
059                        }
060                    });
061                }
062                
063                if(_client != null)
064                {
065                    _bayeux.publish(_client, getTopic(),"TEST", null);
066                }
067            }
068        }
069        
070        public Map getScriptSymbols()
071        {
072            String cometdPath = getWebRequest().getContextPath() + "/cometd";
073            //publish("TEST");
074            Map mapSymbols = new HashMap();
075            mapSymbols.put("topic", getTopic());
076            mapSymbols.put("cometdPath", cometdPath);
077            mapSymbols.put("clickComponent", getClickComponent());
078            return mapSymbols;
079        }
080        
081    }