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 --- .../container/script/ComponentConfiguration.java | 127 ++++++++++++++++++++ .../container/script/MissingSideFileException.java | 32 +++++ .../tuscany/container/script/ScriptComponent.java | 73 +++++++++++ .../container/script/ScriptComponentBuilder.java | 98 +++++++++++++++ .../container/script/ScriptComponentType.java | 46 +++++++ .../script/ScriptComponentTypeLoader.java | 75 ++++++++++++ .../container/script/ScriptImplementation.java | 92 ++++++++++++++ .../script/ScriptImplementationLoader.java | 122 +++++++++++++++++++ .../tuscany/container/script/ScriptInstance.java | 31 +++++ .../container/script/ScriptInstanceFactory.java | 133 +++++++++++++++++++++ .../container/script/ScriptInstanceImpl.java | 47 ++++++++ .../container/script/ScriptTargetInvoker.java | 56 +++++++++ .../container/script/WireObjectFactory.java | 50 ++++++++ 13 files changed, 982 insertions(+) create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ComponentConfiguration.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/MissingSideFileException.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponent.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentBuilder.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentType.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentTypeLoader.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementation.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceImpl.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptTargetInvoker.java create mode 100644 sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/WireObjectFactory.java (limited to 'sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container') diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ComponentConfiguration.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ComponentConfiguration.java new file mode 100644 index 0000000000..b5888521cf --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ComponentConfiguration.java @@ -0,0 +1,127 @@ +/* + * 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.container.script; + +import java.util.List; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.WireService; +import org.apache.tuscany.spi.extension.ExecutionMonitor; + +/** + * Configuration holder for creating script components + * + * @version $Rev$ $Date$ + */ +public class ComponentConfiguration { + + private String name; + private ScriptInstanceFactory factory; + private List> services; + private CompositeComponent parent; + private ScopeContainer scopeContainer; + private WireService wireService; + private WorkContext workContext; + private WorkScheduler workScheduler; + private ExecutionMonitor monitor; + private int initLevel; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ScriptInstanceFactory getFactory() { + return factory; + } + + public void setFactory(ScriptInstanceFactory factory) { + this.factory = factory; + } + + public List> getServices() { + return services; + } + + public void setServices(List> services) { + this.services = services; + } + + public CompositeComponent getParent() { + return parent; + } + + public void setParent(CompositeComponent parent) { + this.parent = parent; + } + + public ScopeContainer getScopeContainer() { + return scopeContainer; + } + + public void setScopeContainer(ScopeContainer scopeContainer) { + this.scopeContainer = scopeContainer; + } + + public WireService getWireService() { + return wireService; + } + + public void setWireService(WireService wireService) { + this.wireService = wireService; + } + + public WorkContext getWorkContext() { + return workContext; + } + + public void setWorkContext(WorkContext workContext) { + this.workContext = workContext; + } + + public WorkScheduler getWorkScheduler() { + return workScheduler; + } + + public void setWorkScheduler(WorkScheduler workScheduler) { + this.workScheduler = workScheduler; + } + + public ExecutionMonitor getMonitor() { + return monitor; + } + + public void setMonitor(ExecutionMonitor monitor) { + this.monitor = monitor; + } + + public int getInitLevel() { + return initLevel; + } + + public void setInitLevel(int initLevel) { + this.initLevel = initLevel; + } +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/MissingSideFileException.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/MissingSideFileException.java new file mode 100644 index 0000000000..f8910e8d22 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/MissingSideFileException.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.container.script; + +import org.apache.tuscany.spi.loader.LoaderException; + +/** + * @version $Rev$ $Date$ + */ +public class MissingSideFileException extends LoaderException { + + public MissingSideFileException(String message, String identifier) { + super(message, identifier); + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponent.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponent.java new file mode 100644 index 0000000000..fc4cf09c7e --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponent.java @@ -0,0 +1,73 @@ +/* + * 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.container.script; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.extension.AtomicComponentExtension; +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * A component implementation for script languages. + * + * @version $Rev$ $Date$ + */ +public class ScriptComponent extends AtomicComponentExtension { + private ScriptInstanceFactory factory; + + public ScriptComponent(ComponentConfiguration config) { + super(config.getName(), + config.getParent(), + config.getWireService(), + config.getWorkContext(), + config.getWorkScheduler(), + config.getMonitor(), + config.getInitLevel()); + this.factory = config.getFactory(); + this.scope = config.getScopeContainer().getScope(); + } + + @SuppressWarnings("unchecked") + public Object createInstance() throws ObjectCreationException { + return factory.getInstance(); //(serviceBindings, context); + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation, InboundWire callbackWire) { + Method[] methods = operation.getServiceContract().getInterfaceClass().getMethods(); + Method method = findMethod(operation, methods); + return new ScriptTargetInvoker(method.getName(), this); + } + + @SuppressWarnings({"unchecked"}) + protected void onReferenceWire(OutboundWire wire) { + Class clazz = wire.getServiceContract().getInterfaceClass(); + factory.addContextObjectFactory(wire.getReferenceName(), new WireObjectFactory(clazz, wire, wireService)); + } + + public Object getTargetInstance() throws TargetResolutionException { + return scopeContainer.getInstance(this); + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentBuilder.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentBuilder.java new file mode 100644 index 0000000000..8916a51bcd --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentBuilder.java @@ -0,0 +1,98 @@ +/* + * 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.container.script; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +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.PropertyValue; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +/** + * Extension point for creating {@link ScriptComponent}s from an assembly configuration + * + * @version $Rev$ $Date$ + */ +public class ScriptComponentBuilder extends ComponentBuilderExtension { + + public ScriptComponentBuilder() { + } + + protected Class getImplementationType() { + return ScriptImplementation.class; + } + + @SuppressWarnings("unchecked") + public Component build(CompositeComponent parent, ComponentDefinition componentDefinition, + DeploymentContext deploymentContext) throws BuilderConfigException { + + String name = componentDefinition.getName(); + ScriptImplementation implementation = componentDefinition.getImplementation(); + ScriptComponentType componentType = implementation.getComponentType(); + + // get list of serviceBindings provided by this component + Collection collection = componentType.getServices().values(); + List> services = new ArrayList>(collection.size()); + for (ServiceDefinition serviceDefinition : collection) { + services.add(serviceDefinition.getServiceContract().getInterfaceClass()); + } + + // TODO: have ComponentBuilderExtension pass ScopeContainer in on build method? + ScopeContainer scopeContainer; + Scope scope = componentType.getLifecycleScope(); + if (Scope.COMPOSITE == scope) { + scopeContainer = deploymentContext.getCompositeScope(); + } else { + scopeContainer = scopeRegistry.getScopeContainer(scope); + } + String className = implementation.getClassName(); + String scriptSource = implementation.getScriptSource(); + String scriptName = implementation.getScriptName(); + ClassLoader cl = implementation.getClassLoader(); + ScriptInstanceFactory instanceFactory = + new ScriptInstanceFactory(scriptName, className, scriptSource, cl); + + // get the properties for the component + for (PropertyValue propertyValue : componentDefinition.getPropertyValues().values()) { + //TODO this is not safe for since multiple instances can share mutable properties + instanceFactory.addContextObjectFactory(propertyValue.getName(), propertyValue.getValueFactory()); + } + + ComponentConfiguration config = new ComponentConfiguration(); + config.setName(name); + config.setFactory(instanceFactory); + config.setServices(services); + config.setParent(parent); + config.setScopeContainer(scopeContainer); + config.setWireService(wireService); + config.setWorkContext(workContext); + config.setWorkScheduler(workScheduler); + return new ScriptComponent(config); + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentType.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentType.java new file mode 100644 index 0000000000..e467eef077 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentType.java @@ -0,0 +1,46 @@ +/* + * 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.container.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> { + + private Scope lifecycleScope = Scope.COMPOSITE; + + public ScriptComponentType() { + } + + public Scope getLifecycleScope() { + return lifecycleScope; + } + + public void setLifecycleScope(Scope lifecycleScope) { + this.lifecycleScope = lifecycleScope; + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentTypeLoader.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentTypeLoader.java new file mode 100644 index 0000000000..871f74b705 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptComponentTypeLoader.java @@ -0,0 +1,75 @@ +/* + * 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.container.script; + +import java.net.URL; + +import org.apache.tuscany.spi.component.CompositeComponent; +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(CompositeComponent parent, + ScriptImplementation implementation, + DeploymentContext deploymentContext) throws LoaderException { + String sideFile = getSideFileName(implementation.getResourceName()); + URL resource = implementation.getClassLoader().getResource(sideFile); + ScriptComponentType componentType; + if (resource == null) { + throw new MissingSideFileException("Component type side file not found", sideFile); + // TODO: or else implement introspection + } else { + componentType = loadFromSidefile(parent, resource, deploymentContext); + } + implementation.setComponentType(componentType); + } + + @SuppressWarnings("unchecked") + protected ScriptComponentType loadFromSidefile(CompositeComponent parent, + URL url, + DeploymentContext deploymentContext) + throws LoaderException { + ScriptComponentType scriptComponentType = new ScriptComponentType(); + return (ScriptComponentType) loaderRegistry + .load(parent, scriptComponentType, url, ComponentType.class, deploymentContext); + } + + 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-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementation.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementation.java new file mode 100644 index 0000000000..da02c67cf9 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementation.java @@ -0,0 +1,92 @@ +/* + * 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.container.script; + +import org.apache.tuscany.spi.model.AtomicImplementation; + +//import org.apache.tuscany.container.script.helper.ScriptFactory; + +/** + * Model object for a script implementation. + */ +public class ScriptImplementation extends AtomicImplementation { + + private String resourceName; + private String className; + private String scriptSource; + private String scriptName; + private ClassLoader classLoader; + +// private ScriptFactory scriptFactory; + + public String getResourceName() { + return resourceName; + } + + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getScriptSource() { + return scriptSource; + } + + public void setScriptSource(String scriptSource) { + this.scriptSource = scriptSource; + } + +// public ScriptFactory getScriptFactory() { +// return scriptFactory; +// } +// +// public void setScriptFactory(ScriptFactory scriptFactory) { +// this.scriptFactory = scriptFactory; +// } + + public String getScriptName() { + return scriptName; + } + + public void setScriptName(String scriptName) { + this.scriptName = scriptName; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } +// public ScriptFactory getScriptInstanceFactory() { +// return scriptFactory; +// } +// +// public void setScriptInstanceFactory(ScriptFactory scriptFactory) { +// this.scriptFactory = scriptFactory; +// } +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java new file mode 100644 index 0000000000..7664ae4227 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java @@ -0,0 +1,122 @@ +/* + * 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.container.script; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import static org.osoa.sca.Version.XML_NAMESPACE_1_0; +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +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; + +/** + * Loader for handling implementation.script elements. + *

+ * + * + * @version $Rev$ $Date$ + */ +public class ScriptImplementationLoader extends LoaderExtension { + + private static final QName IMPLEMENTATION_SCRIPT = new QName(XML_NAMESPACE_1_0, "implementation.script"); + + @Constructor({"registry"}) + public ScriptImplementationLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPLEMENTATION_SCRIPT; + } + + public ScriptImplementation load(CompositeComponent parent, ModelObject mo, XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + String scriptName = reader.getAttributeValue(null, "script"); + if (scriptName == null) { + throw new MissingResourceException("implementation element has no 'script' attribute"); + } + + String className = reader.getAttributeValue(null, "class"); + + LoaderUtil.skipToEndElement(reader); + + ClassLoader cl = deploymentContext.getClassLoader(); + String scriptSource = loadSource(cl, scriptName); + +// ScriptInstanceFactory instanceFactory = new ScriptInstanceFactory(scriptName, className, scriptSource, cl); + + ScriptImplementation implementation = new ScriptImplementation(); + implementation.setResourceName(scriptName); + implementation.setScriptSource(scriptSource); + implementation.setClassName(className); + implementation.setScriptName(scriptName); + implementation.setClassLoader(cl); + //implementation.setScriptInstanceFactory(instanceFactory); + + registry.loadComponentType(parent, implementation, deploymentContext); + + return implementation; + } + + protected String loadSource(ClassLoader cl, String resource) throws LoaderException { + URL url = cl.getResource(resource); + if (url == null) { + throw new MissingResourceException(resource); + } + InputStream is; + try { + is = url.openStream(); + } catch (IOException e) { + throw new MissingResourceException(resource, e); + } + try { + Reader reader = new InputStreamReader(is, "UTF-8"); + char[] buffer = new char[1024]; + StringBuilder source = new StringBuilder(); + int count; + while ((count = reader.read(buffer)) > 0) { + source.append(buffer, 0, count); + } + return source.toString(); + } catch (IOException e) { + throw new LoaderException(resource, e); + } finally { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + } +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java new file mode 100644 index 0000000000..645ddb5750 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java @@ -0,0 +1,31 @@ +/* + * 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.container.script; + +import java.lang.reflect.InvocationTargetException; + +/** + * An invokable instance of a script + * + * Basically just a wrapper around a BSF engine with an optional script class object. + */ +public interface ScriptInstance { + + Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException; +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java new file mode 100644 index 0000000000..f371e7c92f --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java @@ -0,0 +1,133 @@ +/* + * 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.container.script; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; + +import org.apache.bsf.BSFEngine; +import org.apache.bsf.BSFException; +import org.apache.bsf.BSFManager; + +/** + * ScriptFactory creates ScriptInstances for a script + */ +public class ScriptInstanceFactory implements ObjectFactory { + protected String resourceName; + protected ClassLoader classLoader; + private String className; + private String scriptSource; + private Map contextObjects; + + public ScriptInstanceFactory(String resourceName, + String className, + String scriptSource, + ClassLoader classLoader) { + this.resourceName = resourceName; + this.classLoader = classLoader; + this.className = className; + this.scriptSource = scriptSource; + this.contextObjects = new HashMap(); + } + + /** + * Create a new invokeable instance of the script + *

+ * objects to add to scope of the script instance + * + * @return a RhinoScriptInstance + */ + //public ScriptInstanceImpl createInstance(List> serviceBindings, Map context) { + public ScriptInstance getInstance() throws ObjectCreationException { + try { + + //TODO: this uses a new manager and recompiles the scrip each time, may be able to optimize + // but need to be careful about instance scoping + + BSFManager bsfManager = new BSFManager(); + bsfManager.setClassLoader(BSFManager.class.getClassLoader()); + + // TODO: hack to get Ruby working with the standalone launcher + Thread.currentThread().setContextClassLoader(BSFManager.class.getClassLoader()); + +// // register any context objects (SCA properties and references) +// for (String beanName : context.keySet()) { +// bsfManager.registerBean(beanName, context.get(beanName)); +// } + + // register any context objects (SCA properties and references) + for (Map.Entry entry : contextObjects.entrySet()) { + bsfManager.registerBean(entry.getKey(), entry.getValue().getInstance()); + } + + String scriptLanguage = BSFManager.getLangFromFilename(resourceName); + BSFEngine bsfEngine = bsfManager.loadScriptingEngine(scriptLanguage); + bsfEngine.exec(resourceName, 0, 0, scriptSource); + + // if there's a className then get the class object + Object clazz = null; + if (className != null) { + // special case for Ruby which requires a .new call + if ("ruby".equals(scriptLanguage)) { + clazz = bsfEngine.eval(null, 1, 1, className + ".new"); + } else { + clazz = bsfEngine.call(null, className, null); + } + } + + return new ScriptInstanceImpl(bsfEngine, clazz); + + } catch (BSFException e) { + if (e.getTargetException() != null) { + throw new ObjectCreationException(e.getTargetException()); + } + throw new ObjectCreationException(e.getTargetException()); + } + } + + public String getResourceName() { + return resourceName; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + protected Map getResponseClasses(List services) { + Map responseClasses = new HashMap(); + if (services != null) { + for (Class s : services) { + for (Method m : s.getMethods()) { + responseClasses.put(m.getName(), m.getReturnType()); + } + } + } + return responseClasses; + } + + public void addContextObjectFactory(String name, ObjectFactory factory) { + contextObjects.put(name, factory); + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceImpl.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceImpl.java new file mode 100644 index 0000000000..76c1b9d814 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptInstanceImpl.java @@ -0,0 +1,47 @@ +/* + * 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.container.script; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.bsf.BSFEngine; + +/** + * An invokable instance of a script + * + * Basically just a wrapper around a BSF engine with an optional script class object. + */ +public class ScriptInstanceImpl implements ScriptInstance { + protected BSFEngine bsfEngine; + protected Object clazz; + + public ScriptInstanceImpl(BSFEngine bsfEngine, Object clazz) { + this.bsfEngine = bsfEngine; + this.clazz = clazz; + } + + public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException { + try { + return bsfEngine.call(clazz, operationName, args); + } catch (Exception e) { + throw new InvocationTargetException(e); + } + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptTargetInvoker.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptTargetInvoker.java new file mode 100644 index 0000000000..21b84bb83b --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/ScriptTargetInvoker.java @@ -0,0 +1,56 @@ +/* + * 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.container.script; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.extension.TargetInvokerExtension; + +/** + * TargetInvoker implementation that calls a function on a ScriptInstanceImpl + * + * @version $Rev$ $Dev$ + */ +public class ScriptTargetInvoker extends TargetInvokerExtension { + + protected ScriptComponent component; + protected String functionName; + + public ScriptTargetInvoker(String functionName, ScriptComponent component) { + super(null, null, null); + this.functionName = functionName; + this.component = component; + } + + public Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException { + ScriptInstance target; + try { + target = (ScriptInstance) component.getTargetInstance(); + } catch (TargetException e) { + throw new InvocationTargetException(e); + } + try { + return target.invokeTarget(functionName, (Object[]) payload); + } catch (Exception e) { + throw new InvocationTargetException(e); + } + } + +} diff --git a/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/WireObjectFactory.java b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/WireObjectFactory.java new file mode 100644 index 0000000000..66ba432181 --- /dev/null +++ b/sandbox/old/contrib/implementation-bsf/src/main/java/org/apache/tuscany/container/script/WireObjectFactory.java @@ -0,0 +1,50 @@ +/* + * 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.container.script; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.WireService; + +/** + * @version $Rev$ $Date$ + */ +public class WireObjectFactory implements ObjectFactory { + private Class interfaze; + private OutboundWire wire; + private WireService wireService; + + /** + * Constructor. + * + * @param interfaze the interface to inject on the client + * @param wire the backing wire + * @param wireService the wire service to create the proxy + */ + public WireObjectFactory(Class interfaze, OutboundWire wire, WireService wireService) { + this.interfaze = interfaze; + this.wire = wire; + this.wireService = wireService; + } + + public T getInstance() throws ObjectCreationException { + return interfaze.cast(wireService.createProxy(interfaze, wire)); + } +} -- cgit v1.2.3