felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uiter...@apache.org
Subject svn commit: r1346763 [3/3] - in /felix/trunk/http: ./ api/ base/ bridge/ bundle/ cometd/ cometd/src/main/java/org/apache/felix/http/cometd/ cometd/src/main/java/org/apache/felix/http/cometd/internal/ jetty/ jetty/src/main/java/org/apache/felix/http/jet...
Date Wed, 06 Jun 2012 07:04:01 GMT
Modified: felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/Activator.java?rev=1346763&r1=1346762&r2=1346763&view=diff
==============================================================================
--- felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/Activator.java
(original)
+++ felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/Activator.java
Wed Jun  6 07:03:59 2012
@@ -17,23 +17,28 @@
 package org.apache.felix.http.samples.cometd;
 
 
+import java.util.Hashtable;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.felix.http.cometd.CometdService;
+import org.cometd.bayeux.server.BayeuxServer;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
-import javax.servlet.Servlet;
-import javax.servlet.ServletException;
-import java.util.Hashtable;
-import org.apache.felix.http.cometd.CometdService;
-import org.cometd.Bayeux;
 
 public final class Activator
     implements BundleActivator, ServiceTrackerCustomizer
 {
     private BundleContext context;
     private ServiceTracker cometdServiceTracker;
+    private ServiceTracker httpServiceTracker;
     private ServiceRegistration reg;
     
     public void start(BundleContext context)
@@ -42,6 +47,8 @@ public final class Activator
         this.context = context;
         this.cometdServiceTracker = new ServiceTracker(this.context, CometdService.class.getName(),
this);
         this.cometdServiceTracker.open();
+        this.httpServiceTracker = new ServiceTracker(this.context, HttpService.class.getName(),
new HTTPServiceCustomizer());
+        this.httpServiceTracker.open();
     }
 
     public void stop(BundleContext context)
@@ -50,6 +57,9 @@ public final class Activator
         if (this.cometdServiceTracker != null) {
             this.cometdServiceTracker.close();
         }
+        if (this.httpServiceTracker != null) {
+        	this.httpServiceTracker.close();
+        }
         this.unregister();
     }
 
@@ -73,15 +83,16 @@ public final class Activator
 
     private void register(CometdService cometdService) {
         try {
-            Bayeux bayeux = cometdService.getBayeux();
-            if (bayeux != null) {
+            BayeuxServer bayeuxServer = cometdService.getBayeuxServer();
+            if (bayeuxServer != null) {
                 Hashtable<String, String> props = new Hashtable<String, String>();
                 props.put("alias", "/system/time");
-                this.reg = context.registerService(Servlet.class.getName(), new TimeServlet(bayeux),
props);
+                // whiteboard servlet registration
+                this.reg = context.registerService(Servlet.class.getName(), new TimeServlet(bayeuxServer),
props);
                 doLog("Connect a browser to http://<host>:<port>/system/time
to view the time.");
             }
             else {
-                doLog("Failed to get bayeux");
+                doLog("Failed to get bayeux server");
             }
         }
         catch (ServletException e) {
@@ -100,4 +111,27 @@ public final class Activator
     {
         System.out.println("## Activator:   " + message);
     }
+    
+    class HTTPServiceCustomizer implements ServiceTrackerCustomizer {
+
+		public Object addingService(ServiceReference reference) {
+			HttpService httpService = (HttpService) context.getService(reference);
+			try {
+				httpService.registerResources("/js", "/src-web", null);
+			} catch (NamespaceException e) {
+				e.printStackTrace();
+			}
+			return null;
+		}
+
+		public void modifiedService(ServiceReference reference, Object service) {
+			
+		}
+
+		public void removedService(ServiceReference reference, Object service) {
+			HttpService httpService = (HttpService) context.getService(reference);
+			httpService.unregister("/js");
+		}
+    	
+    }
 }

Modified: felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/TimeServlet.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/TimeServlet.java?rev=1346763&r1=1346762&r2=1346763&view=diff
==============================================================================
--- felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/TimeServlet.java
(original)
+++ felix/trunk/http/samples/cometd/src/main/java/org/apache/felix/http/samples/cometd/TimeServlet.java
Wed Jun  6 07:03:59 2012
@@ -16,11 +16,6 @@
  */
 package org.apache.felix.http.samples.cometd;
 
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.text.SimpleDateFormat;
@@ -29,9 +24,18 @@ import java.util.Date;
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
-import org.cometd.Bayeux;
-import org.cometd.Channel;
-import org.cometd.Client;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.cometd.bayeux.server.BayeuxServer;
+import org.cometd.bayeux.server.ConfigurableServerChannel;
+import org.cometd.bayeux.server.LocalSession;
+import org.cometd.bayeux.server.ServerChannel;
+import org.cometd.bayeux.server.ServerMessage;
 
 public class TimeServlet
     extends HttpServlet
@@ -42,31 +46,93 @@ public class TimeServlet
         Time
     };
 
-    private Bayeux bayeux;
-    private Client client;
-    private List<Channel> channels;
+    private BayeuxServer bayeuxServer;
+    private LocalSession session;
+    private List<ServerChannel> channels;
     private Timer updateTimer;
     private SimpleDateFormat logDateFormat;
     private String html =
         "<html>\n" +
         "  <head>\n" +
         "    <title>System Information</title>\n" +
-        "    <script type='text/javascript' src='/dojo/dojo/dojo.js'></script>\n"
+
+        "    <script type='text/javascript' src='/js/dojo/dojo.js'></script>\n"
+
         "    <script type='text/javascript'>\n" +
         "      dojo.require('dojox.cometd');\n" +
-        "      dojo.addOnLoad(init);\n" +
-        "      dojo.addOnUnload(destroy);\n" +
-        "      function init() {\n" +
-        "        dojox.cometd.init('/system/cometd');\n" +
-        "        dojox.cometd.subscribe('/System/Time', updateTime);\n" +
-        "      }\n" +
-        "      function destroy() {\n" +
-        "        dojox.cometd.unsubscribe('/System/Time');\n" +
-        "        dojox.cometd.disconnect();\n" +
-        "      }\n" +
-        "      function updateTime(message) {\n" +
-        "          document.getElementById('systemTime').innerHTML = message.data;\n" +
-        "      }\n" +
+        "dojo.addOnLoad(function()\n" + 
+        "{\n" + 
+        "    var cometd = dojox.cometd;\n" + 
+        "\n" + 
+        "    function _connectionEstablished()\n" + 
+        "    {\n" + 
+        "        dojo.byId('body').innerHTML += '<div>CometD Connection Established</div>';\n"
+ 
+        "    }\n" + 
+        "\n" + 
+        "    function _connectionBroken()\n" + 
+        "    {\n" + 
+        "        dojo.byId('body').innerHTML += '<div>CometD Connection Broken</div>';\n"
+ 
+        "    }\n" + 
+        "\n" + 
+        "    function _connectionClosed()\n" + 
+        "    {\n" + 
+        "        dojo.byId('body').innerHTML += '<div>CometD Connection Closed</div>';\n"
+ 
+        "    }\n" + 
+        "\n" + 
+        "    // Function that manages the connection status with the Bayeux server\n" + 
+        "    var _connected = false;\n" + 
+        "    function _metaConnect(message)\n" + 
+        "    {\n" + 
+        "        if (cometd.isDisconnected())\n" + 
+        "        {\n" + 
+        "            _connected = false;\n" + 
+        "            _connectionClosed();\n" + 
+        "            return;\n" + 
+        "        }\n" + 
+        "\n" + 
+        "        var wasConnected = _connected;\n" + 
+        "        _connected = message.successful === true;\n" + 
+        "        if (!wasConnected && _connected)\n" + 
+        "        {\n" + 
+        "            _connectionEstablished();\n" + 
+        "        }\n" + 
+        "        else if (wasConnected && !_connected)\n" + 
+        "        {\n" + 
+        "            _connectionBroken();\n" + 
+        "        }\n" + 
+        "    }\n" + 
+        "\n" + 
+        "    // Function invoked when first contacting the server and\n" + 
+        "    // when the server has lost the state of this client\n" + 
+        "    function _metaHandshake(handshake)\n" + 
+        "    {\n" + 
+        "        if (handshake.successful === true)\n" + 
+        "        {\n" + 
+        "            cometd.batch(function()\n" + 
+        "            {\n" + 
+        "                cometd.subscribe('/System/Time', function(message)\n" + 
+        "                {\n" + 
+        "                    dojo.byId('systemTime').innerHTML = '<div>' + message.data
+ '</div>';\n" + 
+        "                });\n" + 
+        "            });\n" + 
+        "        }\n" + 
+        "    }\n" + 
+        "\n" + 
+        "    // Disconnect when the page unloads\n" + 
+        "    dojo.addOnUnload(function()\n" + 
+        "    {\n" + 
+        "        cometd.disconnect(true);\n" + 
+        "    });\n" + 
+        "\n" + 
+        "    var cometURL = \"http://localhost:8080/system/cometd\";\n" + 
+        "    cometd.configure({\n" + 
+        "        url: cometURL,\n" + 
+        "        logLevel: 'debug'\n" + 
+        "    });\n" + 
+        "\n" + 
+        "    cometd.addListener('/meta/handshake', _metaHandshake);\n" + 
+        "    cometd.addListener('/meta/connect', _metaConnect);\n" + 
+        "\n" + 
+        "    cometd.handshake();\n" + 
+        "});\n" + 
         "    </script>\n" +
         "  </head>\n" +
         "  <body>\n" +
@@ -74,17 +140,25 @@ public class TimeServlet
         "  </body>\n" +
         "</html>\n";
 
-    public TimeServlet(Bayeux bayeux)
+    public TimeServlet(BayeuxServer bayeuxServer)
     {
-        this.bayeux = bayeux;
-        this.client = this.bayeux.newClient(this.getClass().getName());
-        this.channels = new ArrayList<Channel>();
+        this.bayeuxServer = bayeuxServer;
+        this.channels = new ArrayList<ServerChannel>();
         for (SystemTopics topic : SystemTopics.values()) {
-            this.channels.add(topic.ordinal(), this.bayeux.getChannel("/System/" + topic,
true));
+        	boolean created = bayeuxServer.createIfAbsent("/System/" + topic, new ServerChannel.Initializer()
{
+				public void configureChannel(ConfigurableServerChannel channel) {
+					channel.setPersistent(true);
+				}
+        	});
+        	if (created) {
+        		this.channels.add(topic.ordinal(), this.bayeuxServer.getChannel("/System/" + topic));
+        	}
         }
-        this.updateTimer = new Timer("System.Time.ClientNotifier");
-        this.updateTimer.scheduleAtFixedRate(new ClientNotifier(), 1000, 1000);
         this.logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+		this.updateTimer = new Timer("System.Time.ClientNotifier");
+		this.updateTimer.scheduleAtFixedRate(new ClientNotifier(), 1000, 1000);
+		this.session = bayeuxServer.newLocalSession(TimeServlet.class.getName());
+		this.session.handshake();
     }
 
     @Override
@@ -100,6 +174,7 @@ public class TimeServlet
     {
         doLog("Destroyed servlet");
         this.updateTimer.cancel();
+        this.session.disconnect();
         super.destroy();
     }
 
@@ -131,8 +206,12 @@ public class TimeServlet
                     default:
                         topicData = "unknown topic: " + topic.toString();
                 }
-                channels.get(topic.ordinal()).publish(client, topicData, null);
+                ServerMessage.Mutable message = bayeuxServer.newMessage();
+                message.setChannel(channels.get(topic.ordinal()).getId());
+                message.setData(topicData);
+                channels.get(topic.ordinal()).publish(session, topicData, null);
             }
         }
     }
+
 }

Propchange: felix/trunk/http/samples/filter/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun  6 07:03:59 2012
@@ -2,3 +2,5 @@ target
 .settings
 .classpath
 .project
+.externalToolBuilders
+maven-eclipse.xml

Propchange: felix/trunk/http/samples/whiteboard/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun  6 07:03:59 2012
@@ -2,3 +2,5 @@ target
 .settings
 .classpath
 .project
+maven-eclipse.xml
+.externalToolBuilders

Propchange: felix/trunk/http/whiteboard/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun  6 07:03:59 2012
@@ -2,3 +2,5 @@ target
 .settings
 .classpath
 .project
+.externalToolBuilders
+maven-eclipse.xml



Mime
View raw message