From 7ad16a07b4288227c531c485939318768129fb54 Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 3 Aug 2011 09:20:48 +0000 Subject: Tag debug mod for TUSCANY-3909 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1153403 13f79535-47bb-0310-9956-ffa450edef68 --- .../web/ComponentContextServlet.java | 156 +++++++++++++++++++++ .../implementation/web/ContextScriptProcessor.java | 32 +++++ .../web/ContextScriptProcessorExtensionPoint.java | 27 ++++ ...efaultContextScriptProcessorExtensionPoint.java | 51 +++++++ .../implementation/web/JSONRPCScripProcessor.java | 57 ++++++++ .../web/runtime/ComponentContextProxy.java | 88 ++++++++++++ .../runtime/WebImplementationProviderFactory.java | 78 +++++++++++ 7 files changed, 489 insertions(+) create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ComponentContextServlet.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessor.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessorExtensionPoint.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/DefaultContextScriptProcessorExtensionPoint.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/JSONRPCScripProcessor.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/ComponentContextProxy.java create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/WebImplementationProviderFactory.java (limited to 'sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org') diff --git a/sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ComponentContextServlet.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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 attributes = new HashMap(); + protected transient ServletContext servletContext; + private transient List contextScriptProcessors = new ArrayList(); + + 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/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessor.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/ContextScriptProcessorExtensionPoint.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/DefaultContextScriptProcessorExtensionPoint.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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 tempCSPHolder = new ArrayList(); + + 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/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/JSONRPCScripProcessor.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/ComponentContextProxy.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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 > R cast(B arg0) throws IllegalArgumentException { + return (R) getComponentContext().cast(arg0); + } + + public ServiceReference createSelfReference(Class arg0) { + return getComponentContext().createSelfReference(arg0); + } + + public ServiceReference createSelfReference(Class arg0, String arg1) { + return getComponentContext().createSelfReference(arg0, arg1); + } + + public B getProperty(Class arg0, String arg1) { + return getComponentContext().getProperty(arg0, arg1); + } + + public RequestContext getRequestContext() { + return getComponentContext().getRequestContext(); + } + + public B getService(Class arg0, String arg1) { + return getComponentContext().getService(arg0, arg1); + } + + public ServiceReference getServiceReference(Class arg0, String arg1) { + return getComponentContext().getServiceReference(arg0, arg1); + } + + public String getURI() { + return getComponentContext().getURI(); + } + + public Collection> getServiceReferences(Class businessInterface, String referenceName) { + return ((ComponentContextExtension)getComponentContext()).getServiceReferences(businessInterface, referenceName); + } + + public Collection getServices(Class businessInterface, String referenceName) { + return ((ComponentContextExtension)getComponentContext()).getServices(businessInterface, referenceName); + } + +} diff --git a/sca-java-1.x/tags/1.6.1-TUSCANY-3909/implementation-web-runtime/src/main/java/org/apache/tuscany/sca/implementation/web/runtime/WebImplementationProviderFactory.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/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/tags/1.6.1-TUSCANY-3909/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 { + + private ServletHost servletHost; + private ComponentContextServlet contextServlet; + + public WebImplementationProviderFactory(ExtensionPointRegistry extensionPoints) { + ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class); + List 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 getModelType() { + return WebImplementation.class; + } + +} -- cgit v1.2.3