diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-13 01:42:27 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-13 01:42:27 +0000 |
commit | 3c7c4a749baafcf375f4785a7668d3a25c9063e3 (patch) | |
tree | a66b8b031c5dc6c7744f44b44b6bcd371bd0b8df /sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org | |
parent | eb03ff672236cddf65533f39b83ddd5e2984a2bb (diff) |
Moving 1.x trunk
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835700 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org')
7 files changed, 489 insertions, 0 deletions
diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ComponentContextServlet.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ComponentContextServlet.java new file mode 100644 index 0000000000..bf33f7ad9e --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ComponentContextServlet.java @@ -0,0 +1,156 @@ +/* + * 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.implementation.web; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * Servlet that handles the GET request for the componentContext.js script + * + * The script is generated by calling ScriptProcessors to output the code + * for the SCA references, properties etc. + */ +public class ComponentContextServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected static final String HEADER = "/** --- Apache Tuscany componentContext.js --- */\n"; + protected static final String FOOTER = "/** --- Apache Tuscany componentContext.js EOF --- */\n"; + + public static final String COMPONENT_CONTEXT_SCRIPT_URI = "org.apache.tuscany.sca.componentContext.js"; + + protected transient Map<String, Object> attributes = new HashMap<String, Object>(); + protected transient ServletContext servletContext; + private transient List<ContextScriptProcessor> contextScriptProcessors = new ArrayList<ContextScriptProcessor>(); + + public ComponentContextServlet() { + contextScriptProcessors.add(new JSONRPCScripProcessor()); + } + + @Override + public void init(ServletConfig servletConfig) throws ServletException { + this.servletContext = servletConfig.getServletContext(); + if (attributes.size() > 0) { + for (String name : attributes.keySet()) { + servletContext.setAttribute(name, attributes.get(name)); + } + } + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException { + response.setContentType("text/plain;charset=utf-8"); + PrintWriter out = response.getWriter(); + + out.write(HEADER); + + doScriptInit(req, response); + + doScriptReferences(req, response); + + out.write(FOOTER); + + out.flush(); + out.close(); + } + + /** + * Calls each ContextScriptProcessor once to insert any required initilization code into componentContext.js + */ + protected void doScriptInit(HttpServletRequest req, HttpServletResponse response) throws IOException, UnsupportedEncodingException { + + PrintWriter out = response.getWriter(); + + out.println("if (SCA == undefined) var SCA = new Object();"); + out.println("if (SCA.componentContext == undefined) {"); + out.println(" SCA.componentContext = new Object();"); + out.println(" SCA.componentContext.serviceNames = [];"); + out.println(" SCA.componentContext.serviceProxys = [];"); + out.println(" SCA.componentContext.getService = function(serviceName){"); + out.println(" var i = SCA.componentContext.serviceNames.indexOf(serviceName);"); + out.println(" return SCA.componentContext.serviceProxys[i];"); + out.println(" };"); + out.println(" if (componentContext == undefined) var componentContext = SCA.componentContext;"); + out.println("}"); + + for (ContextScriptProcessor csp : contextScriptProcessors) { + csp.scriptInit(req, response); + } + } + + /** + * Calls each ContextScriptProcessor for each SCA reference to insert code for the reference into componentContext.js + */ + protected void doScriptReferences(HttpServletRequest req, HttpServletResponse response) throws IOException, UnsupportedEncodingException { + + PrintWriter out = response.getWriter(); + + out.write("// SCA References\n"); + + RuntimeComponent component = (RuntimeComponent)servletContext.getAttribute("org.apache.tuscany.sca.implementation.web.RuntimeComponent"); + + for (ComponentReference cr : component.getReferences()) { + String ref = "// SCA Reference " + cr.getName() + "\n"; + out.write(ref); + for (ContextScriptProcessor csp : contextScriptProcessors) { + csp.scriptReference(cr, req, response); + } + } + + out.write("\n// SCA References end.\n"); + } + + /** + * Calls each ContextScriptProcessor for each SCA property to insert code for the property into componentContext.js + */ + protected void doScriptProperties(HttpServletRequest req, HttpServletResponse response) throws IOException, UnsupportedEncodingException { + // TODO: support properties + } + + /** + * Set an attribute on the ServletContext + */ + public void setAttribute(String name, Object value) { + if (servletContext != null) { + servletContext.setAttribute(name, value); + } else { + attributes.put(name, value); + } + } + + public void addContextScriptProcessor(ContextScriptProcessor csp) { + contextScriptProcessors.add(csp); + } +} diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessor.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessor.java new file mode 100644 index 0000000000..02c99debb0 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessor.java @@ -0,0 +1,32 @@ +/* + * 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.implementation.web; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.assembly.ComponentReference; + +public interface ContextScriptProcessor { + void scriptInit(HttpServletRequest req, HttpServletResponse response) throws IOException; + void scriptReference(ComponentReference cr, HttpServletRequest req, HttpServletResponse response) throws IOException; +} diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessorExtensionPoint.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessorExtensionPoint.java new file mode 100644 index 0000000000..6d3f590309 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessorExtensionPoint.java @@ -0,0 +1,27 @@ +/* + * 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.implementation.web; + + +public interface ContextScriptProcessorExtensionPoint { + + void addContextScriptProcessor(ContextScriptProcessor csp); + +} diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/DefaultContextScriptProcessorExtensionPoint.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/DefaultContextScriptProcessorExtensionPoint.java new file mode 100644 index 0000000000..89b951e7d2 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/DefaultContextScriptProcessorExtensionPoint.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 org.apache.tuscany.sca.implementation.web; + +import java.util.ArrayList; +import java.util.List; + + +public class DefaultContextScriptProcessorExtensionPoint implements ContextScriptProcessorExtensionPoint { + + protected ComponentContextServlet componentContextServlet; + protected List<ContextScriptProcessor> tempCSPHolder = new ArrayList<ContextScriptProcessor>(); + + public DefaultContextScriptProcessorExtensionPoint() { + } + + public void addContextScriptProcessor(ContextScriptProcessor csp) { + if (componentContextServlet != null) { + componentContextServlet.addContextScriptProcessor(csp); + } else { + tempCSPHolder.add(csp); + } + } + + public void setComponentContextServlet(ComponentContextServlet servlet) { + componentContextServlet = servlet; + if (tempCSPHolder.size() > 0) { + for (ContextScriptProcessor csp : tempCSPHolder) { + componentContextServlet.addContextScriptProcessor(csp); + } + } + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/JSONRPCScripProcessor.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/JSONRPCScripProcessor.java new file mode 100644 index 0000000000..e4ec0d71f9 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/JSONRPCScripProcessor.java @@ -0,0 +1,57 @@ +/*
+ * 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.implementation.web;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+
+public class JSONRPCScripProcessor implements ContextScriptProcessor {
+
+ public void scriptInit(HttpServletRequest req, HttpServletResponse response) throws IOException {
+ PrintWriter out = response.getWriter();
+ InputStream is = getClass().getClassLoader().getResourceAsStream("jsonrpc.js");
+ if (is != null) {
+ int i;
+ while ((i = is.read()) != -1) {
+ out.write(i);
+ }
+ }
+
+ out.println();
+ }
+
+ public void scriptReference(ComponentReference cr, HttpServletRequest req, HttpServletResponse response) throws IOException {
+ for (Binding b : cr.getBindings()) {
+ if ("org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding".equals(b.getClass().getName())) {
+ PrintWriter out = response.getWriter();
+ out.println("SCA.componentContext.serviceNames.push('" + cr.getName() + "');");
+ out.println("SCA.componentContext.serviceProxys.push(new JSONRpcClient('" + cr.getReference().getTargets().get(0).getName() + "').Service);");
+ }
+ }
+ }
+
+}
diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/ComponentContextProxy.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/ComponentContextProxy.java new file mode 100644 index 0000000000..c4f39bded8 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/ComponentContextProxy.java @@ -0,0 +1,88 @@ +/*
+ * 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.implementation.web.runtime;
+
+import java.util.Collection;
+
+import org.apache.tuscany.sca.api.ComponentContextExtension;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.osoa.sca.CallableReference;
+import org.osoa.sca.ComponentContext;
+import org.osoa.sca.RequestContext;
+import org.osoa.sca.ServiceReference;
+
+/**
+ * Proxy ComponentContext wrappering a RuntimeComponent as the
+ * RuntimeComponent ComponentContext has not been created till later
+ */
+public class ComponentContextProxy implements ComponentContext {
+
+ protected RuntimeComponent runtimeComponent;
+
+ public ComponentContextProxy(RuntimeComponent runtimeComponent) {
+ this.runtimeComponent = runtimeComponent;
+ }
+
+ protected ComponentContext getComponentContext() {
+ return runtimeComponent.getComponentContext();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <B, R extends CallableReference<B>> R cast(B arg0) throws IllegalArgumentException {
+ return (R) getComponentContext().cast(arg0);
+ }
+
+ public <B> ServiceReference<B> createSelfReference(Class<B> arg0) {
+ return getComponentContext().createSelfReference(arg0);
+ }
+
+ public <B> ServiceReference<B> createSelfReference(Class<B> arg0, String arg1) {
+ return getComponentContext().createSelfReference(arg0, arg1);
+ }
+
+ public <B> B getProperty(Class<B> arg0, String arg1) {
+ return getComponentContext().getProperty(arg0, arg1);
+ }
+
+ public RequestContext getRequestContext() {
+ return getComponentContext().getRequestContext();
+ }
+
+ public <B> B getService(Class<B> arg0, String arg1) {
+ return getComponentContext().getService(arg0, arg1);
+ }
+
+ public <B> ServiceReference<B> getServiceReference(Class<B> arg0, String arg1) {
+ return getComponentContext().getServiceReference(arg0, arg1);
+ }
+
+ public String getURI() {
+ return getComponentContext().getURI();
+ }
+
+ public <B> Collection<ServiceReference<B>> getServiceReferences(Class<B> businessInterface, String referenceName) {
+ return ((ComponentContextExtension)getComponentContext()).getServiceReferences(businessInterface, referenceName);
+ }
+
+ public <B> Collection<B> getServices(Class<B> businessInterface, String referenceName) {
+ return ((ComponentContextExtension)getComponentContext()).getServices(businessInterface, referenceName);
+ }
+
+}
diff --git a/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/WebImplementationProviderFactory.java b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/WebImplementationProviderFactory.java new file mode 100644 index 0000000000..2518ba5381 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/WebImplementationProviderFactory.java @@ -0,0 +1,78 @@ +/*
+ * 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.implementation.web.runtime;
+
+import java.util.List;
+
+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.implementation.web.ComponentContextServlet;
+import org.apache.tuscany.sca.implementation.web.ContextScriptProcessorExtensionPoint;
+import org.apache.tuscany.sca.implementation.web.DefaultContextScriptProcessorExtensionPoint;
+import org.apache.tuscany.sca.implementation.web.WebImplementation;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.provider.ImplementationProvider;
+import org.apache.tuscany.sca.provider.ImplementationProviderFactory;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+public class WebImplementationProviderFactory implements ImplementationProviderFactory<WebImplementation> {
+
+ private ServletHost servletHost;
+ private ComponentContextServlet contextServlet;
+
+ public WebImplementationProviderFactory(ExtensionPointRegistry extensionPoints) {
+ ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class);
+ List<ServletHost> hosts = servletHosts.getServletHosts();
+ if (!hosts.isEmpty()) {
+ this.servletHost = hosts.get(0);
+ }
+
+ contextServlet = new ComponentContextServlet();
+
+ DefaultContextScriptProcessorExtensionPoint dcspep = (DefaultContextScriptProcessorExtensionPoint)extensionPoints.getExtensionPoint(ContextScriptProcessorExtensionPoint.class);
+ dcspep.setComponentContextServlet(contextServlet);
+ }
+
+ public ImplementationProvider createImplementationProvider(RuntimeComponent component, WebImplementation implementation) {
+
+ servletHost.addServletMapping("org.osoa.sca.componentContext.js", contextServlet);
+ contextServlet.setAttribute("org.osoa.sca.ComponentContext", new ComponentContextProxy(component));
+ contextServlet.setAttribute("org.apache.tuscany.sca.implementation.web.RuntimeComponent", component);
+
+ return new ImplementationProvider() {
+ public Invoker createInvoker(RuntimeComponentService arg0, Operation arg1) {
+ throw new UnsupportedOperationException("Components using implementation.web have no services");
+ }
+ public void start() {
+ }
+ public void stop() {
+ }
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
+ };
+ }
+
+ public Class<WebImplementation> getModelType() {
+ return WebImplementation.class;
+ }
+
+}
|