summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/modules
diff options
context:
space:
mode:
authorfmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68>2010-08-15 07:25:44 +0000
committerfmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68>2010-08-15 07:25:44 +0000
commitce3c5536c9af5f535d0057498d0d76848b7a4cb5 (patch)
tree8ed58bca59499a71f87bab57d0327ed195b8ad47 /sca-java-2.x/contrib/modules
parentd6451b81703c809abcd0f51e74abdba7c732b513 (diff)
Added Javascript proxy generation.
Added passing parameters through JSON. Added Gson and jquery-json as dependencies. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@985622 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/modules')
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml179
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java5
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java7
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java69
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java72
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java13
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/cometComponentContext.js26
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/jquery.json-2.2.min.js31
-rw-r--r--sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/org.apache.tuscany.sca.cometComponentContext.js55
9 files changed, 301 insertions, 156 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 716cf95c68..b63a97f3ac 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
@@ -1,94 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!--
- * 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.
--->
+<!-- * 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. -->
<project>
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-modules</artifactId>
- <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>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-modules</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
- <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.6.1</version>
- </dependency>
- <dependency>
- <groupId>org.atmosphere</groupId>
- <artifactId>atmosphere-runtime</artifactId>
- <version>0.6.1</version>
- </dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-binding-comet-runtime</artifactId>
+ <name>Apache Tuscany SCA Comet Binding Runtime</name>
+ <packaging>jar</packaging>
- <dependency>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-host-http</artifactId>
- <version>2.0-SNAPSHOT</version>
- </dependency>
+ <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.6.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.atmosphere</groupId>
+ <artifactId>atmosphere-runtime</artifactId>
+ <version>0.6.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-host-http</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-web-api</artifactId>
- <version>6.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-host-jetty</artifactId>
- <version>2.0-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
+ <dependency>
+ <groupId>javax</groupId>
+ <artifactId>javaee-web-api</artifactId>
+ <version>6.0</version>
+ <scope>provided</scope>
+ </dependency>
- <dependency>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-node-impl</artifactId>
- <version>2.0-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-host-jetty</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-implementation-java-runtime</artifactId>
- <version>2.0-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-node-impl</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.apache.tuscany.sca</groupId>
- <artifactId>tuscany-sca-api</artifactId>
- <version>2.0-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-implementation-java-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-sca-api</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>1.4</version>
+ <scope>compile</scope>
+ </dependency>
+
+
+ </dependencies>
+
+ <repositories>
+ <repository>
+ <id>gson</id>
+ <url>http://google-gson.googlecode.com/svn/mavenrepo</url>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ </repository>
+ </repositories>
</project>
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 5ad23097b5..1d295d9b4d 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
@@ -20,6 +20,7 @@
package org.apache.tuscany.sca.binding.comet.runtime;
import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.binding.comet.runtime.javascript.JavascriptGenerator;
import org.apache.tuscany.sca.host.http.ServletHost;
import org.apache.tuscany.sca.interfacedef.Interface;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
@@ -40,7 +41,9 @@ public class CometServiceBindingProvider implements ServiceBindingProvider {
public void start() {
ComponentService service = endpoint.getService();
Interface serviceInterface = service.getInterfaceContract().getInterface();
+ JavascriptGenerator.generateServiceProxy(service);
for (Operation operation : serviceInterface.getOperations()) {
+ JavascriptGenerator.generateMethodProxy(service, operation);
ServletFactory.registerServlet(servletHost, endpoint, operation);
}
}
@@ -49,7 +52,7 @@ public class CometServiceBindingProvider implements ServiceBindingProvider {
ComponentService service = endpoint.getService();
Interface serviceInterface = service.getInterfaceContract().getInterface();
for (Operation op : serviceInterface.getOperations()) {
- String path = endpoint.getBinding().getURI() + "/" + op.getName();
+ String path = service.getName() + "/" + op.getName();
servletHost.removeServletMapping(path);
}
}
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java
index 62e90693fa..df1137cb63 100644
--- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java
@@ -3,6 +3,7 @@ package org.apache.tuscany.sca.binding.comet.runtime;
import java.util.HashMap;
import java.util.Map;
+import org.apache.tuscany.sca.binding.comet.runtime.javascript.JavascriptGenerator;
import org.apache.tuscany.sca.host.http.ServletHost;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
@@ -15,6 +16,8 @@ public class ServletFactory {
private static final String JS_PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.javascript";
public static final String ENDPOINTS_KEY = "org.apache.tuscany.sca.binding.comet.endpoints";
public static final String OPERATIONS_KEY = "org.apache.tuscany.sca.binding.comet.operations";
+ public static final String JS_KEY = "org.apache.tuscany.sca.binding.comet.javascript";
+
public static final String PATH = "/tuscany-comet/*";
public static final String JS_PATH = "/org.apache.tuscany.sca.cometComponentContext.js/*";
@@ -33,8 +36,7 @@ public class ServletFactory {
Map<String, Operation> operations = new HashMap<String, Operation>();
cometServlet.getServletContext().setAttribute(OPERATIONS_KEY, operations);
}
- String url = endpoint.getBinding().getURI() + "/" + operation.getName();
- System.out.println("Adding endpoint and operation for url: " + url);
+ String url = "/" + endpoint.getService().getName() + "/" + operation.getName();
Map<String, RuntimeEndpoint> endpoints =
(Map<String, RuntimeEndpoint>)cometServlet.getServletContext().getAttribute(ENDPOINTS_KEY);
endpoints.put(url, endpoint);
@@ -46,6 +48,7 @@ public class ServletFactory {
javascriptServlet = new AtmosphereServlet();
javascriptServlet.addInitParameter(PACKAGE_KEY, JS_PACKAGE_VALUE);
servletHost.addServletMapping(JS_PATH, javascriptServlet);
+ javascriptServlet.getServletContext().setAttribute(JS_KEY, JavascriptGenerator.javascript);
}
}
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java
index 88f0460839..c1ffc6ec92 100644
--- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java
@@ -1,6 +1,8 @@
package org.apache.tuscany.sca.binding.comet.runtime.handler;
import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
@@ -12,8 +14,8 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
-import org.apache.tuscany.sca.binding.comet.runtime.EventsLogger;
import org.apache.tuscany.sca.binding.comet.runtime.ServletFactory;
+import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
import org.atmosphere.annotation.Broadcast;
@@ -22,6 +24,7 @@ import org.atmosphere.cpr.DefaultBroadcaster;
import org.atmosphere.jersey.Broadcastable;
import org.atmosphere.jersey.SuspendResponse;
+import com.google.gson.Gson;
import com.sun.jersey.spi.container.servlet.PerSession;
@Path("/")
@@ -32,34 +35,72 @@ public class CometBindingHandler {
private Broadcaster broadcaster;
private Map<String, RuntimeEndpoint> endpoints;
private Map<String, Operation> operations;
+ private Gson gson;
@Context
private ServletContext sc;
@GET
public SuspendResponse<String> connect() {
- System.out.println("Entering connect...");
broadcaster = new DefaultBroadcaster();
endpoints = (Map<String, RuntimeEndpoint>)sc.getAttribute(ServletFactory.ENDPOINTS_KEY);
operations = (Map<String, Operation>)sc.getAttribute(ServletFactory.OPERATIONS_KEY);
- return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(broadcaster).outputComments(true)
- .addListener(new EventsLogger()).build();
+ gson = new Gson();
+ return new SuspendResponse.SuspendResponseBuilder<String>()
+ .broadcaster(broadcaster)
+ .outputComments(true)
+ //.addListener(new EventsLogger()) // use for logging or debugging when needed
+ .build();
}
@POST
- @Path("/{component}/{service}/{method}")
+ @Path("/{service}/{method}")
@Broadcast
- public Broadcastable callAndRespond(@PathParam("component") String component,
- @PathParam("service") String service,
+ public Broadcastable callAndRespond(@PathParam("service") String service,
@PathParam("method") String method,
- @FormParam("callback") String callbackMethod) throws InvocationTargetException {
- String url = "/" + component + "/" + service + "/" + method;
- System.out.println("Entered callAndRespond with url: " + url);
- System.out.println("Callback method: " + callbackMethod);
+ @FormParam("callback") String callbackMethod,
+ @FormParam("params") String jsonData) throws InvocationTargetException {
+ String url = "/" + service + "/" + method;
RuntimeEndpoint wire = endpoints.get(url);
Operation operation = operations.get(url);
- Object response = wire.invoke(operation, new Object[] {});
- // TODO: replace with JSON
- return new Broadcastable(callbackMethod + "#" + response.toString(), "", broadcaster);
+ Object[] args = new Object[operation.getInputType().getLogical().size()];
+ String[] json = parseArray(jsonData);
+ int index = 0;
+ for (DataType<?> dataType : operation.getInputType().getLogical()) {
+ args[index] = gson.fromJson(json[index], dataType.getPhysical());
+ index++;
+ }
+ Object response = wire.invoke(operation, args);
+ return new Broadcastable(callbackMethod + "($.secureEvalJSON('" + gson.toJson(response) + "'))", "", broadcaster);
+ }
+
+ private String[] parseArray(String jsonArray) {
+ List<String> objects = new ArrayList<String>();
+ int bracketNum = 0;
+ int parNum = 0;
+ int startPos = 1;
+ for (int i = 0; i < jsonArray.length(); i++) {
+ switch (jsonArray.charAt(i)) {
+ case '{':
+ bracketNum++;
+ break;
+ case '}':
+ bracketNum--;
+ break;
+ case '[':
+ parNum++;
+ break;
+ case ']':
+ parNum--;
+ break;
+ case ',':
+ if (bracketNum == 0 && parNum == 1) {
+ objects.add(jsonArray.substring(startPos, i));
+ startPos = i + 1;
+ }
+ }
+ }
+ objects.add(jsonArray.substring(startPos, jsonArray.length() - 1));
+ return objects.toArray(new String[] {});
}
}
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java
new file mode 100644
index 0000000000..ffd300cb01
--- /dev/null
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java
@@ -0,0 +1,72 @@
+package org.apache.tuscany.sca.binding.comet.runtime.javascript;
+
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.interfacedef.Operation;
+
+public class JavascriptGenerator {
+
+ public static StringBuffer javascript = new StringBuffer();
+ private static final String COMPONENT_CONTEXT_NAMESPACE = "cometComponentContext";
+ private static final String TUSCANY_COMET_NAMESPACE = "tuscanyComet";
+
+ public static void generateServiceProxy(ComponentService service) {
+ javascript.append(COMPONENT_CONTEXT_NAMESPACE + "." + service.getName() + " = new Object();\n");
+ }
+
+ public static void generateMethodProxy(ComponentService service, Operation operation) {
+ javascript.append(COMPONENT_CONTEXT_NAMESPACE + "."
+ + service.getName()
+ + "."
+ + operation.getName()
+ + " = function(");
+ for (int i = 0; i < operation.getInputType().getLogical().size(); i++) {
+ javascript.append("p" + i + ", ");
+ }
+ javascript.append("callbackMethod) {\n");
+ javascript.append(" var params = [];\n");
+ for (int i = 0; i < operation.getInputType().getLogical().size(); i++) {
+ javascript.append(" params.push(p" + i + ");\n");
+ }
+ javascript.append(" " + TUSCANY_COMET_NAMESPACE
+ + ".callAsync('"
+ + service.getName()
+ + "/"
+ + operation.getName()
+ + "', $.toJSON(params), callbackMethod);\n");
+ javascript.append("}\n");
+ }
+
+}
+
+// cometComponentContext.c1 = new Object();
+// cometComponentContext.c1.TemperatureService = new Object();
+// cometComponentContext.c1.TemperatureService.getTemperatureCelsius =
+// function(p0, callbackMethod) {
+// tuscanyComet.callAsync('c1/TemperatureService/getTemperatureCelsius', x,
+// callbackMethod);
+// }
+// cometComponentContext.c1.TemperatureService.getTemperatureFahrenheit =
+// function(callbackMethod) {
+// tuscanyComet.callAsync('c1/TemperatureService/getTemperatureFahrenheit', x,
+// callbackMethod);
+// }
+//
+// if (cometComponentContext.c1 == null) {
+// cometComponentContext.c1 = new Object();
+// }
+// cometComponentContext.c1.HumidityService = new Object();
+// cometComponentContext.c1.HumidityService.getHumidity = function(p0,
+// callbackMethod) {
+// var params = [];
+// params.push(p0);
+// tuscanyComet.callAsync('c1/HumidityService/getHumidity', $.toJSON(params),
+// callbackMethod);
+// }
+//
+// cometComponentContext.c2 = new Object();
+// cometComponentContext.c2.PrecipitationService = new Object();
+// cometComponentContext.c2.PrecipitationService.getPrecipitation =
+// function(callbackMethod) {
+// tuscanyComet.callAsync('c2/PrecipitationService/getPrecipitation', x,
+// callbackMethod);
+// }
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java
index d6ad8a018f..6df45e4ca8 100644
--- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java
@@ -1,20 +1,28 @@
package org.apache.tuscany.sca.binding.comet.runtime.javascript;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;
+import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.PathSegment;
+import org.apache.tuscany.sca.binding.comet.runtime.ServletFactory;
+
@Path("/")
@Produces("text/javascript")
public class JavascriptResource {
private static final String[] DEPENDENCIES = {"/jquery-1.4.2.min.js", "/jquery.atmosphere.js",
- "/org.apache.tuscany.sca.cometComponentContext.js"};
+ "/jquery.json-2.2.min.js", "/cometComponentContext.js"};
+
+ @Context
+ private ServletContext sc;
@GET
public InputStream getJavascript(@PathParam("file") PathSegment fileName) {
@@ -26,6 +34,7 @@ public class JavascriptResource {
stream = new SequenceInputStream(stream, getClass().getResourceAsStream(dependency));
}
}
- return stream;
+ String generatedJs = ((StringBuffer)sc.getAttribute(ServletFactory.JS_KEY)).toString();
+ return new SequenceInputStream(stream, new ByteArrayInputStream(generatedJs.getBytes()));
}
}
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/cometComponentContext.js b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/cometComponentContext.js
new file mode 100644
index 0000000000..7352a2215c
--- /dev/null
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/cometComponentContext.js
@@ -0,0 +1,26 @@
+var tuscanyComet = {
+ appUrl: 'tuscany-comet',
+ connectedEndpoint : null,
+ connect : function(transport) {
+ $.atmosphere.subscribe(document.location.toString() + this.appUrl,
+ this.callback,
+ $.atmosphere.request = {
+ transport : transport
+ });
+ this.connectedEndpoint = $.atmosphere.response;
+ },
+ callAsync : function(url, params, callbackMethod) {
+ this.connectedEndpoint.push(document.location.toString()
+ + this.appUrl + '/' + url,
+ null,
+ $.atmosphere.request = {
+ method : 'POST',
+ data : 'callback=' + callbackMethod.name + '&params=' + params
+ });
+ },
+ callback : function(response) {
+ eval(response.responseBody);
+ }
+};
+
+var cometComponentContext = new Object();
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/jquery.json-2.2.min.js b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/jquery.json-2.2.min.js
new file mode 100644
index 0000000000..bad4a0afa0
--- /dev/null
+++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/jquery.json-2.2.min.js
@@ -0,0 +1,31 @@
+
+(function($){$.toJSON=function(o)
+{if(typeof(JSON)=='object'&&JSON.stringify)
+return JSON.stringify(o);var type=typeof(o);if(o===null)
+return"null";if(type=="undefined")
+return undefined;if(type=="number"||type=="boolean")
+return o+"";if(type=="string")
+return $.quoteString(o);if(type=='object')
+{if(typeof o.toJSON=="function")
+return $.toJSON(o.toJSON());if(o.constructor===Date)
+{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
+hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
+if(o.constructor===Array)
+{var ret=[];for(var i=0;i<o.length;i++)
+ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
+var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
+name='"'+k+'"';else if(type=="string")
+name=$.quoteString(k);else
+continue;if(typeof o[k]=="function")
+continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
+return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
+{if(typeof(JSON)=='object'&&JSON.parse)
+return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
+{if(typeof(JSON)=='object'&&JSON.parse)
+return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
+return eval("("+src+")");else
+throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
+{if(string.match(_escapeable))
+{return'"'+string.replace(_escapeable,function(a)
+{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
+return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery); \ No newline at end of file
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/org.apache.tuscany.sca.cometComponentContext.js b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/org.apache.tuscany.sca.cometComponentContext.js
deleted file mode 100644
index c8dc19d15a..0000000000
--- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/resources/org.apache.tuscany.sca.cometComponentContext.js
+++ /dev/null
@@ -1,55 +0,0 @@
-// TODO: Try isolating functional code one piece at a time by using it like in the inital version
-var tuscanyComet = {
- connectedEndpoint : null,
- connect : function(transport) {
- $.atmosphere.subscribe(document.location.toString() + 'tuscany-comet',
- this.callback,
- $.atmosphere.request = {
- transport : transport
- });
- this.connectedEndpoint = $.atmosphere.response;
- },
- callAsync : function(url, callbackMethod) {
- this.connectedEndpoint.push(document.location.toString()
- + 'tuscany-comet/' + url,
- null,
- $.atmosphere.request = {
- method : 'POST',
- data : 'callback=' + callbackMethod.name
- });
- },
- callback : function(response) {
- var data = response.responseBody;
- if (data.indexOf('#') != -1) {
- var callbackMethod = data.substring(0, data.indexOf('#'));
- data = data.substring(data.indexOf('#') + 1);
- eval(callbackMethod + '("' + data + '");');
- }
- }
-};
-
-// To be generated
-var cometComponentContext = {
- c1 : {
- TemperatureService : {
- getTemperatureCelsius : function(callbackMethod) {
- tuscanyComet.callAsync('c1/TemperatureService/getTemperatureCelsius', callbackMethod);
- },
- getTemperatureFahrenheit : function(callbackMethod) {
- tuscanyComet.callAsync('c1/TemperatureService/getTemperatureFahrenheit', callbackMethod);
- }
- },
- HumidityService : {
- getHumidity : function(callbackMethod) {
- tuscanyComet.callAsync('c1/HumidityService/getHumidity', callbackMethod);
- }
- }
- },
- c2 : {
- PrecipitationService : {
- getPrecipitation : function(callbackMethod) {
- tuscanyComet.callAsync('c2/PrecipitationService/getPrecipitation', callbackMethod);
- }
- }
- }
-} \ No newline at end of file