You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by fm...@apache.org on 2010/08/09 21:00:59 UTC

svn commit: r983776 - in /tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime: CometBindingHandler.java CometServiceBindingProvider.java CometServlet.java ScriptFilter.java

Author: fmoga
Date: Mon Aug  9 19:00:58 2010
New Revision: 983776

URL: http://svn.apache.org/viewvc?rev=983776&view=rev
Log:
Added new implementation for comet binding which supports choosing the transport (http streaming, long pooling, websockets) at runtime.

Added:
    tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java
      - copied, changed from r982281, tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java
Removed:
    tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java
    tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java
Modified:
    tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java

Copied: tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java (from r982281, tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java)
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java?p2=tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java&p1=tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java&r1=982281&r2=983776&rev=983776&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java (original)
+++ tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java Mon Aug  9 19:00:58 2010
@@ -1,82 +1,53 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *   http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.    
- */
-
 package org.apache.tuscany.sca.binding.comet.runtime;
 
-import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
-import java.util.LinkedList;
-import java.util.List;
 
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletContext;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
 
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
-import org.atmosphere.cpr.BroadcastFilter;
-import org.atmosphere.cpr.Broadcaster.SCOPE;
-import org.atmosphere.cpr.Meteor;
-import org.atmosphere.util.XSSHtmlFilter;
-
-public class CometServlet extends HttpServlet {
-    private static final long serialVersionUID = 1L;
-    private static final String METEOR_KEY = "org.apache.tuscany.comet.meteor";
+import org.atmosphere.annotation.Broadcast;
+import org.atmosphere.cpr.Broadcaster;
+import org.atmosphere.cpr.DefaultBroadcaster;
+import org.atmosphere.jersey.Broadcastable;
+import org.atmosphere.jersey.SuspendResponse;
+
+import com.sun.jersey.spi.container.servlet.PerSession;
+
+@Path("/")
+@Produces("text/html;charset=ISO-8859-1")
+@PerSession
+public class CometBindingHandler {
+
+    private Broadcaster broadcaster;
     public static final String ENDPOINT_KEY = "org.apache.tuscany.sca.binding.comet.endpoint";
     public static final String OPERATION_KEY = "org.apache.tuscany.sca.binding.comet.operation";
 
-    protected transient RuntimeEndpoint wire;
-    protected transient Operation operation;
-    private List<BroadcastFilter> filters;
-
-    @Override
-    public void init(ServletConfig config) throws ServletException {
-        super.init(config);
-        filters = new LinkedList<BroadcastFilter>();
-        filters.add(new XSSHtmlFilter());
-        filters.add(new ScriptFilter());
+    @Context
+    private ServletContext sc;
+    
+    @GET
+    public SuspendResponse<String> connect() {
+        broadcaster = new DefaultBroadcaster();
+        return new SuspendResponse.SuspendResponseBuilder<String>()
+                .broadcaster(broadcaster)
+                .outputComments(true)
+        //      .addListener(new EventsLogger())
+                .build();
     }
 
-    @Override
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        Meteor meteor = Meteor.build(req, SCOPE.REQUEST, filters, null);
-        // meteor.addListener(new EventsLogger());
-        req.getSession().setAttribute(METEOR_KEY, meteor);
-        resp.setContentType("text/html");
-        meteor.suspend(-1);
+    @POST
+    @Broadcast
+    public Broadcastable callAndRespond() throws InvocationTargetException {
+        RuntimeEndpoint wire = (RuntimeEndpoint)sc.getAttribute(ENDPOINT_KEY);
+        Operation operation = (Operation)sc.getAttribute(OPERATION_KEY);
+        Object response = wire.invoke(operation, new Object[] {});
+        return new Broadcastable(response.toString(), "", broadcaster);
     }
 
-    @Override
-    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        this.wire = (RuntimeEndpoint)getServletContext().getAttribute(ENDPOINT_KEY);
-        this.operation = (Operation)getServletContext().getAttribute(OPERATION_KEY);
-        Meteor meteor = (Meteor)req.getSession().getAttribute(METEOR_KEY);
-        meteor.broadcast(invokeService(new Object[] {}));
-    }
-
-    protected Object invokeService(Object[] args) {
-        try {
-            return wire.invoke(operation, args);
-        } catch (InvocationTargetException e) {
-            throw new RuntimeException(e);
-        }
-    }
 }

Modified: tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java?rev=983776&r1=983775&r2=983776&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java (original)
+++ tuscany/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java Mon Aug  9 19:00:58 2010
@@ -26,13 +26,13 @@ import org.apache.tuscany.sca.interfaced
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.provider.ServiceBindingProvider;
 import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
-import org.atmosphere.cpr.MeteorServlet;
+import org.atmosphere.cpr.AtmosphereServlet;
 
 public class CometServiceBindingProvider implements ServiceBindingProvider {
 
-    private static final String SERVLET_KEY = "org.atmosphere.servlet";
-    private static final String SERVLET_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.CometServlet";
-    
+    private static final String PACKAGE_KEY = "com.sun.jersey.config.property.packages";
+    private static final String PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime";
+
     private RuntimeEndpoint endpoint;
     private ServletHost servletHost;
 
@@ -45,12 +45,12 @@ public class CometServiceBindingProvider
         ComponentService service = endpoint.getService();
         Interface serviceInterface = service.getInterfaceContract().getInterface();
         for (Operation op : serviceInterface.getOperations()) {
-            MeteorServlet servlet = new MeteorServlet();
-            servlet.addInitParameter(SERVLET_KEY, SERVLET_VALUE);
-            String path = endpoint.getBinding().getURI() + "/" + op.getName();
+            String path = endpoint.getBinding().getURI() + "/" + op.getName() + "/*";
+            AtmosphereServlet servlet = new AtmosphereServlet();
+            servlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE);
             servletHost.addServletMapping(path, servlet);
-            servlet.getServletContext().setAttribute(CometServlet.ENDPOINT_KEY, endpoint);
-            servlet.getServletContext().setAttribute(CometServlet.OPERATION_KEY, op);
+            servlet.getServletContext().setAttribute(CometBindingHandler.ENDPOINT_KEY, endpoint);
+            servlet.getServletContext().setAttribute(CometBindingHandler.OPERATION_KEY, op);
         }
     }