summaryrefslogtreecommitdiffstats
path: root/sandbox/old/contrib/implementation-script/container.jsr223/src
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/old/contrib/implementation-script/container.jsr223/src')
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/InstanceWrapperBase.java53
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java187
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentBuilder.java60
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentType.java37
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentTypeLoader.java68
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementation.java62
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementationLoader.java97
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptInvoker.java130
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireObjectFactory.java85
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireUtils.java114
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl40
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptImplementationLoaderTestCase.java84
-rw-r--r--sandbox/old/contrib/implementation-script/container.jsr223/src/test/java/org/apache/tuscany/extension/script/ScriptInvokerTestCase.java51
13 files changed, 1068 insertions, 0 deletions
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);
+ }
+
+}