From 1491d3d1338236d1dfba96aa2387075c6a74dbb3 Mon Sep 17 00:00:00 2001 From: fmoga Date: Mon, 2 Aug 2010 20:25:24 +0000 Subject: Implemented comet binding using Atmosphere Framework. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@981675 13f79535-47bb-0310-9956-ffa450edef68 --- .../contrib/modules/binding-comet-runtime/pom.xml | 14 +- .../comet/runtime/CometBindingProviderFactory.java | 3 + .../sca/binding/comet/runtime/CometInvoker.java | 125 +---------------- .../runtime/CometReferenceBindingProvider.java | 8 +- .../comet/runtime/CometServiceBindingProvider.java | 15 +- .../sca/binding/comet/runtime/CometServlet.java | 152 ++++++--------------- .../sca/binding/comet/runtime/EventsLogger.java | 67 +++++++++ .../sca/binding/comet/runtime/ScriptFilter.java | 20 +++ .../org/apache/tuscany/sca/test/CometTest.java | 10 +- .../java/org/apache/tuscany/sca/test/StockApp.java | 15 -- .../org/apache/tuscany/sca/test/StockService.java | 4 +- .../apache/tuscany/sca/test/StockServiceImpl.java | 11 +- .../src/test/resources/test.composite | 6 +- 13 files changed, 176 insertions(+), 274 deletions(-) create mode 100644 sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java create mode 100644 sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java delete mode 100644 sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockApp.java (limited to 'sca-java-2.x/contrib/modules') diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml b/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml index e61f7660c3..9ff94ca831 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml @@ -25,16 +25,28 @@ 2.0-SNAPSHOT ../pom.xml + + org.apache.tuscany.sca tuscany-binding-comet-runtime Apache Tuscany SCA Comet Binding Runtime + jar - org.apache.tuscany.sca tuscany-binding-comet 2.0-SNAPSHOT + + org.atmosphere + atmosphere-commons + 0.7-SNAPSHOT + + + org.atmosphere + atmosphere-runtime + 0.7-SNAPSHOT + org.apache.tuscany.sca diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java index 747ea4d78e..cc30fdd518 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java @@ -34,6 +34,7 @@ public class CometBindingProviderFactory implements BindingProviderFactory filters; + + public CometServlet() { + } + public CometServlet(RuntimeEndpoint wire, Operation operation) { + System.out.println("Entering CometServlet constructor..."); this.wire = wire; this.operation = operation; - this.mapper = new ObjectMapper(); + filters = new LinkedList(); + filters.add(new XSSHtmlFilter()); + filters.add(new ScriptFilter()); } @Override - public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { - String jsonRequest = getJSONRequest(servletRequest); - Object[] args = jsonToObjects(jsonRequest); - Object response = invokeService(args); - String jsonResponse = getJSONResponse(servletRequest, response); - servletResponse.getOutputStream().println(jsonResponse); - } - - /** - * Turn the request into JSON - */ - protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException { - - List types = operation.getInputType().getLogical(); - int typesIndex = 0; - - String jsonRequest = ""; - for (String name : getOrderedParameterNames(servletRequest)) { - if (!name.startsWith("_") && !"callback".equals(name)) { - if (jsonRequest.length() > 1) { - jsonRequest += ", "; - } - - // automatically quote string parammeters so clients work in the usual javascript way - if (typesIndex < types.size() && String.class.equals(types.get(typesIndex).getGenericType())) { - String x = servletRequest.getParameter(name); - // TODO: do this more properly - if (!x.startsWith("\"")) { - jsonRequest += "\"" + x + "\""; - } else { - jsonRequest += x; - } - } else { - jsonRequest += servletRequest.getParameter(name); - } - - } - } - - return "[" + jsonRequest + "]"; + public void init(ServletConfig config) throws ServletException { + System.out.println("Entering CometServlet#init..."); + super.init(config); + filters = new LinkedList(); + filters.add(new XSSHtmlFilter()); + filters.add(new ScriptFilter()); } - /** - * Get the request parameter names in the correct order. - * The request args need to be in the correct order to invoke the service so work out the - * order from the order in the query string. Eg, the url: - * http://localhost:8085/HelloWorldService/sayHello2?first=petra&last=arnold&callback=foo" - * should invoke: - * sayHello2("petra", "arnold") - * so the parameter names should be ordered: "first", "last" - */ - protected SortedSet getOrderedParameterNames(ServletRequest servletRequest) { - final String queryString = ((HttpServletRequest)servletRequest).getQueryString(); - - SortedSet sortedNames = new TreeSet(new Comparator(){ - public int compare(String o1, String o2) { - int i = queryString.indexOf(o1); - int j = queryString.indexOf(o2); - return i - j; - }}); - - Set parameterNames = servletRequest.getParameterMap().keySet(); - for (String name : parameterNames) { - sortedNames.add(name); - } - - return sortedNames; - } - - /** - * Turn the response object into JSON - */ - protected String getJSONResponse(ServletRequest servletRequest, Object response) throws IOException, JsonParseException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - mapper.writeValue(os , response); - String jsonResponse = os.toString(); - - String callback = servletRequest.getParameter("callback"); - if (callback != null && callback.length() > 1) { - jsonResponse = callback + "(" + jsonResponse + ");"; - } - - return jsonResponse; + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + System.out.println("Entering CometServlet#doGet..."); + 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); } - /** - * Turn the request JSON into objects - */ - protected Object[] jsonToObjects(String jsonRequest) throws IOException, JsonParseException, JsonMappingException { - Class c = new Object[0].getClass(); - Object[] args = (Object[])mapper.readValue(jsonRequest, c); - return args; + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + System.out.println("Entering CometServlet#doPost..."); + System.out.println("Getting attributes from servlet context..."); + this.wire = (RuntimeEndpoint)getServletContext().getAttribute("org.apache.tuscany.sca.binding.comet.endpoint"); + this.operation = (Operation)getServletContext().getAttribute("org.apache.tuscany.sca.binding.comet.operation"); + System.out.println("Getting Meteor..."); + Meteor meteor = (Meteor)req.getSession().getAttribute(METEOR_KEY); + meteor.broadcast(invokeService(new Object[] {})); } - /** - * Send the request down the wire to invoke the service - */ protected Object invokeService(Object[] args) { try { return wire.invoke(operation, args); @@ -160,4 +94,4 @@ public class CometServlet extends GenericServlet { throw new RuntimeException(e); } } -} \ No newline at end of file +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java new file mode 100644 index 0000000000..934f5ef907 --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java @@ -0,0 +1,67 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.apache.tuscany.sca.binding.comet.runtime; + +import org.atmosphere.cpr.AtmosphereResourceEvent; +import org.atmosphere.cpr.AtmosphereResourceEventListener; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class EventsLogger implements AtmosphereResourceEventListener { + + public EventsLogger() { + } + + public void onSuspend(final AtmosphereResourceEvent event) { + System.out.println("onSuspend: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onResume(AtmosphereResourceEvent event) { + System.out.println("onResume: " + event.getResource().getRequest().getRemoteAddr()); + } + + public void onDisconnect(AtmosphereResourceEvent event) { + System.out.println("onDisconnect: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onBroadcast(AtmosphereResourceEvent event) { + System.out.println("onBroadcast: " + event.getMessage()); + } +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java new file mode 100644 index 0000000000..8964db68cc --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java @@ -0,0 +1,20 @@ +package org.apache.tuscany.sca.binding.comet.runtime; + +import org.atmosphere.cpr.BroadcastFilter; + +public class ScriptFilter implements BroadcastFilter { + + private static final String BEGIN_SCRIPT_TAG = "\n"; + // TODO: add dynamic function configuration + private static final String DEFAULT_FUNCTION = "window.parent.update"; + + public BroadcastAction filter(Object o) { + if (o instanceof String) { + String message = (String)o; + return new BroadcastAction(BEGIN_SCRIPT_TAG + DEFAULT_FUNCTION + "('" + message + "');\n" + END_SCRIPT_TAG); + } else { + return new BroadcastAction(o); + } + } +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTest.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTest.java index e1fbfc7c79..0b22515f7a 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTest.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTest.java @@ -1,26 +1,18 @@ package org.apache.tuscany.sca.test; -import java.io.IOException; +import junit.framework.TestCase; import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; -import junit.framework.TestCase; - public class CometTest extends TestCase { public void testComet() { String location = ContributionLocationHelper.getContributionLocation("test.composite"); Node node = NodeFactory.newInstance().createNode("test.composite", new Contribution("c1", location)); node.start(); - try { - System.out.println("Press any key to stop the node."); - System.in.read(); - } catch (IOException e) { - e.printStackTrace(); - } node.stop(); } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockApp.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockApp.java deleted file mode 100644 index a7175e7de7..0000000000 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockApp.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.apache.tuscany.sca.test; - -public class StockApp { - - private StockService service = new StockServiceImpl(); - - public StockService getService() { - return service; - } - - public void setService(StockService service) { - this.service = service; - } - -} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java index 4bb364918a..7dbfb07b74 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java @@ -23,8 +23,6 @@ import org.oasisopen.sca.annotation.Remotable; @Remotable public interface StockService { - String getSymbol(); + String getQuotes(); - Double getValue(); - } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java index 46401748c8..80e7a7e94b 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java @@ -31,14 +31,9 @@ public class StockServiceImpl implements StockService { private Random random = new Random(new Date().getTime()); @Override - public String getSymbol() { - return "ASF"; - } - - @Override - public Double getValue() { + public String getQuotes() { Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); - return Double.valueOf(new DecimalFormat("#.##").format(value)); + return "ASF" + "#" + Double.valueOf(new DecimalFormat("#.##").format(value)); } - + } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/resources/test.composite b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/resources/test.composite index 1815393dfc..15026167be 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/resources/test.composite +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/resources/test.composite @@ -22,9 +22,9 @@ targetNamespace="http://samples" name="Stock"> - - - + + + -- cgit v1.2.3