summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/binding-jsonp-runtime/src
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-06-16 21:23:22 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-06-16 21:23:22 +0000
commit4e3fa9e9e911427695cc432bc4ab5cb67da3abf3 (patch)
tree1ad0f764c592699c2e6a8f82bd3ce9171492243c /java/sca/modules/binding-jsonp-runtime/src
parent0eda2e8edc498098b998349a8c7b90e77224ea53 (diff)
Intial code for a jsonp binding runtime. Very basic butit does wokfor a simple helloworld type jsonp service
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@785412 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-jsonp-runtime/src')
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java63
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java74
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java114
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory19
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java28
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java28
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java53
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/test/Foo.java51
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite31
9 files changed, 461 insertions, 0 deletions
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java
new file mode 100644
index 0000000000..7c7a827571
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java
@@ -0,0 +1,63 @@
+/*
+ * 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.jsonp.runtime;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.binding.jsonp.JSONPBinding;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint;
+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.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+
+public class JSONPBindingProviderFactory implements BindingProviderFactory<JSONPBinding> {
+
+ private ServletHost servletHost;
+
+ public JSONPBindingProviderFactory(ExtensionPointRegistry extensionPoints) {
+ ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class);
+ List<ServletHost> hosts = servletHosts.getServletHosts();
+ if (!hosts.isEmpty()) {
+ this.servletHost = hosts.get(0);
+ }
+ }
+
+ public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ JSONPBinding binding) {
+ return null;
+ }
+
+ public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ JSONPBinding binding) {
+ return new JSONPServiceBindingProvider(component, service, binding, servletHost);
+ }
+
+ public Class<JSONPBinding> getModelType() {
+ return JSONPBinding.class;
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java
new file mode 100644
index 0000000000..25f1d8cc51
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java
@@ -0,0 +1,74 @@
+/*
+ * 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.jsonp.runtime;
+
+import org.apache.tuscany.sca.binding.jsonp.JSONPBinding;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.interfacedef.Interface;
+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.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+
+public class JSONPServiceBindingProvider implements ServiceBindingProvider {
+
+ private RuntimeComponentService service;
+ private JSONPBinding binding;
+ private ServletHost servletHost;
+
+ public JSONPServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ JSONPBinding binding,
+ ServletHost servletHost) {
+ this.service = service;
+ this.binding = binding;
+ this.servletHost = servletHost;
+ }
+
+ public void start() {
+ RuntimeWire wire = service.getRuntimeWire(binding);
+ Interface serviceInterface = service.getInterfaceContract().getInterface();
+ for (Operation op : serviceInterface.getOperations()) {
+ JSONPServlet servlet = new JSONPServlet(wire, op);
+ String path = service.getName() + "/" + op.getName();
+ servletHost.addServletMapping(path, servlet);
+ }
+ }
+
+ public void stop() {
+ Interface serviceInterface = service.getInterfaceContract().getInterface();
+ for (Operation op : serviceInterface.getOperations()) {
+ String path = service.getName() + "/" + op.getName();
+ servletHost.removeServletMapping(path);
+ }
+ }
+
+ // TODO: Why are these two still on the ServiceBindingProvider interface?
+ public InterfaceContract getBindingInterfaceContract() {
+ return null;
+ }
+
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
new file mode 100644
index 0000000000..6521e336e6
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
@@ -0,0 +1,114 @@
+/*
+ * 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.jsonp.runtime;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Enumeration;
+
+import javax.servlet.GenericServlet;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+
+public class JSONPServlet extends GenericServlet {
+ private static final long serialVersionUID = 1L;
+
+ protected transient RuntimeWire wire;
+ protected transient Operation operation;
+ protected transient ObjectMapper mapper;
+
+ public JSONPServlet(RuntimeWire wire, Operation operation) {
+ this.wire = wire;
+ this.operation = operation;
+ this.mapper = new ObjectMapper();
+ }
+
+ @Override
+ public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
+ String jsonRequest = getJSONRequest(servletRequest);
+ Object[] args = jsonToObjects(jsonRequest);
+ Object response = invokeService(args);
+ String jsonResponse = getJSONResponse(servletRequest, response);
+ servletResponse.getOutputStream().println(jsonResponse);
+ }
+
+ /**
+ * Turn the request into JSON
+ */
+ protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException {
+ String jsonRequest = "[";
+ for (Enumeration<?> ns = servletRequest.getParameterNames(); ns.hasMoreElements(); ) {
+ String name = (String) ns.nextElement();
+ if (!name.startsWith("_") && !"callback".equals(name)) {
+ if (jsonRequest.length() > 1) {
+ jsonRequest += ", ";
+ }
+ //jsonRequest += name + ":" + servletRequest.getParameter(name);
+ jsonRequest += "\""+ servletRequest.getParameter(name) + "\"";
+ }
+ }
+ jsonRequest += "]";
+ return jsonRequest;
+ }
+
+ /**
+ * Turn the response object into JSON
+ */
+ protected String getJSONResponse(ServletRequest servletRequest, Object response) throws IOException, JsonParseException {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ mapper.writeValue(os , response);
+ String jsonResponse = os.toString();
+
+ String callback = servletRequest.getParameter("callback");
+ if (callback != null && callback.length() > 1) {
+ jsonResponse = callback + "(" + jsonResponse + ");";
+ }
+
+ return jsonResponse;
+ }
+
+ /**
+ * Turn the request JSON into objects
+ */
+ protected Object[] jsonToObjects(String jsonRequest) throws IOException, JsonParseException, JsonMappingException {
+ Class<?> c = new Object[0].getClass();
+ Object[] args = (Object[])mapper.readValue(jsonRequest, c);
+ return args;
+ }
+
+ /**
+ * Send the request down the wire to invoke the service
+ */
+ protected Object invokeService(Object[] args) {
+ try {
+ return wire.invoke(operation, args);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+} \ No newline at end of file
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory b/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory
new file mode 100644
index 0000000000..3efe5535ce
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory
@@ -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.
+
+# Implementation class for the binding extension
+org.apache.tuscany.sca.binding.jsonp.runtime.JSONPBindingProviderFactory;model=org.apache.tuscany.sca.binding.jsonp.JSONPBinding
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java
new file mode 100644
index 0000000000..16bf1db7ab
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.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 helloworld;
+
+
+public class HelloWorldImpl implements HelloWorldService {
+
+ public String sayHello(String name) {
+ return "Hello " + name;
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java
new file mode 100644
index 0000000000..064d615c45
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.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 helloworld;
+
+import org.oasisopen.sca.annotation.Remotable;
+
+@Remotable
+public interface HelloWorldService {
+
+ String sayHello(String name);
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java
new file mode 100644
index 0000000000..2b5676a4c9
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.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 test;
+
+import helloworld.HelloWorldService;
+import junit.framework.Assert;
+
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class BindingTestCase {
+
+ private static Node node;
+ private static HelloWorldService helloWorldService;
+
+ @Test
+ public void testService() {
+// Assert.assertEquals("Hello Petra", helloWorldService.sayHello("Petra"));
+ }
+
+ @BeforeClass
+ public static void init() throws Exception {
+ node = NodeFactory.newInstance().createNode("helloworld.composite").start();
+ helloWorldService = node.getService(HelloWorldService.class, "HelloWorldComponent");
+ }
+
+ @AfterClass
+ public static void destroy() throws Exception {
+ if (node != null) {
+ node.stop();
+ }
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/test/Foo.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/Foo.java
new file mode 100644
index 0000000000..b3bd70ff09
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/Foo.java
@@ -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.
+ */
+
+package test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+
+public class Foo {
+
+ public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
+
+ String jsonRequest = "{}";
+
+ ObjectMapper mapper = new ObjectMapper();
+
+ Object[] x = new Object[] { "ant" };
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ mapper.writeValue(os , x);
+ jsonRequest = os.toString();
+
+ System.out.println(jsonRequest);
+
+ Class c = new Object[0].getClass();
+ Object oo = mapper.readValue(jsonRequest, c);
+
+ System.out.println(oo);
+
+ }
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite b/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite
new file mode 100644
index 0000000000..8213d0e06f
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ -->
+<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
+ xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
+ name="HelloWorldComposite">
+
+ <component name="HelloWorldComponent">
+ <implementation.java class="helloworld.HelloWorldImpl"/>
+ <service name="HelloWorldService" >
+ <tuscany:binding.jsonp />
+ </service>
+ </component>
+
+</composite>