From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../extension/script/InstanceWrapperBase.java | 53 ++++++ .../tuscany/extension/script/ScriptComponent.java | 187 +++++++++++++++++++++ .../extension/script/ScriptComponentBuilder.java | 60 +++++++ .../extension/script/ScriptComponentType.java | 37 ++++ .../script/ScriptComponentTypeLoader.java | 68 ++++++++ .../extension/script/ScriptImplementation.java | 62 +++++++ .../script/ScriptImplementationLoader.java | 97 +++++++++++ .../tuscany/extension/script/ScriptInvoker.java | 130 ++++++++++++++ .../extension/script/WireObjectFactory.java | 85 ++++++++++ .../apache/tuscany/extension/script/WireUtils.java | 114 +++++++++++++ 10 files changed, 893 insertions(+) create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/InstanceWrapperBase.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentBuilder.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentType.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponentTypeLoader.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementation.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptImplementationLoader.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptInvoker.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireObjectFactory.java create mode 100644 sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/WireUtils.java (limited to 'sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension') 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 implements InstanceWrapper { + 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> wires = new HashMap>(); + + 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 wireList = wires.get(referenceName); + if (wireList == null) { + wireList = new ArrayList(); + 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 attachWires) { + assert attachWires.size() > 0; + assert attachWires.get(0).getSourceUri().getFragment() != null; + String referenceName = attachWires.get(0).getSourceUri().getFragment(); + List wireList = wires.get(referenceName); + if (wireList == null) { + wireList = new ArrayList(); + 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 getWires(String name) { + return wires.get(name); + } + + public TargetInvoker createTargetInvoker(String targetName, PhysicalOperationDefinition operation) { + throw new UnsupportedOperationException(); + } + + protected ObjectFactory createWireFactory(Class interfaze, Wire wire) { + return new WireObjectFactory(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 { + + public ScriptComponentBuilder() { + } + + @Override + protected Class 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> { + + 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 { + + public ScriptComponentTypeLoader() { + } + + @Override + protected Class 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 { + + 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.

+ * + */ +public class ScriptImplementationLoader extends LoaderExtension { + + 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 extends TargetInvokerExtension { + + protected Object clazz; + protected String operationName; + + private final AtomicComponent component; + private final ScopeContainer scopeContainer; + protected InstanceWrapper 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 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 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 implements ObjectFactory { + private Class interfaze; + private Wire wire; + private ProxyService proxyService; + // the cache of proxy interface method to operation mappings + private Map 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 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 newChains = new HashMap(mappings.size()); + for (Map.Entry 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 createInterfaceToWireMapping(Class interfaze, Wire wire) { + Map, InvocationChain> invocationChains = wire.getInvocationChains(); + + Map chains = new HashMap(invocationChains.size()); + for (Map.Entry, 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 createInterfaceToWireMapping2(Class interfaze, Wire wire) { + Map invocationChains = wire.getPhysicalInvocationChains(); + + Map chains = new HashMap(invocationChains.size()); + for (Map.Entry 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(); + } +} -- cgit v1.2.3