From 24eb985532c90f1fa09a6c4672ced19e63627fc9 Mon Sep 17 00:00:00 2001 From: fmoga Date: Tue, 26 Jul 2011 21:10:39 +0000 Subject: Move the websocket binding to trunk. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1151250 13f79535-47bb-0310-9956-ffa450edef68 --- .../trunk/modules/binding-websocket/LICENSE | 205 +++++++++++++++++++++ .../modules/binding-websocket/META-INF/MANIFEST.MF | 17 ++ .../trunk/modules/binding-websocket/NOTICE | 6 + .../trunk/modules/binding-websocket/pom.xml | 74 ++++++++ .../src/main/java/META-INF/MANIFEST.MF | 3 + .../sca/binding/websocket/WebsocketBinding.java | 49 +++++ .../binding/websocket/WebsocketBindingFactory.java | 31 ++++ .../sca/binding/websocket/runtime/JSONUtil.java | 106 +++++++++++ .../websocket/runtime/JavascriptGenerator.java | 113 ++++++++++++ .../runtime/JavascriptResourceServlet.java | 105 +++++++++++ .../websocket/runtime/TuscanyWebsocket.java | 85 +++++++++ .../runtime/WebsocketBindingCallback.java | 31 ++++ .../runtime/WebsocketBindingDispatcher.java | 42 +++++ .../websocket/runtime/WebsocketBindingMessage.java | 50 +++++ .../runtime/WebsocketBindingProviderFactory.java | 55 ++++++ .../runtime/WebsocketCallbackInvoker.java | 59 ++++++ .../runtime/WebsocketConnectionManager.java | 53 ++++++ .../runtime/WebsocketReferenceBindingProvider.java | 60 ++++++ .../binding/websocket/runtime/WebsocketServer.java | 59 ++++++ .../runtime/WebsocketServiceBindingProvider.java | 109 +++++++++++ .../websocket/runtime/WebsocketServiceInvoker.java | 97 ++++++++++ .../binding/websocket/runtime/WebsocketStatus.java | 9 + ...y.sca.binding.websocket.WebsocketBindingFactory | 20 ++ ...ca.contribution.processor.StAXArtifactProcessor | 20 ++ ...any.sca.contribution.processor.ValidationSchema | 19 ++ ...che.tuscany.sca.provider.BindingProviderFactory | 20 ++ .../src/main/resources/binding-websocket.xsd | 37 ++++ .../main/resources/js/TuscanyWebsocketToolkit.js | 51 +++++ .../src/test/java/async/NotificationService.java | 34 ++++ .../test/java/async/NotificationServiceImpl.java | 50 +++++ .../src/test/java/sync/HelloService.java | 28 +++ .../src/test/java/sync/HelloServiceImpl.java | 30 +++ .../java/testcase/WebsocketBindingTestCase.java | 59 ++++++ .../src/test/resources/async.composite | 37 ++++ .../src/test/resources/sync.composite | 32 ++++ 35 files changed, 1855 insertions(+) create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/LICENSE create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/META-INF/MANIFEST.MF create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/NOTICE create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/pom.xml create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBindingFactory.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebsocket.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingCallback.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingDispatcher.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingMessage.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketCallbackInvoker.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketConnectionManager.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServer.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketStatus.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ValidationSchema create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/binding-websocket.xsd create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationService.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationServiceImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloService.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloServiceImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/java/testcase/WebsocketBindingTestCase.java create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/async.composite create mode 100644 sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/sync.composite (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/binding-websocket/LICENSE b/sca-java-2.x/trunk/modules/binding-websocket/LICENSE new file mode 100644 index 0000000000..6e529a25c4 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/LICENSE @@ -0,0 +1,205 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. + + + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-websocket/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..2f29092dbe --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/META-INF/MANIFEST.MF @@ -0,0 +1,17 @@ +Manifest-Version: 1.0 +SCA-Version: 1.1 +Bundle-Name: Apache Tuscany SCA Websocket Binding +Bundle-Vendor: The Apache Software Foundation +Bundle-Version: 2.0.0 +Bundle-ManifestVersion: 2 +Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt +Bundle-Description: Apache Tuscany SCA Websocket Binding +Import-Package: org.apache.tuscany.sca.assembly;version="2.0.0", + org.apache.tuscany.sca.core;version="2.0.0", + org.apache.tuscany.sca.interfacedef;version="2.0.0", + org.apache.tuscany.sca.invocation;version="2.0.0", + org.apache.tuscany.sca.provider;version="2.0.0", + org.apache.tuscany.sca.runtime;version="2.0.0" +Bundle-SymbolicName: org.apache.tuscany.sca.binding.binding-websocket +Bundle-DocURL: http://www.apache.org/ +Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/sca-java-2.x/trunk/modules/binding-websocket/NOTICE b/sca-java-2.x/trunk/modules/binding-websocket/NOTICE new file mode 100644 index 0000000000..f6ce2542c3 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/NOTICE @@ -0,0 +1,6 @@ +${pom.name} +Copyright (c) 2005 - 2011 The Apache Software Foundation + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/pom.xml b/sca-java-2.x/trunk/modules/binding-websocket/pom.xml new file mode 100644 index 0000000000..d473ba8503 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/pom.xml @@ -0,0 +1,74 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 2.0-SNAPSHOT + ../pom.xml + + + org.apache.tuscany.sca + tuscany-binding-websocket + 2.0-SNAPSHOT + Apache Tuscany SCA Websocket Binding Extension + + + + + org.apache.tuscany.sca + tuscany-base-runtime-pom + pom + 2.0-SNAPSHOT + provided + + + + org.eclipse.jetty + jetty-websocket + 8.0.0.M3 + compile + + + + com.google.code.gson + gson + 1.7.1 + compile + + + + junit + junit + 4.8.1 + test + + + + org.mortbay.jetty + jetty + 6.1.26 + test + + + + + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..5e9495128c --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java new file mode 100644 index 0000000000..a78a6b11c2 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBinding.java @@ -0,0 +1,49 @@ +/* + * 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; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.provider.BaseBindingImpl; + +/** + * Represents a binding to a Websocket service. + */ +public class WebsocketBinding extends BaseBindingImpl { + + public static final QName TYPE = new QName(SCA11_TUSCANY_NS, "binding.websocket"); + private String port; + + public WebsocketBinding() { + } + + @Override + public QName getType() { + return TYPE; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBindingFactory.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBindingFactory.java new file mode 100644 index 0000000000..1357bb5a7c --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/WebsocketBindingFactory.java @@ -0,0 +1,31 @@ +/* + * 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; + +/** + * Factory implementation to create Websocket models. + */ +public class WebsocketBindingFactory { + + public WebsocketBinding createWebsocketBinding() { + return new WebsocketBinding(); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java new file mode 100644 index 0000000000..80cbf6a6d9 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JSONUtil.java @@ -0,0 +1,106 @@ +/* + * 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.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; + +import com.google.gson.Gson; + +/** + * Utility class to handle JSON convertions. + */ +public class JSONUtil { + + private static Gson gson = new Gson(); + + public static String encodeMessage(WebsocketBindingMessage message) { + return gson.toJson(message); + } + + public static WebsocketBindingMessage decodeMessage(String jsonMessage) { + return gson.fromJson(jsonMessage, WebsocketBindingMessage.class); + } + + public static String encodePayload(Object payload) { + return gson.toJson(payload); + } + + /** + * Convert opeartion parameters from JSON to the appropriate parameter + * types. + */ + public static Object[] decodePayloadForOperation(String jsonData, Operation operation) { + Object[] args = new Object[operation.getInputType().getLogical().size()]; + final String[] json = parseArray(jsonData); + int index = 0; + for (final DataType dataType : operation.getInputType().getLogical()) { + args[index] = gson.fromJson(json[index], dataType.getPhysical()); + index++; + } + return args; + } + + /** + * Split the JSON array containing the arguments for the opeartion in order + * to avoid converting JSON to Object[]. Converting each object separately + * to it's corresponding data type avoids type mismatch problems at service + * invocation. + */ + private static String[] parseArray(String jsonArray) { + List objects = new ArrayList(); + int bracketNum = 0; + int parNum = 0; + int quoteNum = 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 '\"': + quoteNum++; + break; + case ',': + if ((bracketNum == 0) && (parNum == 1) && quoteNum % 2 == 0) { + objects.add(jsonArray.substring(startPos, i)); + startPos = i + 1; + } + } + } + objects.add(jsonArray.substring(startPos, jsonArray.length() - 1)); + return objects.toArray(new String[] {}); + } + + private JSONUtil() { + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java new file mode 100644 index 0000000000..21b953151b --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptGenerator.java @@ -0,0 +1,113 @@ +/* + * 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; + +/** + * Generates javascript service proxies using the websocket API. This enables + * simulating SCA on the client side javascript. This is a stateful singleton. + */ +public class JavascriptGenerator { + + private final static String CONTEXT = "this.WebsocketComponentContext"; + private static final String LF = "\n"; + private static StringBuilder builder = new StringBuilder(); + + /** + * Generate javascript code for one service and all it's operations. Add + * this to the state of the generator. + */ + public static void generateServiceProxy(String component, String service, List 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; + } + + /** + * Get the state of the generator. + */ + public static String getServiceProxies() { + return builder.toString(); + } + + /** + * Reset the state of the generator. + */ + public static void clear() { + builder.setLength(0); + } + + private JavascriptGenerator() { + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java new file mode 100644 index 0000000000..50d68a19bc --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/JavascriptResourceServlet.java @@ -0,0 +1,105 @@ +/* + * 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; + +/** + * This servlet serves the generated javascript service proxies. + */ +public class JavascriptResourceServlet extends HttpServlet { + + private static final String WEBSOCKET_TOOLKIT_PATH = "js/TuscanyWebsocketToolkit.js"; + + private String websocketToolkit; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { + try { + resp.setContentType("text/javascript"); + OutputStream os = resp.getOutputStream(); + 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 getWebsocketToolkit() { + if (websocketToolkit == null) { + websocketToolkit = getResource(WEBSOCKET_TOOLKIT_PATH); + } + return websocketToolkit; + } + + /** + * Read a resource from the disk by relative path. + */ + 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(); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebsocket.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebsocket.java new file mode 100644 index 0000000000..b71ca27543 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/TuscanyWebsocket.java @@ -0,0 +1,85 @@ +/* + * 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 java.util.UUID; + +import org.eclipse.jetty.websocket.WebSocket; + +/** + * A websocket connection handling communication between one browser client and + * *all* websocket services hosted by a server. The connection is persisted as + * long as the client is connected and communication with all services is + * multiplexed via a single websocket connection. + */ +public class TuscanyWebsocket implements WebSocket, WebSocket.OnTextMessage { + + private String id; + private Connection connection; + private WebsocketBindingDispatcher dispatcher; + + public TuscanyWebsocket(WebsocketBindingDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + @Override + public void onOpen(Connection connection) { + this.connection = connection; + this.id = UUID.randomUUID().toString(); + WebsocketConnectionManager.addConnection(this); + } + + @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 { + if (!invoker.isNonBlocking()) { + WebsocketBindingMessage response = invoker.invokeSync(request); + send(response); + } else { + invoker.invokeAsync(request, this); + } + } + } + + @Override + public void onClose(int closeCode, String message) { + WebsocketConnectionManager.removeConnection(this); + } + + public void send(WebsocketBindingMessage message) { + try { + if (connection.isOpen()) { + connection.sendMessage(JSONUtil.encodeMessage(message)); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public String getId() { + return id; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingCallback.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingCallback.java new file mode 100644 index 0000000000..3ece55aa00 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingCallback.java @@ -0,0 +1,31 @@ +/* + * 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 org.oasisopen.sca.annotation.Remotable; + +/** + * Interface describing the websocket binding callback to a browser client. + */ +@Remotable +public interface WebsocketBindingCallback { + + public WebsocketStatus sendMessage(Object message); + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingDispatcher.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingDispatcher.java new file mode 100644 index 0000000000..eda78de98a --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingDispatcher.java @@ -0,0 +1,42 @@ +/* + * 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; + +/** + * A dispatcher stores all service invokers for a servers and is used to + * determine which one of them should be used when a request comes in. + */ +public class WebsocketBindingDispatcher { + + private Map invokers = new HashMap(); + + public void addOperation(String uri, RuntimeEndpoint endpoint, Operation operation) { + invokers.put(uri, new WebsocketServiceInvoker(operation, endpoint)); + } + + public WebsocketServiceInvoker dispatch(String uri) { + return invokers.get(uri); + } +} \ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingMessage.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingMessage.java new file mode 100644 index 0000000000..f7ea1d1620 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingMessage.java @@ -0,0 +1,50 @@ +/* + * 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; + +/** + * The websocket binding message structure. + */ +public class WebsocketBindingMessage { + + private String operation; + private String payload; + + public WebsocketBindingMessage(String operation, String payload) { + this.operation = operation; + this.payload = payload; + } + + public String getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = operation; + } + + public String getPayload() { + return payload; + } + + public void setPayload(String payload) { + this.payload = payload; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java new file mode 100644 index 0000000000..5c5f96bd76 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketBindingProviderFactory.java @@ -0,0 +1,55 @@ +/* + * 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 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; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +/** + * Factory for reference and service binding providers. + */ +public class WebsocketBindingProviderFactory implements BindingProviderFactory { + + private ServletHost servletHost; + + public WebsocketBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + this.servletHost = ServletHostHelper.getServletHost(extensionPoints); + } + + public Class getModelType() { + return WebsocketBinding.class; + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpoint) { + return new WebsocketReferenceBindingProvider(endpoint); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + return new WebsocketServiceBindingProvider(endpoint, servletHost); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketCallbackInvoker.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketCallbackInvoker.java new file mode 100644 index 0000000000..ebd7f13081 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketCallbackInvoker.java @@ -0,0 +1,59 @@ +/* + * 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 org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.core.invocation.Constants; +import org.apache.tuscany.sca.core.invocation.impl.MessageImpl; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +/** + * The callback invoker is used to send asynchronous responses back to the + * browser client. + */ +public class WebsocketCallbackInvoker implements Invoker { + + protected Operation operation; + protected EndpointReference endpoint; + + public WebsocketCallbackInvoker(Operation operation, EndpointReference endpoint) { + this.operation = operation; + this.endpoint = endpoint; + } + + public Message invoke(Message msg) { + String channelId = (String) msg.getHeaders().get(Constants.RELATES_TO); + TuscanyWebsocket websocket = WebsocketConnectionManager.getConnection(channelId); + Message response = new MessageImpl(); + if (websocket == null) { + response.setBody(WebsocketStatus.CLOSED); + } else { + Object[] body = msg.getBody(); + String payload = JSONUtil.encodePayload(body[0]); + String operation = msg.getTo().getURI(); + WebsocketBindingMessage message = new WebsocketBindingMessage(operation, payload); + websocket.send(message); + response.setBody(WebsocketStatus.OPEN); + } + return response; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketConnectionManager.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketConnectionManager.java new file mode 100644 index 0000000000..ba95d77b7f --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketConnectionManager.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.websocket.runtime; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * The connection manager stores all active websocket connections in order to be + * able to retrieve the appropriate communication channel when an asynchronous + * response is sent. + */ +public class WebsocketConnectionManager { + + private static ConcurrentMap activeConnections = new ConcurrentHashMap(); + + public static void addConnection(TuscanyWebsocket websocket) { + activeConnections.put(websocket.getId(), websocket); + } + + public static void removeConnection(TuscanyWebsocket websocket) { + activeConnections.remove(websocket.getId()); + } + + public static TuscanyWebsocket getConnection(String id) { + return activeConnections.get(id); + } + + public static void clear() { + activeConnections.clear(); + } + + private WebsocketConnectionManager() { + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java new file mode 100644 index 0000000000..dfd632d330 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketReferenceBindingProvider.java @@ -0,0 +1,60 @@ +/* + * 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 org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; + +/** + * The reference binding provider is used to initiate the necessary + * infrastructure on the reference side. As the binding only supports browser + * clients, the reference binding provider is used as a factory for callback + * invokers. + */ +public class WebsocketReferenceBindingProvider implements ReferenceBindingProvider { + + private EndpointReference endpoint; + + public WebsocketReferenceBindingProvider(EndpointReference endpoint) { + this.endpoint = endpoint; + } + + public Invoker createInvoker(Operation operation) { + return new WebsocketCallbackInvoker(operation, endpoint); + } + + public void start() { + } + + public void stop() { + } + + public InterfaceContract getBindingInterfaceContract() { + return endpoint.getComponentReferenceInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServer.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServer.java new file mode 100644 index 0000000000..cde86b0575 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServer.java @@ -0,0 +1,59 @@ +/* + * 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; + +/** + * The websocket server is an embedded Jetty instance which will be started on + * the port specified in the component definition. + */ +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) { + return new TuscanyWebsocket(dispatcher); + } + }); + + dispatcher = new WebsocketBindingDispatcher(); + } + + public WebsocketBindingDispatcher getDispatcher() { + return dispatcher; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java new file mode 100644 index 0000000000..2352970b2e --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceBindingProvider.java @@ -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. + */ + +package org.apache.tuscany.sca.binding.websocket.runtime; + +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; + +/** + * The service binding provider initiates the necessary infrastructure to expose + * services via websockets. + */ +public class WebsocketServiceBindingProvider implements ServiceBindingProvider { + + private static final int DEFAULT_PORT = 9000; + private static final String JAVASCRIPT_RESOURCE_PATH = "/org.apache.tuscany.sca.WebsocketComponentContext.js"; + private static Map servers = new HashMap(); + private RuntimeEndpoint endpoint; + private ServletHost servletHost; + + public WebsocketServiceBindingProvider(RuntimeEndpoint endpoint, ServletHost servletHost) { + this.endpoint = endpoint; + this.servletHost = servletHost; + } + + public void start() { + WebsocketBinding binding = (WebsocketBinding) endpoint.getBinding(); + int port = DEFAULT_PORT; + if (binding.getPort() != null) { + 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 void initJavascriptResource() { + if (servletHost.getServletMapping(JAVASCRIPT_RESOURCE_PATH) == null) { + servletHost.addServletMapping(JAVASCRIPT_RESOURCE_PATH, new JavascriptResourceServlet()); + } + } + + public void stop() { + for (WebsocketServer server : servers.values()) { + try { + server.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + servers.clear(); + servletHost.removeServletMapping(JAVASCRIPT_RESOURCE_PATH); + JavascriptGenerator.clear(); + WebsocketConnectionManager.clear(); + } + + public InterfaceContract getBindingInterfaceContract() { + return endpoint.getService().getInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java new file mode 100644 index 0000000000..6c7a25c59e --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketServiceInvoker.java @@ -0,0 +1,97 @@ +/* + * 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.assembly.EndpointReference; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointReferenceImpl; +import org.apache.tuscany.sca.core.invocation.Constants; +import org.apache.tuscany.sca.core.invocation.impl.MessageImpl; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * The service invoker is used to call an operation implementation in a + * synchronous or asynchronous way. + */ +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) { + String jsonParams = request.getPayload(); + Object[] args = JSONUtil.decodePayloadForOperation(jsonParams, operation); + Message msg = new MessageImpl(); + msg.getHeaders().put(Constants.MESSAGE_ID, channel.getId()); + msg.setBody(args); + EndpointReference re = new RuntimeEndpointReferenceImpl(); + RuntimeEndpointImpl callbackEndpoint = new RuntimeEndpointImpl(); + callbackEndpoint.setURI(request.getOperation()); + re.setCallbackEndpoint(callbackEndpoint); + msg.setFrom(re); + endpoint.invoke(operation, msg); + } + + public boolean isNonBlocking() { + return operation.isNonBlocking(); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketStatus.java b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketStatus.java new file mode 100644 index 0000000000..bb89761459 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/java/org/apache/tuscany/sca/binding/websocket/runtime/WebsocketStatus.java @@ -0,0 +1,9 @@ +package org.apache.tuscany.sca.binding.websocket.runtime; + +/** + * The websocket status indicates the state of a websocket connection when a + * send operation is performed. + */ +public enum WebsocketStatus { + OPEN, CLOSED +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory new file mode 100644 index 0000000000..4ec8ba4eb2 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory @@ -0,0 +1,20 @@ +# 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. + +# Implementation class for model factory +org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..fd2e21a468 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,20 @@ +# 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. + +# Implementation class for the artifact processor extension +org.apache.tuscany.sca.assembly.xml.DefaultBeanModelProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#binding.websocket,model=org.apache.tuscany.sca.binding.websocket.WebsocketBinding,factory=org.apache.tuscany.sca.binding.websocket.WebsocketBindingFactory + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ValidationSchema b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ValidationSchema new file mode 100644 index 0000000000..d4a7646f48 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ValidationSchema @@ -0,0 +1,19 @@ +# 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. +# +binding-websocket.xsd + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory new file mode 100644 index 0000000000..e97707636d --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory @@ -0,0 +1,20 @@ +# 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. + +# Implementation class for the binding extension +org.apache.tuscany.sca.binding.websocket.runtime.WebsocketBindingProviderFactory;model=org.apache.tuscany.sca.binding.websocket.WebsocketBinding + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/binding-websocket.xsd b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/binding-websocket.xsd new file mode 100644 index 0000000000..8ed0011361 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/binding-websocket.xsd @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js new file mode 100644 index 0000000000..ede28579d3 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js @@ -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 JSON.stringify({ + operation: operation, + payload: JSON.stringify(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 = eval('(' + 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 diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationService.java b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationService.java new file mode 100644 index 0000000000..f6cb155023 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationService.java @@ -0,0 +1,34 @@ +/* + * 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 async; + +import org.apache.tuscany.sca.binding.websocket.runtime.WebsocketBindingCallback; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.OneWay; +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +@Callback(WebsocketBindingCallback.class) +public interface NotificationService { + + @OneWay + void registerForNotifications(); + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationServiceImpl.java b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationServiceImpl.java new file mode 100644 index 0000000000..3350e7bd5f --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/async/NotificationServiceImpl.java @@ -0,0 +1,50 @@ +/* + * 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 async; + +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.tuscany.sca.binding.websocket.runtime.WebsocketBindingCallback; +import org.apache.tuscany.sca.binding.websocket.runtime.WebsocketStatus; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Service; + +@Service(NotificationService.class) +public class NotificationServiceImpl implements NotificationService { + + @Callback + protected WebsocketBindingCallback client; + + @Override + public void registerForNotifications() { + new Timer().scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + WebsocketStatus status = client.sendMessage("Notification @ " + new Date()); + if (status == WebsocketStatus.CLOSED) { + this.cancel(); + } + } + }, 0L, 1000L); + } +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloService.java b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloService.java new file mode 100644 index 0000000000..3e205c8148 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloService.java @@ -0,0 +1,28 @@ +/* + * 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 sync; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface HelloService { + + String sayHello(String name); + +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloServiceImpl.java b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloServiceImpl.java new file mode 100644 index 0000000000..ee55d1db78 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/sync/HelloServiceImpl.java @@ -0,0 +1,30 @@ +/* + * 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 sync; + +import org.oasisopen.sca.annotation.Service; + +@Service(HelloService.class) +public class HelloServiceImpl implements HelloService { + + @Override + public String sayHello(String name) { + return "Hello " + name; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/testcase/WebsocketBindingTestCase.java b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/testcase/WebsocketBindingTestCase.java new file mode 100644 index 0000000000..318e2fdd01 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/java/testcase/WebsocketBindingTestCase.java @@ -0,0 +1,59 @@ +/* + * 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 testcase; + +import junit.framework.Assert; + +import org.apache.tuscany.sca.http.jetty.JettyServer; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.BeforeClass; +import org.junit.Test; + +public class WebsocketBindingTestCase { + + @BeforeClass + public static void init() { + JettyServer.portDefault = 8085; + } + + @Test + public void testSync() { + try { + Node node = NodeFactory.newInstance().createNode("sync.composite"); + node.start(); + node.stop(); + } catch (final Exception e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void testAsync() { + try { + Node node = NodeFactory.newInstance().createNode("async.composite"); + node.start(); + node.stop(); + } catch (final Exception e) { + Assert.fail(e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/async.composite b/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/async.composite new file mode 100644 index 0000000000..277cf17df2 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/async.composite @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/sync.composite b/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/sync.composite new file mode 100644 index 0000000000..a4662cb2ee --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-websocket/src/test/resources/sync.composite @@ -0,0 +1,32 @@ + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3