diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org')
6 files changed, 332 insertions, 0 deletions
diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/assembly/JSONRPCBinding.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/assembly/JSONRPCBinding.java new file mode 100644 index 0000000000..3629bbe98c --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/assembly/JSONRPCBinding.java @@ -0,0 +1,39 @@ +/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.
+ */
+package org.apache.tuscany.binding.jsonrpc.assembly;
+
+import org.apache.tuscany.model.assembly.impl.BindingImpl;
+
+/**
+ * An Binding implementation for JSON-RPC.
+ */
+public class JSONRPCBinding extends BindingImpl {
+
+ private String webAppName;
+
+ public JSONRPCBinding() {
+ }
+
+ public void setWebAppName(String webAppName) {
+ this.webAppName = webAppName;
+ }
+
+ public String getWebAppName() {
+ return webAppName;
+ }
+
+}
diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/builder/JSONRPCEntryPointBuilder.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/builder/JSONRPCEntryPointBuilder.java new file mode 100644 index 0000000000..b0725c464b --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/builder/JSONRPCEntryPointBuilder.java @@ -0,0 +1,41 @@ +/**
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.
+ */
+package org.apache.tuscany.binding.jsonrpc.builder;
+
+import org.apache.tuscany.binding.jsonrpc.assembly.JSONRPCBinding;
+import org.apache.tuscany.binding.jsonrpc.config.JSONEntryPointContextFactory;
+import org.apache.tuscany.core.extension.EntryPointBuilderSupport;
+import org.apache.tuscany.core.extension.EntryPointContextFactory;
+import org.apache.tuscany.core.message.MessageFactory;
+import org.apache.tuscany.core.system.annotation.Autowire;
+import org.apache.tuscany.core.webapp.ServletHost;
+import org.apache.tuscany.model.assembly.EntryPoint;
+import org.osoa.sca.annotations.Scope;
+
+@Scope("MODULE")
+public class JSONRPCEntryPointBuilder extends EntryPointBuilderSupport<JSONRPCBinding> {
+
+ private ServletHost tomcatHost;
+
+ @Autowire
+ public void setTomcatHost(ServletHost tomcatHost) {
+ this.tomcatHost = tomcatHost;
+ }
+
+ @Override
+ protected EntryPointContextFactory createEntryPointContextFactory(EntryPoint entryPoint, MessageFactory msgFactory) {
+ String webAppName = ((JSONRPCBinding) entryPoint.getBindings().get(0)).getWebAppName();
+ return new JSONEntryPointContextFactory(entryPoint.getName(), msgFactory, webAppName, tomcatHost);
+ }
+
+}
diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/config/JSONEntryPointContextFactory.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/config/JSONEntryPointContextFactory.java new file mode 100644 index 0000000000..7c36f83e6b --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/config/JSONEntryPointContextFactory.java @@ -0,0 +1,60 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * 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. + */ +package org.apache.tuscany.binding.jsonrpc.config; + +import org.apache.tuscany.binding.jsonrpc.handler.JSONRPCEntryPointServlet; +import org.apache.tuscany.binding.jsonrpc.handler.ScriptGetterServlet; +import org.apache.tuscany.core.builder.ContextCreationException; +import org.apache.tuscany.core.context.EntryPointContext; +import org.apache.tuscany.core.extension.EntryPointContextFactory; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.webapp.ServletHost; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class JSONEntryPointContextFactory extends EntryPointContextFactory { + + private ServletHost tomcatHost; + + private String webAppName; + + public JSONEntryPointContextFactory(String name, MessageFactory msgFactory, String webAppName, ServletHost tomcatHost) { + super(name, msgFactory); + this.webAppName = webAppName; + this.tomcatHost = tomcatHost; + } + + public EntryPointContext createContext() throws ContextCreationException { + EntryPointContext epc = super.createContext(); + JSONRPCEntryPointServlet jsonrpcServlet = getServlet(); + jsonrpcServlet.addEntryPoint(epc); + return epc; + } + + private JSONRPCEntryPointServlet getServlet() { + String jsonrpcServletMapping = webAppName + "/SCA/jsonrpc"; + JSONRPCEntryPointServlet servlet; + synchronized (tomcatHost) { + servlet = (JSONRPCEntryPointServlet) tomcatHost.getMapping(jsonrpcServletMapping); + if (servlet == null) { + servlet = new JSONRPCEntryPointServlet(); + tomcatHost.registerMapping(jsonrpcServletMapping, servlet); + tomcatHost.registerMapping(webAppName + "/SCA/scripts/*", new ScriptGetterServlet()); + } + } + return servlet; + } + +} diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/JSONRPCEntryPointServlet.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/JSONRPCEntryPointServlet.java new file mode 100644 index 0000000000..f59867b725 --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/JSONRPCEntryPointServlet.java @@ -0,0 +1,76 @@ +/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.
+ */
+
+package org.apache.tuscany.binding.jsonrpc.handler;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.tuscany.core.context.EntryPointContext;
+
+import com.metaparadigm.jsonrpc.JSONRPCBridge;
+import com.metaparadigm.jsonrpc.JSONRPCServlet;
+
+/**
+ *
+ *
+ */
+public class JSONRPCEntryPointServlet extends JSONRPCServlet {
+ private static final long serialVersionUID = 1L;
+
+ private transient List<EntryPointContext> entryPoints;
+
+ public JSONRPCEntryPointServlet() {
+ entryPoints = new ArrayList<EntryPointContext>();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.metaparadigm.jsonrpc.JSONRPCServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ @Override
+ public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ClassCastException {
+
+ /*
+ * Create a new bridge for every request to aviod all the problems with JSON-RPC-Java storing the bridge in the session
+ */
+ HttpSession session = request.getSession();
+ try {
+
+ JSONRPCBridge jsonrpcBridge = new JSONRPCBridge();
+ for (EntryPointContext epc : entryPoints) {
+ jsonrpcBridge.registerObject(epc.getName(), epc.getInstance(null));
+ }
+ session.setAttribute("JSONRPCBridge", jsonrpcBridge);
+
+ super.service(request, response);
+
+ } finally {
+ session.removeAttribute("JSONRPCBridge");
+ }
+ }
+
+ public void addEntryPoint(EntryPointContext epc) {
+ entryPoints.add(epc);
+ }
+}
diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/ScriptGetterServlet.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/ScriptGetterServlet.java new file mode 100644 index 0000000000..9177462de0 --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/handler/ScriptGetterServlet.java @@ -0,0 +1,51 @@ +/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.
+ */
+package org.apache.tuscany.binding.jsonrpc.handler;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class ScriptGetterServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final String SCA_INIT_JS = "SCA = new JSONRpcClient(\"SCA/jsonrpc\");";
+
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ URL url = getClass().getResource("jsonrpc.js");
+ InputStream is = url.openStream();
+ writeToStream(response, is);
+
+ writeToStream(response, new ByteArrayInputStream(SCA_INIT_JS.getBytes()));
+ }
+
+ private void writeToStream(HttpServletResponse response, InputStream is) throws IOException {
+ ServletOutputStream os = response.getOutputStream();
+ int i;
+ while ((i = is.read()) != -1) {
+ os.write(i);
+ }
+ }
+
+}
diff --git a/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/loader/JSONRPCBindingLoader.java b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/loader/JSONRPCBindingLoader.java new file mode 100644 index 0000000000..64cefc9ea3 --- /dev/null +++ b/branches/java-post-M1/sca/bindings/binding.jsonrpc/src/main/java/org/apache/tuscany/binding/jsonrpc/loader/JSONRPCBindingLoader.java @@ -0,0 +1,65 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * 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. + */ +package org.apache.tuscany.binding.jsonrpc.loader; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.binding.jsonrpc.assembly.JSONRPCBinding; +import org.apache.tuscany.core.config.ConfigurationLoadException; +import org.apache.tuscany.core.loader.LoaderContext; +import org.apache.tuscany.core.loader.StAXElementLoader; +import org.apache.tuscany.core.loader.StAXLoaderRegistry; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; + +/** + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class JSONRPCBindingLoader implements StAXElementLoader<JSONRPCBinding> { + + public static final QName BINDING_JSONRPC = new QName("http://org.apache.tuscany/xmlns/jsonrpc/0.9", "binding.jsonrpc"); + + protected StAXLoaderRegistry registry; + + @Autowire + public void setRegistry(StAXLoaderRegistry registry) { + this.registry = registry; + } + + @Init(eager = true) + public void start() { + registry.registerLoader(BINDING_JSONRPC, this); + } + + @Destroy + public void stop() { + registry.unregisterLoader(BINDING_JSONRPC, this); + } + + @SuppressWarnings("deprecation") + public JSONRPCBinding load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException { + JSONRPCBinding binding = new JSONRPCBinding(); + binding.setURI(reader.getAttributeValue(null, "uri")); + binding.setWebAppName(registry.getContext().getWebAppName()); + return binding; + } +} |