summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/ant/container.script/src/main/java/org/apache/tuscany/container')
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java81
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java52
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java94
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncInvoker.java210
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncMonitor.java31
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponent.java117
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentBuilder.java83
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentType.java62
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentTypeLoader.java71
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementation.java47
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementationLoader.java85
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstance.java31
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstanceFactory.java71
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInvoker.java53
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java118
15 files changed, 1206 insertions, 0 deletions
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java
new file mode 100644
index 0000000000..3e5d228cb5
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptImplementationLoader.java
@@ -0,0 +1,81 @@
+/*
+ * 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 static org.osoa.sca.Version.XML_NAMESPACE_1_0;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.container.script.helper.ScriptHelperImplementation;
+import org.apache.tuscany.container.script.helper.ScriptHelperImplementationLoader;
+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.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;
+
+/**
+ * Loader for handling implementation.script elements.
+ *
+ * <implementation.script script="path/foo.py" class="myclass">
+ *
+ */
+public class ScriptImplementationLoader extends ScriptHelperImplementationLoader {
+
+ 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 ScriptHelperImplementation 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);
+
+ ScriptHelperImplementation implementation = new ScriptHelperImplementation();
+ implementation.setResourceName(scriptName);
+ implementation.setScriptInstanceFactory(instanceFactory);
+
+ registry.loadComponentType(parent, implementation, deploymentContext);
+
+ return implementation;
+ }
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java
new file mode 100644
index 0000000000..9910661f85
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstance.java
@@ -0,0 +1,52 @@
+/*
+ * 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;
+import org.apache.tuscany.container.script.helper.ScriptHelperInstance;
+
+/**
+ * An invokable instance of a script
+ *
+ * Basically just a wrapper around a BSF engine with an optional script class object.
+ */
+public class ScriptInstance implements ScriptHelperInstance {
+
+ protected BSFEngine bsfEngine;
+
+ protected Object clazz;
+
+ public ScriptInstance(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/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java
new file mode 100644
index 0000000000..7206eb9e8f
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/ScriptInstanceFactory.java
@@ -0,0 +1,94 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.bsf.BSFEngine;
+import org.apache.bsf.BSFException;
+import org.apache.bsf.BSFManager;
+import org.apache.tuscany.container.script.helper.ScriptHelperInstanceFactory;
+import org.apache.tuscany.spi.ObjectCreationException;
+
+/**
+ * ScriptInstanceFactory creates ScriptInstances for a script
+ */
+public class ScriptInstanceFactory extends ScriptHelperInstanceFactory<ScriptInstance>{
+
+ private String className;
+
+ private String scriptSource;
+
+ public ScriptInstanceFactory(String resourceName, String className, String scriptSource, ClassLoader classLoader) {
+ super(resourceName, classLoader);
+ this.className = className;
+ this.scriptSource = scriptSource;
+ }
+
+ /**
+ * Create a new invokeable instance of the script
+ *
+ * @param context
+ * objects to add to scope of the script instance
+ * @return a RhinoScriptInstance
+ */
+ public ScriptInstance createInstance(List<Class> services, Map<String, Object> context) {
+ 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));
+ }
+
+ 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 ScriptInstance(bsfEngine, clazz);
+
+ } catch (BSFException e) {
+ if (e.getTargetException() != null) {
+ throw new ObjectCreationException(e.getTargetException());
+ }
+ throw new ObjectCreationException(e.getTargetException());
+ }
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncInvoker.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncInvoker.java
new file mode 100644
index 0000000000..d5cfe581ce
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncInvoker.java
@@ -0,0 +1,210 @@
+/*
+ * 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.helper;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.tuscany.spi.component.TargetException;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
+import org.apache.tuscany.spi.wire.InboundWire;
+import org.apache.tuscany.spi.wire.InvocationRuntimeException;
+import org.apache.tuscany.spi.wire.Message;
+import org.apache.tuscany.spi.wire.TargetInvoker;
+import org.osoa.sca.CompositeContext;
+import org.osoa.sca.CurrentCompositeContext;
+import org.osoa.sca.SCA;
+import org.osoa.sca.ServiceRuntimeException;
+
+/**
+ * Responsible for performing a non-blocking dispatch on a component implementation instance
+ *
+ * TODO: Pretty much a direct copy of what the groovy container does for async
+ */
+public class AsyncInvoker extends ScriptHelperInvoker {
+
+ private static final ContextBinder BINDER = new ContextBinder();
+ private static final Message RESPONSE = new AsyncInvoker.ImmutableMessage();
+
+ private InboundWire wire;
+ private WorkScheduler workScheduler;
+ private AsyncMonitor monitor;
+ private WorkContext workContext;
+ private Object target;
+ private Object messageId;
+
+ /**
+ * Creates a new invoker
+ *
+ * @param operation the operation the invoker is associated with
+ * @param wire
+ * @param component the target component
+ * @param workScheduler the work scheduler to run the invocation
+ * @param monitor the monitor to pass events to
+ * @param workContext
+ */
+ public AsyncInvoker(String operation,
+ InboundWire wire,
+ ScriptHelperComponent component,
+ WorkScheduler workScheduler,
+ AsyncMonitor monitor,
+ WorkContext workContext) {
+ super(operation, component);
+ this.wire = wire;
+ this.workScheduler = workScheduler;
+ this.monitor = monitor;
+ this.workContext = workContext;
+ }
+
+ // Override invocation methods to defer invocation to work item
+ // Both methods return null to indicate asynchrony; result will
+ // be conveyed by callback
+ @Override
+ public Object invokeTarget(final Object payload) throws InvocationTargetException {
+ final CompositeContext currentContext = CurrentCompositeContext.getContext();
+ // Schedule the invocation of the next interceptor in a new Work instance
+ try {
+ workScheduler.scheduleWork(new Runnable() {
+ private Object currentMessageId = messageId;
+
+ public void run() {
+ workContext.setCurrentMessageId(null);
+ workContext.setCurrentCorrelationId(currentMessageId);
+ CompositeContext oldContext = CurrentCompositeContext.getContext();
+ try {
+ BINDER.setContext(currentContext);
+ // REVIEW response must be null for one-way and non-null for callback
+ AsyncInvoker.super.invokeTarget(payload);
+ } catch (Exception e) {
+ // REVIEW uncomment when it is available
+ // monitor.executionError(e);
+ e.printStackTrace();
+ } finally {
+ BINDER.setContext(oldContext);
+ }
+ }
+ });
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
+ return RESPONSE;
+ }
+
+ public Message invoke(Message msg) throws InvocationRuntimeException {
+ // can't just call overriden invoke because it would bypass async
+ try {
+ messageId = msg.getMessageId();
+ wire.addMapping(messageId, msg.getFromAddress());
+ return (Message) invokeTarget(msg.getBody());
+ } catch (Throwable e) {
+ // FIXME need to log exceptions
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public AsyncInvoker clone() {
+ AsyncInvoker invoker = (AsyncInvoker) super.clone();
+ invoker.workScheduler = this.workScheduler;
+ invoker.monitor = this.monitor;
+ return invoker;
+ }
+
+ /**
+ * Resolves the target service instance or returns a cached one
+ */
+ protected Object getInstance() throws TargetException {
+ if (!isCacheable()) {
+ return component.getTargetInstance();
+ } else {
+ if (target == null) {
+ target = component.getTargetInstance();
+ }
+ return target;
+ }
+ }
+
+ protected static class ContextBinder extends SCA {
+ public void setContext(CompositeContext context) {
+ setCompositeContext(context);
+ }
+
+ public void start() {
+ throw new AssertionError();
+ }
+
+ public void stop() {
+ throw new AssertionError();
+ }
+ }
+
+ /**
+ * A dummy message passed back on an invocation
+ */
+ protected static class ImmutableMessage implements Message {
+
+ public Object getBody() {
+ return null;
+ }
+
+ public void setBody(Object body) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setTargetInvoker(TargetInvoker invoker) {
+ throw new UnsupportedOperationException();
+ }
+
+ public TargetInvoker getTargetInvoker() {
+ return null;
+ }
+
+ public Object getFromAddress() {
+ return null;
+ }
+
+ public void setFromAddress(Object fromAddress) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object getMessageId() {
+ return null;
+ }
+
+ public void setMessageId(Object messageId) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object getCorrelationId() {
+ return null;
+ }
+
+ public void setCorrelationId(Object correlationId) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isFault() {
+ return false;
+ }
+
+ public void setBodyWithFault(Object fault) {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncMonitor.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncMonitor.java
new file mode 100644
index 0000000000..a3aa9958c7
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/AsyncMonitor.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.helper;
+
+/**
+ * A monitor used to log events during non-blocking invocations
+ */
+public interface AsyncMonitor {
+
+ /**
+ * Logs an exception thrown during an invocation
+ */
+ void executionError(Exception e);
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponent.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponent.java
new file mode 100644
index 0000000000..03f5da96ca
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponent.java
@@ -0,0 +1,117 @@
+/*
+ * 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.helper;
+
+import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod;
+
+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.component.CompositeComponent;
+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.AtomicComponentExtension;
+import org.apache.tuscany.spi.model.Operation;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
+import org.apache.tuscany.spi.wire.InboundWire;
+import org.apache.tuscany.spi.wire.OutboundWire;
+import org.apache.tuscany.spi.wire.TargetInvoker;
+import org.apache.tuscany.spi.wire.WireService;
+
+/**
+ * A component implementation for script languages.
+ */
+public class ScriptHelperComponent extends AtomicComponentExtension {
+
+ private final List<Class<?>> services;
+
+ private final Map<String, Object> properties;
+
+ protected ScriptHelperInstanceFactory instanceFactory;
+
+ public ScriptHelperComponent(String name, ScriptHelperInstanceFactory instanceFactory, Map<String, Object> properties, List<Class<?>> services, CompositeComponent parent, ScopeContainer scopeContainer,
+ WireService wireService, WorkContext workContext, WorkScheduler workScheduler) {
+
+ super(name, parent, scopeContainer, wireService, workContext, workScheduler, 0);
+
+ this.instanceFactory = instanceFactory;
+ this.services = services;
+ this.scope = scopeContainer.getScope();
+ this.properties = properties;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Object createInstance() throws ObjectCreationException {
+
+ Map<String, Object> context = new HashMap<String, Object>(getProperties());
+
+ for (List<OutboundWire> referenceWires : getOutboundWires().values()) {
+ for (OutboundWire wire : referenceWires) {
+ Object wireProxy = wireService.createProxy(wire);
+ context.put(wire.getReferenceName(), wireProxy);
+ }
+ }
+
+ return instanceFactory.createInstance(services, context);
+ }
+
+ public TargetInvoker createTargetInvoker(String targetName, Operation operation) {
+ Method[] methods = operation.getServiceContract().getInterfaceClass().getMethods();
+ Method method = findMethod(operation, methods);
+ return new ScriptHelperInvoker(method.getName(), this);
+ }
+
+ public TargetInvoker createAsyncTargetInvoker(InboundWire wire, Operation operation) {
+ return new AsyncInvoker(operation.getName(), wire, this, workScheduler, null, workContext);
+ }
+
+ // TODO: move all the following up to AtomicComponentExtension?
+
+ public List<Class<?>> getServiceInterfaces() {
+ return services;
+ }
+
+ public Map<String, Object> getProperties() {
+ return properties;
+ }
+
+ public Object getTargetInstance() throws TargetException {
+ return scopeContainer.getInstance(this);
+ }
+
+ public Object getServiceInstance() throws TargetException {
+ return getServiceInstance(null);
+ }
+
+ @SuppressWarnings("unchecked")
+ public Object getServiceInstance(String service) throws TargetException {
+ InboundWire wire = getInboundWire(service);
+ if (wire == null) {
+ TargetException e = new TargetException("ServiceDefinition not found"); // TODO better error message
+ e.setIdentifier(service);
+ throw e;
+ }
+ return wireService.createProxy(wire);
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentBuilder.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentBuilder.java
new file mode 100644
index 0000000000..865827f83f
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentBuilder.java
@@ -0,0 +1,83 @@
+/*
+ * 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.helper;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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
+ */
+public class ScriptHelperComponentBuilder extends ComponentBuilderExtension<ScriptHelperImplementation> {
+
+ public ScriptHelperComponentBuilder() {
+ }
+
+ protected Class<ScriptHelperImplementation> getImplementationType() {
+ return ScriptHelperImplementation.class;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Component build(CompositeComponent parent, ComponentDefinition<ScriptHelperImplementation> componentDefinition,
+ DeploymentContext deploymentContext) throws BuilderConfigException {
+
+ String name = componentDefinition.getName();
+ ScriptHelperImplementation implementation = componentDefinition.getImplementation();
+ ScriptHelperComponentType componentType = implementation.getComponentType();
+
+ // get list of services provided by this component
+ Collection<ServiceDefinition> collection = componentType.getServices().values();
+ List<Class<?>> services = new ArrayList<Class<?>>(collection.size());
+ for (ServiceDefinition serviceDefinition : collection) {
+ services.add(serviceDefinition.getServiceContract().getInterfaceClass());
+ }
+
+ // get the properties for the component
+ Map<String, Object> properties = new HashMap<String, Object>();
+ for (PropertyValue propertyValue : componentDefinition.getPropertyValues().values()) {
+ properties.put(propertyValue.getName(), propertyValue.getValueFactory().getInstance());
+ }
+
+ // TODO: have ComponentBuilderExtension pass ScopeContainer in on build method?
+ ScopeContainer scopeContainer;
+ Scope scope = componentType.getLifecycleScope();
+ if (Scope.MODULE == scope) {
+ scopeContainer = deploymentContext.getModuleScope();
+ } else {
+ scopeContainer = scopeRegistry.getScopeContainer(scope);
+ }
+
+ return new ScriptHelperComponent(name, implementation.getScriptInstanceFactory(), properties, services, parent, scopeContainer, wireService, workContext, workScheduler);
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentType.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentType.java
new file mode 100644
index 0000000000..b7abb9f60e
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentType.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.container.script.helper;
+
+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
+ * TODO: really need a generic componentType that supports scope and lifecycle
+ */
+public class ScriptHelperComponentType extends ComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> {
+
+ private Scope lifecycleScope = Scope.MODULE;
+
+ public ScriptHelperComponentType() {
+ }
+
+ @SuppressWarnings("unchecked")
+ public ScriptHelperComponentType(ComponentType ct) {
+ // TODO: A bit hacky but this is so the generic .componentType XML side file can be used for now
+ setInitLevel(ct.getInitLevel());
+ for (Object property : ct.getProperties().values()) {
+ add((Property) property);
+ }
+ for (Object reference : ct.getReferences().values()) {
+ add((ReferenceDefinition) reference);
+ }
+ for (Object service : ct.getServices().values()) {
+ add((ServiceDefinition) service);
+ }
+ }
+
+ public Scope getLifecycleScope() {
+ return lifecycleScope;
+ }
+
+ public void setLifecycleScope(Scope lifecycleScope) {
+ this.lifecycleScope = lifecycleScope;
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentTypeLoader.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentTypeLoader.java
new file mode 100644
index 0000000000..2c7b1dd4e2
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentTypeLoader.java
@@ -0,0 +1,71 @@
+/*
+ * 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.helper;
+
+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 ScriptHelperComponentTypeLoader extends ComponentTypeLoaderExtension<ScriptHelperImplementation> {
+
+ public ScriptHelperComponentTypeLoader() {
+ }
+
+ @Override
+ protected Class<ScriptHelperImplementation> getImplementationClass() {
+ return ScriptHelperImplementation.class;
+ }
+
+ // TODO: must be possible to move all the following up in to ComponentTypeLoaderExtension
+
+ public void load(CompositeComponent parent, ScriptHelperImplementation implementation, DeploymentContext deploymentContext) throws LoaderException {
+ String sideFile = getSideFileName(implementation.getResourceName());
+ URL resource = implementation.getScriptInstanceFactory().getClassLoader().getResource(sideFile);
+ ScriptHelperComponentType componentType;
+ if (resource == null) {
+ throw new IllegalArgumentException("missing .componentType side file: " + sideFile);
+ // TODO: or else implement introspection
+ } else {
+ componentType = loadFromSidefile(resource, deploymentContext);
+ }
+ implementation.setComponentType(componentType);
+ }
+
+ protected ScriptHelperComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) throws LoaderException {
+ ComponentType ct = loaderRegistry.load(null, null, url, ComponentType.class, deploymentContext);
+ ScriptHelperComponentType scriptComponentType = new ScriptHelperComponentType(ct);
+ return scriptComponentType;
+ }
+
+ protected String getSideFileName(String resourceName) {
+ int lastDot = resourceName.lastIndexOf('.');
+ if (lastDot != -1) {
+ resourceName = resourceName.substring(0, lastDot);
+ }
+ return resourceName + ".componentType";
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementation.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementation.java
new file mode 100644
index 0000000000..39b867c069
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementation.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.helper;
+
+import org.apache.tuscany.spi.model.AtomicImplementation;
+
+/**
+ * Model object for a script implementation.
+ */
+public class ScriptHelperImplementation extends AtomicImplementation<ScriptHelperComponentType> {
+
+ private String resourceName;
+
+ private ScriptHelperInstanceFactory scriptInstanceFactory;
+
+ public String getResourceName() {
+ return resourceName;
+ }
+
+ public void setResourceName(String resourceName) {
+ this.resourceName = resourceName;
+ }
+
+ public ScriptHelperInstanceFactory getScriptInstanceFactory() {
+ return scriptInstanceFactory;
+ }
+
+ public void setScriptInstanceFactory(ScriptHelperInstanceFactory scriptInstanceFactory) {
+ this.scriptInstanceFactory = scriptInstanceFactory;
+ }
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementationLoader.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementationLoader.java
new file mode 100644
index 0000000000..cda196590c
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperImplementationLoader.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.container.script.helper;
+
+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 org.apache.tuscany.spi.annotation.Autowire;
+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.MissingResourceException;
+import org.osoa.sca.annotations.Constructor;
+
+/**
+ * Loader for handling implementation.script elements.
+ *
+ * <implementation.script script="path/foo.py" class="myclass">
+ *
+ */
+public abstract class ScriptHelperImplementationLoader extends LoaderExtension<ScriptHelperImplementation> {
+
+ @Constructor( { "registry" })
+ public ScriptHelperImplementationLoader(@Autowire LoaderRegistry registry) {
+ super(registry);
+ }
+
+ public abstract QName getXMLType();
+
+ 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) {
+ MissingResourceException mre = new MissingResourceException(resource, e);
+ mre.setIdentifier(resource);
+ throw mre;
+ }
+ 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) {
+ LoaderException le = new LoaderException(e);
+ le.setIdentifier(resource);
+ throw le;
+ } finally {
+ try {
+ is.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstance.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstance.java
new file mode 100644
index 0000000000..dda5c6e27f
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstance.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.helper;
+
+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 ScriptHelperInstance {
+
+ public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException;
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstanceFactory.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstanceFactory.java
new file mode 100644
index 0000000000..b3b13fc1a7
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInstanceFactory.java
@@ -0,0 +1,71 @@
+/*
+ * 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.helper;
+
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * ScriptInstanceFactory creates ScriptInstances for a script
+ */
+public abstract class ScriptHelperInstanceFactory<T extends ScriptHelperInstance> {
+
+ protected String resourceName;
+
+ protected ClassLoader classLoader;
+
+ public ScriptHelperInstanceFactory(String resourceName, ClassLoader classLoader) {
+ this.resourceName = resourceName;
+ this.classLoader = classLoader;
+ }
+
+ /**
+ * Create a new invokeable instance of the script
+ * @param services
+ *
+ * @param context
+ * objects to add to scope of the script instance
+ * @return a RhinoScriptInstance
+ * TODO: services should be on the constructor not on this method
+ */
+ public abstract T createInstance(List<Class> services, Map<String, Object> context);
+
+ public String getResourceName() {
+ return resourceName;
+ }
+
+ public ClassLoader getClassLoader() {
+ return classLoader;
+ }
+
+ protected Map<String, Class> getResponseClasses(List<Class> services) {
+ Map<String, Class> responseClasses = new HashMap<String, Class>();
+ if (services != null) {
+ for (Class s : services) {
+ for (Method m : s.getMethods()) {
+ responseClasses.put(m.getName(), m.getReturnType());
+ }
+ }
+ }
+ return responseClasses;
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInvoker.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInvoker.java
new file mode 100644
index 0000000000..2a8ffe855a
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/helper/ScriptHelperInvoker.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.container.script.helper;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.tuscany.spi.extension.TargetInvokerExtension;
+
+/**
+ * TargetInvoker that calls a function on a ScriptInstance
+ */
+public class ScriptHelperInvoker extends TargetInvokerExtension {
+
+ protected ScriptHelperComponent component;
+
+ protected String functionName;
+
+ public ScriptHelperInvoker(String functionName, ScriptHelperComponent component) {
+ this.functionName = functionName;
+ this.component = component;
+ }
+
+ /**
+ * Invoke the function
+ */
+ public Object invokeTarget(final Object payload) throws InvocationTargetException {
+ ScriptHelperInstance target = (ScriptHelperInstance) component.getTargetInstance();
+ try {
+
+ return target.invokeTarget(functionName, (Object[]) payload);
+
+ } catch (Exception e) {
+ throw new InvocationTargetException(e);
+ }
+ }
+
+}
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
new file mode 100644
index 0000000000..fab9813af4
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.script.jsr223;
+
+import java.util.Iterator;
+import java.util.Vector;
+
+import javax.script.Invocable;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.bsf.BSFDeclaredBean;
+import org.apache.bsf.BSFException;
+import org.apache.bsf.BSFManager;
+import org.apache.bsf.util.BSFEngineImpl;
+
+/**
+ * JSR223BSFEngine enables using any available JSR-223 script engine with BSF
+ *
+ * Use the static method JSR223BSFEngine.registerAllJSR223Engines to register all
+ * the available JSR-223 engines with BSF.
+ */
+public class JSR223BSFEngine extends BSFEngineImpl {
+
+ private ScriptEngine engine;
+
+ /*
+ * @see org.apache.bsf.BSFEngine.call(Object, String, Object[])
+ */
+ public Object call(Object object, String name, Object[] args) throws BSFException {
+ if (!(engine instanceof Invocable)) {
+ throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error, call not supported by engine: "
+ + engine.getContext().getAttribute(ScriptEngine.ENGINE));
+ }
+ try {
+
+ return ((Invocable) engine).call(name, object, args);
+
+ } catch (Exception e) {
+ throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error: " + e.getLocalizedMessage(), e);
+ }
+ }
+
+ /*
+ * @see org.apache.bsf.BSFEngine.call(Object, String, Object[])
+ */
+ public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException {
+ try {
+
+ return engine.eval(expr.toString());
+
+ } catch (ScriptException e) {
+ throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error: " + e.getLocalizedMessage(), e);
+ }
+ }
+
+ /*
+ * @see org.apache.bsf.BSFEngine.declareBean(BSFDeclaredBean)
+ */
+ public void declareBean(BSFDeclaredBean bean) throws BSFException {
+ engine.getContext().setAttribute(bean.name, bean.bean, ScriptContext.ENGINE_SCOPE);
+ }
+
+ /*
+ * @see org.apache.bsf.BSFEngine.undeclareBean(BSFDeclaredBean)
+ */
+ public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
+ engine.getContext().removeAttribute(bean.name, ScriptContext.ENGINE_SCOPE);
+ }
+
+ /*
+ * @see org.apache.bsf.BSFEngine.initialize(BSFManager, String, Vector)
+ */
+ public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
+
+ super.initialize(mgr, lang, declaredBeans);
+
+ this.engine = new ScriptEngineManager().getEngineByName(lang);
+
+ for (Iterator it = declaredBeans.iterator(); it.hasNext();) {
+ declareBean((BSFDeclaredBean) it.next());
+ }
+
+ }
+
+ /**
+ * Register all the available JSR-223 engines with BSF.
+ *
+ * Any BSF engines for the same language will be replaced with the JSR-223 engine.
+ */
+ public static void registerAllJSR223Engines() {
+ ScriptEngineFactory[] engineFactories = new ScriptEngineManager().getEngineFactories();
+ for (int i = 0; i < engineFactories.length; i++) {
+ String[] names = engineFactories[i].getNames();
+ String[] extensions = engineFactories[i].getExtensions();
+ for (int j = 0; j < names.length; j ++) {
+ BSFManager.registerScriptingEngine(names[j], JSR223BSFEngine.class.getName(), extensions);
+ }
+ }
+ }
+
+}