diff options
Diffstat (limited to 'sandbox/old/contrib/implementation-script/container.jsr223')
16 files changed, 1402 insertions, 0 deletions
diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/LICENSE.txt b/sandbox/old/contrib/implementation-script/container.jsr223/LICENSE.txt new file mode 100755 index 0000000000..0084319535 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/LICENSE.txt @@ -0,0 +1,202 @@ + + 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, serviceDefinition 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/sandbox/old/contrib/implementation-script/container.jsr223/NOTICE.txt b/sandbox/old/contrib/implementation-script/container.jsr223/NOTICE.txt new file mode 100644 index 0000000000..ecc68a9614 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/NOTICE.txt @@ -0,0 +1,14 @@ +${pom.name}
+Copyright (c) 2005 - 2006 The Apache Software Foundation
+
+Apache Tuscany is an effort undergoing incubation at The Apache Software
+Foundation (ASF), sponsored by the Apache Web Services PMC. Incubation is
+required of all newly accepted projects until a further review indicates that
+the infrastructure, communications, and decision making process have stabilized
+in a manner consistent with other successful ASF projects. While incubation
+status is not necessarily a reflection of the completeness or stability of the
+code, it does indicate that the project has yet to be fully endorsed by the ASF.
+
+This product includes software developed by
+The Apache Software Foundation (http://www.apache.org/).
+
diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/pom.xml.off b/sandbox/old/contrib/implementation-script/container.jsr223/pom.xml.off new file mode 100644 index 0000000000..f2ed60361b --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/pom.xml.off @@ -0,0 +1,118 @@ +<?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.
+-->
+<project>
+
+ <parent>
+ <groupId>org.apache.tuscany.sca.script</groupId>
+ <artifactId>parent</artifactId>
+ <version>2.0-alpha2-incubating-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>container-jsr223</artifactId>
+ <name>Apache Tuscany Script Container using JSR-223</name>
+ <description>Apache Tuscany Script Container using JSR-223</description>
+
+ <repositories>
+ <!-- This is reqquired for Jython as 2.2 Beta1 hasn't made it to the maven repos yet -->
+ <repository>
+ <id>ant</id>
+ <url>http://people.apache.org/~antelder/maven2</url>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca.kernel</groupId>
+ <artifactId>tuscany-spi</artifactId>
+ <version>2.0-alpha2-incubating-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.bsf</groupId>
+ <artifactId>bsf-all</artifactId>
+ <version>3.0-beta1-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>asm</groupId>
+ <artifactId>asm</artifactId>
+ <version>2.2</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>2.2</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymockclassextension</artifactId>
+ <version>2.2</version>
+ <scope>test</scope>
+ </dependency>
+
+
+<!-- TODO: big hack to add script engine dependencies till extension dependencies fixed -->
+
+ <dependency>
+ <groupId>rhino</groupId>
+ <artifactId>js</artifactId>
+ <version>1.6R5</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jruby</groupId>
+ <artifactId>jruby-complete</artifactId>
+ <version>0.9.8</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>jython</groupId>
+ <artifactId>jython</artifactId>
+ <version>2.2b1</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ <version>1.0</version>
+ <scope>compile</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/InstanceWrapperBase.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/InstanceWrapperBase.java new file mode 100644 index 0000000000..730512ca58 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/InstanceWrapperBase.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.extension.script; + +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetInitializationException; + +/** + * TODO: this should be in the SPI + */ +public class InstanceWrapperBase<T> implements InstanceWrapper<T> { + protected final T instance; + private boolean started; + + public InstanceWrapperBase(T instance) { + assert instance != null; + this.instance = instance; + } + + public T getInstance() { + assert started; + return instance; + } + + public boolean isStarted() { + return started; + } + + public void start() throws TargetInitializationException { + started = true; + } + + public void stop() throws TargetDestructionException { + started = false; + } +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java new file mode 100644 index 0000000000..874e978e77 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java @@ -0,0 +1,187 @@ +/* + * 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.extension.script; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.AtomicComponentExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.physical.PhysicalOperationDefinition; +import org.apache.tuscany.spi.wire.ProxyService; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.Wire; + +public class ScriptComponent extends AtomicComponentExtension { + + private ScriptImplementation impl; + protected Map<String, List<Wire>> wires = new HashMap<String, List<Wire>>(); + + protected ScriptComponent(URI name, + ScriptImplementation implementation, + ProxyService proxyService, + WorkContext workContext, + URI groupId, + int initLevel) { + super(name, proxyService, workContext, groupId, initLevel); + impl = implementation; + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + return new ScriptInvoker(operation.getName(), this, scopeContainer, workContext); + } + + @SuppressWarnings("unchecked") + public InstanceWrapper<?> createInstanceWrapper() throws ObjectCreationException { + try { + + Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); // TODO: classloader? + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByExtension(impl.getScriptLanguage()); + if (engine == null) { + throw new ObjectCreationException("no script engine found for language: " + impl.getScriptLanguage()); + } + + Reader reader; + if (impl.getInlineSrc() == null) { + URL url = impl.getClassLoader().getResource(impl.getScriptName()); + reader = new InputStreamReader(url.openStream()); + } else { + reader = new StringReader(impl.getInlineSrc()); + } + + engine.eval(reader); + + return new InstanceWrapperBase(engine); + + } catch (ScriptException e) { + throw new ObjectCreationException(e); + } catch (IOException e) { + throw new ObjectCreationException(e); + } + } + + // TODO: move all the rest to SPI extension + + @Deprecated + public Object createInstance() throws ObjectCreationException { + throw new UnsupportedOperationException(); + } + + public Object getAssociatedTargetInstance() throws TargetResolutionException { + // TODO Auto-generated method stub + return null; + } + + public Object getTargetInstance() throws TargetResolutionException { + // TODO Auto-generated method stub + return null; + } + + public void attachCallbackWire(Wire wire) { + // TODO Auto-generated method stub + + } + + public void attachWire(Wire wire) { + assert wire.getSourceUri().getFragment() != null; + String referenceName = wire.getSourceUri().getFragment(); + List<Wire> wireList = wires.get(referenceName); + if (wireList == null) { + wireList = new ArrayList<Wire>(); + wires.put(referenceName, wireList); + } + wireList.add(wire); +// Member member = referenceSites.get(referenceName); +// if (member != null) { +// injectors.add(createInjector(member, wire)); +// } +// // cycle through constructor param names as well +// for (int i = 0; i < constructorParamNames.size(); i++) { +// if (referenceName.equals(constructorParamNames.get(i))) { +// ObjectFactory[] initializerFactories = instanceFactory.getInitializerFactories(); +// initializerFactories[i] = createWireFactory(constructorParamTypes.get(i), wire); +// break; +// } +// } +// //TODO error if ref not set on constructor or ref site + + } + + + public void attachWires(List<Wire> attachWires) { + assert attachWires.size() > 0; + assert attachWires.get(0).getSourceUri().getFragment() != null; + String referenceName = attachWires.get(0).getSourceUri().getFragment(); + List<Wire> wireList = wires.get(referenceName); + if (wireList == null) { + wireList = new ArrayList<Wire>(); + wires.put(referenceName, wireList); + } + wireList.addAll(attachWires); +// Member member = referenceSites.get(referenceName); +// if (member == null) { +// if (constructorParamNames.contains(referenceName)) { +// // injected on the constructor +// throw new UnsupportedOperationException(); +// } else { +// throw new NoAccessorException(referenceName); +// } +// } +// +// Class<?> type = attachWires.get(0).getSourceContract().getInterfaceClass(); +// if (type == null) { +// throw new NoMultiplicityTypeException("Java interface must be specified for multiplicity", referenceName); +// } +// injectors.add(createMultiplicityInjector(member, type, wireList)); + + } + + public List<Wire> getWires(String name) { + return wires.get(name); + } + + public TargetInvoker createTargetInvoker(String targetName, PhysicalOperationDefinition operation) { + throw new UnsupportedOperationException(); + } + + protected <B> ObjectFactory<B> createWireFactory(Class<B> interfaze, Wire wire) { + return new WireObjectFactory<B>(interfaze, wire, proxyService); + } + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentBuilder.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentBuilder.java new file mode 100644 index 0000000000..508cd7bc37 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentBuilder.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.extension.script; + +import java.net.URI; + +import org.apache.tuscany.spi.builder.BuilderException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.ReferenceDefinition; + +public class ScriptComponentBuilder extends ComponentBuilderExtension<ScriptImplementation> { + + public ScriptComponentBuilder() { + } + + @Override + protected Class<ScriptImplementation> getImplementationType() { + return ScriptImplementation.class; + } + + public Component build(ComponentDefinition componentDefinition, DeploymentContext context) throws BuilderException { + + // setup reference injection sites + ComponentType componentType = componentDefinition.getImplementation().getComponentType(); + + for (Object o : componentType.getReferences().values()) { + ReferenceDefinition reference = (ReferenceDefinition) o; + System.out.println(reference); + } + + URI name = componentDefinition.getUri(); + ScriptImplementation impl = (ScriptImplementation)componentDefinition.getImplementation(); + URI groupId = context.getComponentId(); + + Component scriptComponent = new ScriptComponent(name, impl, proxyService, workContext, groupId, 0); + return scriptComponent; + } + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentType.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentType.java new file mode 100644 index 0000000000..bd01317873 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentType.java @@ -0,0 +1,37 @@ +/* + * 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.extension.script; + +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +/** + * A componentType for script components + * TODO: need lifecycle methods init/destroy + */ +public class ScriptComponentType extends ComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> { + + public ScriptComponentType() { + this.implementationScope = Scope.COMPOSITE; + } + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentTypeLoader.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentTypeLoader.java new file mode 100644 index 0000000000..1e2f7e31f8 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentTypeLoader.java @@ -0,0 +1,68 @@ +/* + * 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.extension.script; + +import java.net.URL; + +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.model.ComponentType; + +/** + * ComponentType loader for script components + */ +public class ScriptComponentTypeLoader extends ComponentTypeLoaderExtension<ScriptImplementation> { + + public ScriptComponentTypeLoader() { + } + + @Override + protected Class<ScriptImplementation> getImplementationClass() { + return ScriptImplementation.class; + } + + public void load(ScriptImplementation implementation, DeploymentContext deploymentContext) throws LoaderException { + + String sideFile = getSideFileName(implementation.getScriptName()); + URL resource = implementation.getClassLoader().getResource(sideFile); + + ScriptComponentType componentType; + if (resource == null) { + // TODO: or else implement introspection + throw new LoaderException("ComponentType side file not found", sideFile); + } else { + componentType = + (ScriptComponentType)loaderRegistry.load(new ScriptComponentType(), + resource, + ComponentType.class, + deploymentContext); + } + implementation.setComponentType(componentType); + } + + protected String getSideFileName(String resourceName) { + int lastDot = resourceName.lastIndexOf('.'); + if (lastDot != -1) { + resourceName = resourceName.substring(0, lastDot); + } + return resourceName + ".componentType"; + } + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementation.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementation.java new file mode 100644 index 0000000000..96eb1cfd21 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementation.java @@ -0,0 +1,62 @@ +/* + * 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.extension.script; + +import org.apache.tuscany.spi.model.Implementation; + +/** + * Model object for a script implementation. + */ +public class ScriptImplementation extends Implementation<ScriptComponentType> { + + private String scriptName; + private String inlineSrc; + private String scriptLanguage; + private String scriptClassName; + private ClassLoader classLoader; + + public ScriptImplementation(String scriptName, String inlineSrc, String scriptLanguage, String scriptClassName, ClassLoader classLoader) { + this.scriptName = scriptName; + this.inlineSrc = inlineSrc; + this.scriptLanguage = scriptLanguage; + this.scriptClassName = scriptClassName; + this.classLoader = classLoader; + } + + public String getScriptClassName() { + return scriptClassName; + } + + public String getScriptName() { + return scriptName; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public String getScriptLanguage() { + return scriptLanguage; + } + + public String getInlineSrc() { + return inlineSrc; + } + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementationLoader.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementationLoader.java new file mode 100644 index 0000000000..75e2ecea95 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementationLoader.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. + */ +package org.apache.tuscany.extension.script; + +import static org.osoa.sca.Constants.SCA_NS; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.loader.MissingResourceException; +import org.apache.tuscany.spi.model.ModelObject; +import org.osoa.sca.annotations.Constructor; +import org.osoa.sca.annotations.Reference; + +/** + * Loader for handling implementation.script elements. <p/> + * <code><implementation.script script="path/foo.py" [language="lang" class="myclass"]></code> + */ +public class ScriptImplementationLoader extends LoaderExtension<ScriptImplementation> { + + private static final QName IMPLEMENTATION_SCRIPT = new QName(SCA_NS, "implementation.script"); + + @Constructor( {"registry"}) + public ScriptImplementationLoader(@Reference LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPLEMENTATION_SCRIPT; + } + + public ScriptImplementation load(ModelObject mo, XMLStreamReader reader, DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + + String scriptName = reader.getAttributeValue(null, "script"); + if (scriptName != null && scriptName.length() < 1) { + scriptName = null; + } + + String scriptLanguage = reader.getAttributeValue(null, "language"); + if (scriptLanguage == null || scriptLanguage.length() < 1) { + int i = scriptName.lastIndexOf('.'); + if (i > 0) { + scriptLanguage = scriptName.substring(i + 1); + } + } + if (scriptLanguage == null || scriptLanguage.length() < 1) { + throw new LoaderException("unable to determine script language"); + } + + String scriptClassName = reader.getAttributeValue(null, "class"); + + String scriptSrc = null; +// String scriptSrc = reader.getElementText(); +// if (scriptSrc != null && scriptSrc.length() < 1) { +// scriptSrc = null; +// } + if (scriptName == null && scriptSrc == null) { + throw new MissingResourceException("no 'script' attribute or inline script source"); + } + if (scriptName != null && scriptSrc != null) { + throw new MissingResourceException("cannot use both 'script' attribute and inline script"); + } + + LoaderUtil.skipToEndElement(reader); + + ClassLoader cl = deploymentContext.getClassLoader(); + + ScriptImplementation impl = new ScriptImplementation(scriptName, scriptSrc, scriptLanguage, scriptClassName, cl); + + registry.loadComponentType(impl, deploymentContext); + + return impl; + } +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptInvoker.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptInvoker.java new file mode 100644 index 0000000000..e7c2e656a2 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptInvoker.java @@ -0,0 +1,130 @@ +/* + * 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.extension.script; + +import java.lang.reflect.InvocationTargetException; + +import javax.script.Invocable; +import javax.script.ScriptException; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ComponentException; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.InvalidConversationSequenceException; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.TargetInvokerExtension; +import org.apache.tuscany.spi.model.Scope; + +/** + * Perform the actual script invocation + * TODO: move vertually all of this to SPI TargetInvokerExtension + */ +@SuppressWarnings("deprecation") +public class ScriptInvoker<T> extends TargetInvokerExtension { + + protected Object clazz; + protected String operationName; + + private final AtomicComponent<T> component; + private final ScopeContainer scopeContainer; + protected InstanceWrapper<T> target; + protected boolean stateless; + + public ScriptInvoker(String operationName, + AtomicComponent component, + ScopeContainer scopeContainer, + WorkContext workContext) { + + this.operationName = operationName; + this.component = component; + this.scopeContainer = scopeContainer; + stateless = Scope.STATELESS == scopeContainer.getScope(); + + // TODO: support script classes + } + + public Object invokeTarget(Object payload, short sequence, WorkContext workContext) throws InvocationTargetException { + Object contextId = workContext.getIdentifier(scopeContainer.getScope()); + try { + + InstanceWrapper<T> wrapper = getInstance(sequence, contextId); + Invocable scriptEngine = (Invocable)wrapper.getInstance(); + + Object ret; + if (clazz == null) { + ret = scriptEngine.invokeFunction(operationName, (Object[])payload); + } else { + ret = scriptEngine.invokeMethod(clazz, operationName, (Object[])payload); + } + + scopeContainer.returnWrapper(component, wrapper, contextId); + if (sequence == END) { + // if end conversation, remove resource + scopeContainer.remove(component); + } + + return ret; + + } catch (ScriptException e) { + throw new InvocationTargetException(e); + } catch (ComponentException e) { + throw new InvocationTargetException(e); + } + } + + @Override + public ScriptInvoker clone() throws CloneNotSupportedException { + try { + ScriptInvoker invoker = (ScriptInvoker)super.clone(); + invoker.target = null; + return invoker; + } catch (CloneNotSupportedException e) { + return null; // will not happen + } + } + + /** + * Resolves the target service instance or returns a cached one + */ + protected InstanceWrapper<T> getInstance(short sequence, Object contextId) throws TargetException { + switch (sequence) { + case NONE: + if (cacheable) { + if (target == null) { + target = scopeContainer.getWrapper(component, contextId); + } + return target; + } else { + return scopeContainer.getWrapper(component, contextId); + } + case START: + assert !cacheable; + return scopeContainer.getWrapper(component, contextId); + case CONTINUE: + case END: + assert !cacheable; + return scopeContainer.getAssociatedWrapper(component, contextId); + default: + throw new InvalidConversationSequenceException("Unknown sequence type", String.valueOf(sequence)); + } + } +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireObjectFactory.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireObjectFactory.java new file mode 100644 index 0000000000..9cec649cdb --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireObjectFactory.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.extension.script; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.wire.ChainHolder; +import org.apache.tuscany.spi.wire.Wire; +import org.apache.tuscany.spi.wire.ProxyService; + +/** + * Uses a wire to return an object instance + * @Deprecated + * + * @version $Rev$ $Date$ + */ +public class WireObjectFactory<T> implements ObjectFactory<T> { + private Class<T> interfaze; + private Wire wire; + private ProxyService proxyService; + // the cache of proxy interface method to operation mappings + private Map<Method, ChainHolder> mappings; + private boolean optimizable; + + /** + * Constructor. + * + * @param interfaze the interface to inject on the client + * @param wire the backing wire + * @param proxyService the wire service to create the proxy + * @throws NoMethodForOperationException + */ + public WireObjectFactory(Class<T> interfaze, Wire wire, ProxyService proxyService) + { + this.interfaze = interfaze; + this.wire = wire; + this.proxyService = proxyService; + this.mappings = WireUtils.createInterfaceToWireMapping(interfaze, wire); // TODO + if (wire.isOptimizable() + && wire.getSourceContract().getInterfaceClass() != null + && interfaze.isAssignableFrom(wire.getSourceContract().getInterfaceClass())) { + optimizable = true; + } + } + + public T getInstance() throws ObjectCreationException { + if (optimizable) { + try { + return interfaze.cast(wire.getTargetInstance()); + } catch (TargetResolutionException e) { + throw new ObjectCreationException(e); + } + } else { + // clone the cached mappings + Map<Method, ChainHolder> newChains = new HashMap<Method, ChainHolder>(mappings.size()); + for (Map.Entry<Method, ChainHolder> entry : mappings.entrySet()) { + newChains.put(entry.getKey(), entry.getValue().clone()); + } + return interfaze.cast(proxyService.createProxy(interfaze, wire, newChains)); + } + } + + +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireUtils.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireUtils.java new file mode 100644 index 0000000000..b63c3c881a --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireUtils.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.extension.script; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod; +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod2; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.physical.PhysicalOperationDefinition; +import org.apache.tuscany.spi.wire.ChainHolder; +import org.apache.tuscany.spi.wire.Interceptor; +import org.apache.tuscany.spi.wire.InvocationChain; +import org.apache.tuscany.spi.wire.ProxyCreationException; +import org.apache.tuscany.spi.wire.Wire; + +/** + * Utilities for operating on wires + * + * @version $Rev$ $Date$ + */ +public final class WireUtils { + + private WireUtils() { + } + + + /** + * Maps methods on an interface to operations on a wire + * + * @param interfaze the interface to map from + * @param wire the wire to map to + * @return a collection of method to operation mappings + * @throws NoMethodForOperationException + * @Deprecated + */ + public static Map<Method, ChainHolder> createInterfaceToWireMapping(Class<?> interfaze, Wire wire) { + Map<Operation<?>, InvocationChain> invocationChains = wire.getInvocationChains(); + + Map<Method, ChainHolder> chains = new HashMap<Method, ChainHolder>(invocationChains.size()); + for (Map.Entry<Operation<?>, InvocationChain> entry : invocationChains.entrySet()) { + Operation operation = entry.getKey(); + try { + Method method = findMethod(interfaze, operation); + chains.put(method, new ChainHolder(entry.getValue())); + } catch (NoSuchMethodException e) { + throw new RuntimeException(operation.getName()); + } + } + return chains; + } + + public static Map<Method, InvocationChain> createInterfaceToWireMapping2(Class<?> interfaze, Wire wire) { + Map<PhysicalOperationDefinition, InvocationChain> invocationChains = wire.getPhysicalInvocationChains(); + + Map<Method, InvocationChain> chains = new HashMap<Method, InvocationChain>(invocationChains.size()); + for (Map.Entry<PhysicalOperationDefinition, InvocationChain> entry : invocationChains.entrySet()) { + PhysicalOperationDefinition operation = entry.getKey(); + try { + Method method = findMethod2(interfaze, operation); + chains.put(method, entry.getValue()); + } catch (NoSuchMethodException e) { + throw new RuntimeException(operation.getName()); + } catch (ClassNotFoundException e) { + throw new ProxyCreationException(e); + } + } + return chains; + } + + /** + * Determines if the given wire is optimizable, i.e. its invocation chains may be bypassed during an invocation. + * This is typically calculated during the connect phase to optimize away invocation chains. + * + * @param wire the wire + * @return true if the wire is optimizable + */ + public static boolean isOptimizable(Wire wire) { + for (InvocationChain chain : wire.getInvocationChains().values()) { + if (chain.getHeadInterceptor() != null) { + Interceptor current = chain.getHeadInterceptor(); + if (current == null) { + break; + } + while (current != null) { + if (!current.isOptimizable()) { + return false; + } + current = current.getNext(); + } + } + } + // if there is a callback, the wire is never optimizable since the callback target needs to be disambiguated + return wire.getCallbackInvocationChains().isEmpty(); + } +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl new file mode 100644 index 0000000000..b075194f7f --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl @@ -0,0 +1,40 @@ +<?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. +--> +<!-- + JavaScript configuration for the launcher environment. +--> +<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" + xmlns:system="http://tuscany.apache.org/xmlns/sca/system/2.0-alpha" + name="org.apache.tuscany.extension.script.jsr223" + autowire="true"> + + <component name="script.implementationLoader"> + <system:implementation.system class="org.apache.tuscany.extension.script.ScriptImplementationLoader"/> + </component> + + <component name="script.componentTypeLoader"> + <system:implementation.system class="org.apache.tuscany.extension.script.ScriptComponentTypeLoader"/> + </component> + + <component name="script.componentBuilder"> + <system:implementation.system class="org.apache.tuscany.extension.script.ScriptComponentBuilder"/> + </component> + +</composite> diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptImplementationLoaderTestCase.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptImplementationLoaderTestCase.java new file mode 100644 index 0000000000..60495b6763 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptImplementationLoaderTestCase.java @@ -0,0 +1,84 @@ +/* + * 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.extension.script; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.easymock.EasyMock; + +public class ScriptImplementationLoaderTestCase extends TestCase { + + private String XML_START = + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:tuscany=\"http://tuscany.apache.org/xmlns/sca/2.0-alpha\">"; + private String XML_END = "</composite>"; + + public void testLoadNamedScript() throws XMLStreamException, LoaderException { + + String xml = + XML_START + "<implementation.script script=\"path/foo.py\" language=\"myLang\" class=\"myClass\" />" + + XML_END; + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xml)); + reader.next(); + reader.next(); + + LoaderRegistry reg = EasyMock.createNiceMock(LoaderRegistry.class); + ScriptImplementationLoader loader = new ScriptImplementationLoader(reg); + DeploymentContext deploymentContext = EasyMock.createNiceMock(DeploymentContext.class); + ScriptImplementation impl = loader.load(null, reader, deploymentContext); + + assertEquals("path/foo.py", impl.getScriptName()); + assertEquals("myLang", impl.getScriptLanguage()); + assertEquals("myClass", impl.getScriptClassName()); + assertNull(impl.getInlineSrc()); + } + +// public void testLoadInlineScript() throws XMLStreamException, LoaderException { +// +// String xml = +// XML_START + "<implementation.script language=\"myLang\" class=\"myClass\">" +// + "myScriptSrc" +// + "</implementation.script>" +// + XML_END; +// XMLInputFactory factory = XMLInputFactory.newInstance(); +// XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xml)); +// reader.next(); +// reader.next(); +// +// LoaderRegistry reg = EasyMock.createNiceMock(LoaderRegistry.class); +// ScriptImplementationLoader loader = new ScriptImplementationLoader(reg); +// DeploymentContext deploymentContext = EasyMock.createNiceMock(DeploymentContext.class); +// ScriptImplementation impl = loader.load(null, reader, deploymentContext); +// +// assertNull(impl.getScriptName()); +// assertEquals("myScriptSrc", impl.getInlineSrc()); +// assertEquals("myLang", impl.getScriptLanguage()); +// assertEquals("myClass", impl.getScriptClassName()); +// } +} diff --git a/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptInvokerTestCase.java b/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptInvokerTestCase.java new file mode 100644 index 0000000000..26373595f6 --- /dev/null +++ b/sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptInvokerTestCase.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.extension.script; + +import java.lang.reflect.InvocationTargetException; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import junit.framework.TestCase; + +import org.apache.tuscany.spi.component.ScopeContainer; +import org.easymock.EasyMock; + +public class ScriptInvokerTestCase extends TestCase { + + private ScriptEngine engine; + private ScopeContainer mockScopeContainer; + + public void testInvokeTarget() throws ScriptException, InvocationTargetException { +// engine.eval("function foo(s) {return 'hi ' + s; }"); +// ScriptInvoker invoker = new ScriptInvoker("foo", null, mockScopeContainer, null); +// assertEquals("hi petra", invoker.invokeTarget(engine, new Object[] {"petra"})); + } + + @Override + public void setUp() { + ScriptEngineManager manager = new ScriptEngineManager(); + engine = manager.getEngineByExtension("js"); + mockScopeContainer = EasyMock.createNiceMock(ScopeContainer.class); + } + +} |