diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-02 20:25:24 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-02 20:25:24 +0000 |
commit | 1491d3d1338236d1dfba96aa2387075c6a74dbb3 (patch) | |
tree | 503cc7f2a338cff2969794cd9fb9467a0e5763e6 | |
parent | 4fa9cb86b99f147ab8f42588dd03383fd317b090 (diff) |
Implemented comet binding using Atmosphere Framework.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@981675 13f79535-47bb-0310-9956-ffa450edef68
13 files changed, 176 insertions, 274 deletions
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 @@ <version>2.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> + + <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-binding-comet-runtime</artifactId> <name>Apache Tuscany SCA Comet Binding Runtime</name> + <packaging>jar</packaging> <dependencies> - <dependency> <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-binding-comet</artifactId> <version>2.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>org.atmosphere</groupId> + <artifactId>atmosphere-commons</artifactId> + <version>0.7-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.atmosphere</groupId> + <artifactId>atmosphere-runtime</artifactId> + <version>0.7-SNAPSHOT</version> + </dependency> <dependency> <groupId>org.apache.tuscany.sca</groupId> 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<Comet private ServletHost servletHost; public CometBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + System.out.println("Entering CometBindingProviderFactory constructor..."); this.servletHost = ServletHostHelper.getServletHost(extensionPoints); } @@ -42,10 +43,12 @@ public class CometBindingProviderFactory implements BindingProviderFactory<Comet } public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpoint) { + System.out.println("Entering CometBindingProviderFactory#createReferenceBindingProvider..."); return new CometReferenceBindingProvider(endpoint); } public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + System.out.println("Entering CometBindingProviderFactory#createServiceBindingProvider..."); return new CometServiceBindingProvider(endpoint, servletHost); } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java index 6c3801f04d..5ca9583065 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java @@ -19,144 +19,25 @@ package org.apache.tuscany.sca.binding.comet.runtime; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -import org.apache.commons.codec.EncoderException; -import org.apache.commons.codec.net.URLCodec; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; -import org.codehaus.jackson.JsonGenerationException; -import org.codehaus.jackson.JsonParseException; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; -// TODO public class CometInvoker implements Invoker { protected Operation operation; protected EndpointReference endpoint; - protected ObjectMapper mapper; // TODO: share mapper btw invoker and servlet or move to databinding - public CometInvoker(Operation operation, EndpointReference endpoint) { + System.out.println("Entering CometInvoker constructor..."); this.operation = operation; this.endpoint = endpoint; - this.mapper = new ObjectMapper(); } public Message invoke(Message msg) { - try { - - return doInvoke(msg); - - } catch (Exception e) { - throw new RuntimeException(e); - } + System.out.println("Entering CometInvoker#invoke..."); + return null; } - public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException, EncoderException { - String uri = endpoint.getBinding().getURI() + "/" + operation.getName(); - String[] jsonArgs = objectsToJSON((Object[])msg.getBody()); - - String responseJSON = invokeHTTPRequest(uri, jsonArgs); - - Object response = jsonToObjects(responseJSON)[0]; - msg.setBody(response); - - return msg; - } - - protected String invokeHTTPRequest(String url, String[] jsonArgs) throws IOException, EncoderException { - - HttpClient httpclient = new DefaultHttpClient(); - - - URLCodec uc = new URLCodec(); - for (int i=0 ; i<jsonArgs.length; i++) { - if (i == 0) { - url += '?'; - } else { - url += '&'; - } - url += "arg" + i + "="; - url += uc.encode(jsonArgs[i]); - } - - HttpGet httpget = new HttpGet(url); - - HttpResponse response = httpclient.execute(httpget); - - StringBuffer responseJSON = new StringBuffer(); - - HttpEntity entity = response.getEntity(); - - // If the response does not enclose an entity, there is no need - // to worry about connection release - if (entity != null) { - InputStream instream = entity.getContent(); - try { - - BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); - String s = null; - while ((s = reader.readLine()) != null) { - responseJSON.append(s); - } - - } catch (IOException ex) { - - // In case of an IOException the connection will be released - // back to the connection manager automatically - throw ex; - - } catch (RuntimeException ex) { - - // In case of an unexpected exception you may want to abort - // the HTTP request in order to shut down the underlying - // connection and release it back to the connection manager. - httpget.abort(); - throw ex; - - } finally { - - // Closing the input stream will trigger connection release - instream.close(); - - } - - // When HttpClient instance is no longer needed, - // shut down the connection manager to ensure - // immediate deallocation of all system resources - httpclient.getConnectionManager().shutdown(); - } - - return responseJSON.toString(); - } - - protected String[] objectsToJSON(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException { - String[] jsonArgs = new String[msgArgs.length]; - for (int i=0; i<msgArgs.length; i++) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - mapper.writeValue(os , msgArgs[i]); - jsonArgs[i] = os.toString(); - } - return jsonArgs; - } - - protected Object[] jsonToObjects(String jsonRequest) throws JsonParseException, JsonMappingException, IOException { - Class<?> c = new Object[0].getClass(); - Object[] args = (Object[])mapper.readValue("[" + jsonRequest +"]", c); - return args; - } - } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java index c7b96338b3..29f00f634a 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java @@ -31,24 +31,30 @@ public class CometReferenceBindingProvider implements ReferenceBindingProvider { private EndpointReference endpoint; public CometReferenceBindingProvider(EndpointReference endpoint) { + System.out.println("Entering CometReferenceBindingProvider constructor..."); this.endpoint = endpoint; } public Invoker createInvoker(Operation operation) { + System.out.println("Entering CometReferenceBindingProvider#createInvoker..."); return new CometInvoker(operation, endpoint); } public void start() { + System.out.println("Entering CometReferenceBindingProvider#start..."); } public void stop() { + System.out.println("Entering CometReferenceBindingProvider#stop..."); } public InterfaceContract getBindingInterfaceContract() { + System.out.println("Entering CometReferenceBindingProvider#getInterfaceContract..."); return null; } public boolean supportsOneWayInvocation() { - return false; + System.out.println("Entering CometReferenceBindingProvider#supportsOneWayInvocation..."); + return true; } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java index b4f45240bc..3aeff7269f 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -26,29 +26,37 @@ import org.apache.tuscany.sca.interfacedef.InterfaceContract; 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; -// TODO public class CometServiceBindingProvider implements ServiceBindingProvider { private RuntimeEndpoint endpoint; private ServletHost servletHost; public CometServiceBindingProvider(RuntimeEndpoint endpoint, ServletHost servletHost) { + System.out.println("Entering CometServiceBindingProvider constructor..."); this.endpoint = endpoint; this.servletHost = servletHost; } public void start() { + System.out.println("Entering CometServiceBindingProvider#start..."); ComponentService service = endpoint.getService(); Interface serviceInterface = service.getInterfaceContract().getInterface(); for (Operation op : serviceInterface.getOperations()) { - CometServlet servlet = new CometServlet(endpoint, op); + // CometServlet servlet = new CometServlet(endpoint, op); + MeteorServlet servlet = new MeteorServlet(); + servlet.addInitParameter("org.atmosphere.servlet", "org.apache.tuscany.sca.binding.comet.runtime.CometServlet"); String path = endpoint.getBinding().getURI() + "/" + op.getName(); servletHost.addServletMapping(path, servlet); + System.out.println("Adding attributes to servlet context..."); + servlet.getServletContext().setAttribute("org.apache.tuscany.sca.binding.comet.endpoint", endpoint); + servlet.getServletContext().setAttribute("org.apache.tuscany.sca.binding.comet.operation", op); } } public void stop() { + System.out.println("Entering CometServiceBindingProvider#stop..."); ComponentService service = endpoint.getService(); Interface serviceInterface = service.getInterfaceContract().getInterface(); for (Operation op : serviceInterface.getOperations()) { @@ -57,12 +65,13 @@ public class CometServiceBindingProvider implements ServiceBindingProvider { } } - // TODO: Why are these two still on the ServiceBindingProvider interface? public InterfaceContract getBindingInterfaceContract() { + System.out.println("Entering CometServiceBindingProvider#getBindingInterfaceContract..."); return null; } public boolean supportsOneWayInvocation() { + System.out.println("Entering CometServiceBindingProvider#supportsOneWayInvocation..."); return true; } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java index a1d685ac9b..7b8b39dcce 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java @@ -19,140 +19,74 @@ package org.apache.tuscany.sca.binding.comet.runtime; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Comparator; +import java.util.LinkedList; import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import javax.servlet.GenericServlet; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; -import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; -import org.codehaus.jackson.JsonParseException; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; +import org.atmosphere.cpr.BroadcastFilter; +import org.atmosphere.cpr.Broadcaster.SCOPE; +import org.atmosphere.cpr.Meteor; +import org.atmosphere.util.XSSHtmlFilter; -// TODO -public class CometServlet extends GenericServlet { +public class CometServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private static final String METEOR_KEY = "org.apache.tuscany.comet.meteor"; protected transient RuntimeEndpoint wire; protected transient Operation operation; - protected transient ObjectMapper mapper; - + private List<BroadcastFilter> 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<BroadcastFilter>(); + 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<DataType> 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<BroadcastFilter>(); + 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<String> getOrderedParameterNames(ServletRequest servletRequest) { - final String queryString = ((HttpServletRequest)servletRequest).getQueryString(); - - SortedSet<String> sortedNames = new TreeSet<String>(new Comparator<String>(){ - public int compare(String o1, String o2) { - int i = queryString.indexOf(o1); - int j = queryString.indexOf(o2); - return i - j; - }}); - - Set<String> 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<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onSuspend: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onResume(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onResume: " + event.getResource().getRequest().getRemoteAddr()); + } + + public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onDisconnect: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> 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 = "<script type='text/javascript'>\n"; + private static final String END_SCRIPT_TAG = "</script>\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"> - <component name="StockApp"> - <implementation.java class="org.apache.tuscany.sca.test.StockApp"/> - <service name="service"> + <component name="test"> + <implementation.java class="org.apache.tuscany.sca.test.StockServiceImpl"/> + <service name="StockService"> <interface.java interface="org.apache.tuscany.sca.test.StockService"/> <tuscany:binding.comet/> </service> |