Author: fmoga
Date: Sun Jul 24 13:32:46 2011
New Revision: 1150357
URL: http://svn.apache.org/viewvc?rev=1150357&view=rev
Log:
Migrate to embedded Jetty as websocket server. Add javascript service proxy
injection.
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebSocket.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingDispatcher.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingMessage.java
- copied, changed from r1148438,
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingRequest.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketServer.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js
Removed:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/deploy-monsoon.sh
tuscany/sca-java-2.x/contrib/modules/binding-websocket/monsoon-repo/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingRequest.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingResponse.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketOperationDispatcher.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketRequestHandler.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/test/java/registration/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/test/java/salutes/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/test/java/test/
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/test/resources/registration.composite
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/test/resources/salutes.composite
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/pom.xml
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceInvoker.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/binding-websocket.xsd
Modified: tuscany/sca-java-2.x/contrib/modules/binding-websocket/pom.xml
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/pom.xml?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/binding-websocket/pom.xml (original)
+++ tuscany/sca-java-2.x/contrib/modules/binding-websocket/pom.xml Sun Jul 24
13:32:46 2011
@@ -42,11 +42,11 @@
</dependency>
<dependency>
- <groupId>org.apache</groupId>
- <artifactId>websocket</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <scope>compile</scope>
- </dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-websocket</artifactId>
+ <version>8.0.0.M3</version>
+ <scope>compile</scope>
+ </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
@@ -64,11 +64,4 @@
</dependencies>
- <repositories>
- <repository>
- <id>monsoon-local</id>
- <url>file://${basedir}/monsoon-repo</url>
- </repository>
- </repositories>
-
</project>
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java
Sun Jul 24 13:32:46 2011
@@ -28,6 +28,7 @@ import org.apache.tuscany.sca.provider.B
public class WebsocketBinding extends BaseBindingImpl {
public static final QName TYPE = new QName(SCA11_TUSCANY_NS,
"binding.websocket");
+ private String port;
public WebsocketBinding() {
}
@@ -37,4 +38,12 @@ public class WebsocketBinding extends Ba
return TYPE;
}
+ public String getPort() {
+ return port;
+ }
+
+ public void setPort(String port) {
+ this.port = port;
+ }
+
}
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java
Sun Jul 24 13:32:46 2011
@@ -33,6 +33,18 @@ public class JSONUtil {
private static Gson gson = new Gson();
+ public static String encodeMessage(WebSocketBindingMessage request) {
+ return gson.toJson(request);
+ }
+
+ public static WebSocketBindingMessage decodeMessage(String jsonRequest) {
+ return gson.fromJson(jsonRequest, WebSocketBindingMessage.class);
+ }
+
+ public static String encodePayload(Object payload) {
+ return gson.toJson(payload);
+ }
+
/**
* Convert request parameters from JSON to operation parameter types.
*
@@ -42,7 +54,7 @@ public class JSONUtil {
* the operation to invoke
* @return an array of objects
*/
- public static Object[] decodeJsonParamsForOperation(String jsonData,
Operation operation) {
+ public static Object[] decodePayloadForOperation(String jsonData,
Operation operation) {
Object[] args = new
Object[operation.getInputType().getLogical().size()];
final String[] json = parseArray(jsonData);
int index = 0;
@@ -100,49 +112,4 @@ public class JSONUtil {
private JSONUtil() {
}
- /**
- * Converts a Java object to JSON format.
- *
- * @param response
- * the response to convert
- * @return the object in JSON format
- */
- public static String encodeResponse(Object response) {
- return gson.toJson(response);
- }
-
- /**
- * Convert request parameters as JSON array.
- *
- * @param params
- * request parameters
- * @return request parameters as JSON array
- */
- public static String encodeRequestParams(Object[] params) {
- StringBuilder builder = new StringBuilder();
- for (int index = 0; index < params.length; index++) {
- Object param = params[index];
- builder.append(index == 0 ? "[" : ",");
- builder.append(gson.toJson(param));
- }
- builder.append("]");
- return builder.toString();
- }
-
- public static String encodeRequest(WebSocketBindingRequest request) {
- return gson.toJson(request);
- }
-
- public static WebSocketBindingRequest decodeRequest(String jsonRequest) {
- return gson.fromJson(jsonRequest, WebSocketBindingRequest.class);
- }
-
- public static WebSocketBindingResponse decodeResponse(String
operationResponse) {
- return gson.fromJson(operationResponse,
WebSocketBindingResponse.class);
- }
-
- public static Object decodeResponsePayload(String payload, Class<?>
returnType) {
- return gson.fromJson(payload, returnType);
- }
-
}
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,99 @@
+/*
+ * 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.
+ */
+
+package org.apache.tuscany.sca.binding.websocket.runtime;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+
+public class JavascriptGenerator {
+
+ private final static String CONTEXT = "this.WebsocketComponentContext";
+ private static final String LF = "\n";
+ private static StringBuilder builder = new StringBuilder();
+
+ public static void generateServiceProxy(String component, String service,
List<Operation> operations, int port) {
+ if (builder.length() == 0) {
+ builder.append(CONTEXT).append("={};").append(LF);
+ }
+ builder.append("if(!" + CONTEXT + "." + component + ")" + CONTEXT +
"." + component + "={};").append(LF);
+ builder.append(CONTEXT + "." + component + "." + service +
"={};").append(LF);
+ for (Operation operation : operations) {
+ builder.append(
+ CONTEXT + "." + component + "." + service + "." +
operation.getName() + "="
+ + generateFunctionHeader(operation)).append(LF);
+ builder.append(generateFunctionContent(port, component, service,
operation)).append(LF);
+ builder.append("};").append(LF);
+ }
+ }
+
+ private static String generateFunctionHeader(Operation operation) {
+ String header = "function(";
+ for (int i = 0; i < operation.getInputType().getLogical().size(); i++)
{
+ if (i > 0)
+ header += ",";
+ header += "p" + i;
+ }
+ header += ") {";
+ return header;
+ }
+
+ private static String generateFunctionContent(int port, String component,
String service, Operation operation) {
+ String content = "sendMessage(" + port + ",'" + component + "." +
service + "." + operation.getName() + "',[";
+ for (int i = 0; i < operation.getInputType().getLogical().size(); i++)
{
+ if (i > 0)
+ content += ",";
+ content += "p" + i;
+ }
+ content += "]);";
+ return content;
+ }
+
+ public static String getServiceProxies() {
+ return builder.toString();
+ }
+
+ public static void clear() {
+ builder.setLength(0);
+ }
+
+ private JavascriptGenerator() {
+ }
+
+}
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,109 @@
+/*
+ * 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.
+ */
+
+package org.apache.tuscany.sca.binding.websocket.runtime;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class JavascriptResourceServlet extends HttpServlet {
+
+ private static final String JSON_PLUGIN_PATH = "js/jquery.json-2.2.min.js";
+ private static final String WEBSOCKET_TOOLKIT_PATH =
"js/TuscanyWebsocketToolkit.js";
+
+ private String jsonPlugin;
+ private String websocketToolkit;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
+ try {
+ resp.setContentType("text/javascript");
+ OutputStream os = resp.getOutputStream();
+ os.write(getJsonPlugin().getBytes());
+ os.write("var Tuscany = new function() {\n".getBytes());
+ os.write(getWebsocketToolkit().getBytes());
+ os.write(JavascriptGenerator.getServiceProxies().getBytes());
+ os.write("}\n".getBytes());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private String getJsonPlugin() {
+ if (jsonPlugin == null) {
+ jsonPlugin = getResource(JSON_PLUGIN_PATH);
+ }
+ return jsonPlugin;
+ }
+
+ private String getWebsocketToolkit() {
+ if (websocketToolkit == null) {
+ websocketToolkit = getResource(WEBSOCKET_TOOLKIT_PATH);
+ }
+ return websocketToolkit;
+ }
+
+ private String getResource(String path) {
+ InputStream is = getClass().getClassLoader().getResourceAsStream(path);
+ BufferedReader r = new BufferedReader(new InputStreamReader(is));
+ StringBuilder builder = new StringBuilder();
+ try {
+ String line = null;
+ while ((line = r.readLine()) != null) {
+ builder.append(line + "\n");
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ r.close();
+ } catch (IOException ignored) {
+ }
+ }
+ return builder.toString();
+ }
+
+}
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebSocket.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebSocket.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebSocket.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebSocket.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,61 @@
+/*
+ * 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.websocket.runtime;
+
+import java.io.IOException;
+
+import org.eclipse.jetty.websocket.WebSocket;
+
+public class TuscanyWebSocket implements WebSocket, WebSocket.OnTextMessage {
+
+ private Connection connection;
+ private WebSocketBindingDispatcher dispatcher;
+
+ public TuscanyWebSocket(WebSocketBindingDispatcher dispatcher) {
+ this.dispatcher = dispatcher;
+ }
+
+ @Override
+ public void onOpen(Connection connection) {
+ this.connection = connection;
+ }
+
+ @Override
+ public void onMessage(String jsonRequest) {
+ WebSocketBindingMessage request = JSONUtil.decodeMessage(jsonRequest);
+ WebsocketServiceInvoker invoker =
dispatcher.dispatch(request.getOperation());
+ if (invoker == null) {
+ throw new RuntimeException("No operation found for " +
request.getOperation());
+ } else {
+ WebSocketBindingMessage response = invoker.invokeSync(request);
+ String jsonResponse = JSONUtil.encodeMessage(response);
+ try {
+ connection.sendMessage(jsonResponse);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ @Override
+ public void onClose(int closeCode, String message) {
+ }
+
+}
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingDispatcher.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingDispatcher.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingDispatcher.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingDispatcher.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,39 @@
+/*
+ * 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.websocket.runtime;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
+
+public class WebSocketBindingDispatcher {
+
+ private Map<String, WebsocketServiceInvoker> invokers = new
HashMap<String, WebsocketServiceInvoker>();
+
+ public void addOperation(String uri, RuntimeEndpoint endpoint, Operation
operation) {
+ System.out.println("Adding " + uri);
+ invokers.put(uri, new WebsocketServiceInvoker(operation, endpoint));
+ }
+
+ public WebsocketServiceInvoker dispatch(String uri) {
+ return invokers.get(uri);
+ }
+}
\ No newline at end of file
Copied:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingMessage.java
(from r1148438,
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingRequest.java)
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingMessage.java?p2=tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingMessage.java&p1=tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingRequest.java&r1=1148438&r2=1150357&rev=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingRequest.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketBindingMessage.java
Sun Jul 24 13:32:46 2011
@@ -18,32 +18,22 @@
*/
package org.apache.tuscany.sca.binding.websocket.runtime;
-public class WebSocketBindingRequest {
+public class WebSocketBindingMessage {
- private String requestId;
- private String uri;
+ private String operation;
private String payload;
- public WebSocketBindingRequest(String requestId, String uri, String
payload) {
- this.requestId = requestId;
- this.uri = uri;
+ public WebSocketBindingMessage(String operation, String payload) {
+ this.operation = operation;
this.payload = payload;
}
- public String getRequestId() {
- return requestId;
+ public String getOperation() {
+ return operation;
}
- public void setRequestId(String requestId) {
- this.requestId = requestId;
- }
-
- public String getUri() {
- return uri;
- }
-
- public void setUri(String uri) {
- this.uri = uri;
+ public void setOperation(String operation) {
+ this.operation = operation;
}
public String getPayload() {
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketServer.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketServer.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketServer.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebSocketServer.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,56 @@
+/*
+ * 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.websocket.runtime;
+
+import java.net.URISyntaxException;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
+import org.eclipse.jetty.websocket.WebSocket;
+import org.eclipse.jetty.websocket.WebSocketHandler;
+
+public class WebSocketServer extends Server {
+
+ private WebSocketBindingDispatcher dispatcher;
+
+ public WebSocketServer(int port) throws URISyntaxException {
+ SelectChannelConnector connector = new SelectChannelConnector();
+ connector.setPort(port);
+ addConnector(connector);
+
+ setHandler(new WebSocketHandler() {
+
+ @Override
+ public WebSocket doWebSocketConnect(HttpServletRequest request,
String protocol) {
+ System.out.println("Connection established");
+ return new TuscanyWebSocket(dispatcher);
+ }
+ });
+
+ dispatcher = new WebSocketBindingDispatcher();
+ }
+
+ public WebSocketBindingDispatcher getDispatcher() {
+ return dispatcher;
+ }
+
+}
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java
Sun Jul 24 13:32:46 2011
@@ -21,6 +21,8 @@ package org.apache.tuscany.sca.binding.w
import org.apache.tuscany.sca.binding.websocket.WebsocketBinding;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.host.http.ServletHostHelper;
import org.apache.tuscany.sca.provider.BindingProviderFactory;
import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
import org.apache.tuscany.sca.provider.ServiceBindingProvider;
@@ -29,7 +31,10 @@ import org.apache.tuscany.sca.runtime.Ru
public class WebsocketBindingProviderFactory implements
BindingProviderFactory<WebsocketBinding> {
+ private ServletHost servletHost;
+
public WebsocketBindingProviderFactory(ExtensionPointRegistry
extensionPoints) {
+ this.servletHost = ServletHostHelper.getServletHost(extensionPoints);
}
public Class<WebsocketBinding> getModelType() {
@@ -41,7 +46,7 @@ public class WebsocketBindingProviderFac
}
public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint
endpoint) {
- return new WebsocketServiceBindingProvider(endpoint);
+ return new WebsocketServiceBindingProvider(endpoint, servletHost);
}
}
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java
Sun Jul 24 13:32:46 2011
@@ -41,7 +41,6 @@ public class WebsocketReferenceBindingPr
}
public void stop() {
- WebsocketReferenceInvoker.shutdown();
}
public InterfaceContract getBindingInterfaceContract() {
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceInvoker.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceInvoker.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceInvoker.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceInvoker.java
Sun Jul 24 13:32:46 2011
@@ -19,26 +19,13 @@
package org.apache.tuscany.sca.binding.websocket.runtime;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.channels.SocketChannel;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
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.apache.websocket.WebSocket;
-import org.apache.websocket.WebSocketConnector;
public class WebsocketReferenceInvoker implements Invoker {
- // TODO add timeout mechanism for persistent connections
- private static ConcurrentMap<String, WebSocket<SocketChannel>>
persistentWebsockets = new ConcurrentHashMap<String,
WebSocket<SocketChannel>>();
-
protected Operation operation;
protected EndpointReference endpoint;
@@ -48,58 +35,7 @@ public class WebsocketReferenceInvoker i
}
public Message invoke(Message msg) {
- try {
- WebSocket<SocketChannel> websocket =
initWebsocketConnection(endpoint.getBinding().getURI());
- return doInvoke(msg, websocket);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private WebSocket<SocketChannel> initWebsocketConnection(String uri)
throws IOException, URISyntaxException {
- WebSocket<SocketChannel> websocket = null;
- synchronized (persistentWebsockets) {
- websocket = persistentWebsockets.get(uri);
- if (websocket == null) {
- WebSocketConnector connector = new WebSocketConnector();
- websocket = connector.connect(new URI(uri), null,
"apache-tuscany", null);
- persistentWebsockets.put(uri, websocket);
- }
- }
- return websocket;
- }
-
- public Message doInvoke(Message msg, WebSocket<SocketChannel> websocket)
throws IOException {
- String componentName =
endpoint.getTargetEndpoint().getComponent().getName();
- String serviceName =
endpoint.getTargetEndpoint().getService().getName();
- String operationName = operation.getName();
- String uri = componentName + "/" + serviceName + "/" + operationName;
- String payload = JSONUtil.encodeRequestParams((Object[])
msg.getBody());
- WebSocketBindingRequest request = new
WebSocketBindingRequest(UUID.randomUUID().toString(), uri, payload);
-
- String operationResponse = invokeViaWebsocket(websocket,
JSONUtil.encodeRequest(request));
-
- WebSocketBindingResponse response =
JSONUtil.decodeResponse(operationResponse);
- Class<?> returnType =
operation.getOutputType().getLogical().get(0).getPhysical();
- Object invocationResponse =
JSONUtil.decodeResponsePayload(response.getPayload(), returnType);
- msg.setBody(invocationResponse);
- return msg;
- }
-
- private String invokeViaWebsocket(WebSocket<SocketChannel> websocket,
String request) throws IOException {
- websocket.sendText(request);
- return websocket.receiveText();
- }
-
- public static void shutdown() {
- for (WebSocket<SocketChannel> websocket :
persistentWebsockets.values()) {
- try {
- websocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- persistentWebsockets.clear();
+ throw new RuntimeException("Not implemented yet");
}
}
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java
Sun Jul 24 13:32:46 2011
@@ -19,65 +19,75 @@
package org.apache.tuscany.sca.binding.websocket.runtime;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
+import org.apache.tuscany.sca.binding.websocket.WebsocketBinding;
+import org.apache.tuscany.sca.host.http.ServletHost;
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.apache.websocket.ServerWebSocket;
public class WebsocketServiceBindingProvider implements ServiceBindingProvider
{
- private static Map<String, WebSocketOperationDispatcher> dispatchers = new
HashMap<String, WebSocketOperationDispatcher>();
-
+ private static final String JAVASCRIPT_RESOURCE_PATH =
"/org.apache.tuscany.sca.WebsocketComponentContext.js";
+ private static Map<Integer, WebSocketServer> servers = new
HashMap<Integer, WebSocketServer>();
private RuntimeEndpoint endpoint;
+ private ServletHost servletHost;
- public WebsocketServiceBindingProvider(RuntimeEndpoint endpoint) {
+ public WebsocketServiceBindingProvider(RuntimeEndpoint endpoint,
ServletHost servletHost) {
this.endpoint = endpoint;
+ this.servletHost = servletHost;
}
public void start() {
- String uri = endpoint.getBinding().getURI();
- WebSocketOperationDispatcher dispatcher = initDispatcherForURI(uri);
- String component = endpoint.getComponent().getName();
- String service = endpoint.getService().getName();
- for (Operation op :
getBindingInterfaceContract().getInterface().getOperations()) {
- String operation = op.getName();
- dispatcher.addOperation(component + "/" + service + "/" +
operation, endpoint, op);
+ WebsocketBinding binding = (WebsocketBinding) endpoint.getBinding();
+ int port = Integer.parseInt(binding.getPort());
+ try {
+ WebSocketServer server = initServerForURI(port);
+ String component = endpoint.getComponent().getName();
+ String service = endpoint.getService().getName();
+ for (Operation op :
getBindingInterfaceContract().getInterface().getOperations()) {
+ String operation = op.getName();
+ server.getDispatcher().addOperation(component + "." + service
+ "." + operation, endpoint, op);
+ }
+ JavascriptGenerator.generateServiceProxy(component, service,
getBindingInterfaceContract().getInterface()
+ .getOperations(), port);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
}
+ initJavascriptResource();
+ }
+ private WebSocketServer initServerForURI(int port) throws Exception {
+ WebSocketServer server = servers.get(port);
+ if (server == null) {
+ server = new WebSocketServer(port);
+ server.start();
+ servers.put(port, server);
+ }
+ return server;
}
- private WebSocketOperationDispatcher initDispatcherForURI(String uri) {
- WebSocketOperationDispatcher dispatcher = dispatchers.get(uri);
- if (dispatcher == null) {
- try {
- ServerWebSocket server = new ServerWebSocket(new URI(uri));
- System.out.println("Starting websocket server " + server + "
at " + uri + "...");
- dispatcher = new WebSocketOperationDispatcher(server);
- System.out.println("Created new dispatcher for " + uri + " " +
dispatcher);
- dispatchers.put(uri, dispatcher);
- server.register("/", dispatcher);
- new Thread(server).start();
- } catch (IOException e) {
- throw new RuntimeException(e);
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
+ private void initJavascriptResource() {
+ if (servletHost.getServletMapping(JAVASCRIPT_RESOURCE_PATH) == null) {
+ System.out.println("Adding javascript servlet");
+ servletHost.addServletMapping(JAVASCRIPT_RESOURCE_PATH, new
JavascriptResourceServlet());
}
- return dispatcher;
}
public void stop() {
- for (WebSocketOperationDispatcher dispatcher : dispatchers.values()) {
- dispatcher.shutdown();
+ for (WebSocketServer server : servers.values()) {
+ try {
+ server.stop();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
}
- dispatchers.clear();
+ servers.clear();
+ servletHost.removeServletMapping(JAVASCRIPT_RESOURCE_PATH);
+ JavascriptGenerator.clear();
}
public InterfaceContract getBindingInterfaceContract() {
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+package org.apache.tuscany.sca.binding.websocket.runtime;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
+
+public class WebsocketServiceInvoker {
+
+ protected Operation operation;
+ protected RuntimeEndpoint endpoint;
+
+ public WebsocketServiceInvoker(Operation operation, RuntimeEndpoint
endpoint) {
+ this.operation = operation;
+ this.endpoint = endpoint;
+ }
+
+ public WebSocketBindingMessage invokeSync(WebSocketBindingMessage request)
{
+ String jsonParams = request.getPayload();
+ Object[] args = JSONUtil.decodePayloadForOperation(jsonParams,
operation);
+ try {
+ Object operationResponse = endpoint.invoke(operation, args);
+ String payload = JSONUtil.encodePayload(operationResponse);
+ WebSocketBindingMessage response = new
WebSocketBindingMessage(request.getOperation(), payload);
+ return response;
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void invokeAsync(WebSocketBindingMessage request, TuscanyWebSocket
channel) {
+ // TODO add multiple response support
+ }
+
+}
Modified:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/binding-websocket.xsd
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/binding-websocket.xsd?rev=1150357&r1=1150356&r2=1150357&view=diff
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/binding-websocket.xsd
(original)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/binding-websocket.xsd
Sun Jul 24 13:32:46 2011
@@ -30,7 +30,7 @@
<complexType name="WebsocketBinding">
<complexContent>
<extension base="sca:Binding">
- <attribute name="someAttr" type="string" use="optional"/>
+ <attribute name="port" type="string" use="required"/>
</extension>
</complexContent>
</complexType>
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+var channels = [];
+
+function buildRequest(operation, message) {
+ return $.toJSON({
+ operation: operation,
+ payload: $.toJSON(message)
+ });
+}
+
+function sendMessage(port, operation, message) {
+ if (!channels[port]) {
+ ws = new WebSocket("ws://" + window.location.hostname + ":" + port);
+ channels[port] = ws;
+ ws.onopen = function() {
+ $(document).trigger('' + port);
+ }
+ ws.onmessage = function (message) {
+ var response = $.evalJSON(message.data);
+ eval('Tuscany.WebsocketComponentContext.' +
response.operation + '.responseHandler(' + response.payload + ')');
+ }
+ }
+
+ var jsonReq = buildRequest(operation, message);
+
+ if (ws.readyState == WebSocket.CONNECTING) {
+ $(document).bind('' + port, jsonReq, function(event) {
+ ws.send(event.data);
+ });
+ } else if (ws.readyState == WebSocket.OPEN) {
+ ws.send(jsonReq);
+ }
+}
\ No newline at end of file
Added:
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js
URL:
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js?rev=1150357&view=auto
==============================================================================
---
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js
(added)
+++
tuscany/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js
Sun Jul 24 13:32:46 2011
@@ -0,0 +1,45 @@
+/*
+ * jQuery JSON Plugin
+ * version: 2.2 (2009-08-14)
+ *
+ * This document is licensed as free software under the terms of the
+ * MIT License: http://www.opensource.org/licenses/mit-license.php
+ *
+ * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
+ * website's http://www.json.org/json2.js, which proclaims:
+ * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
+ * I uphold.
+ *
+ * It is also influenced heavily by MochiKit's serializeJSON, which is
+ * copyrighted 2005 by Bob Ippolito.
+ */
+(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