diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany')
3 files changed, 206 insertions, 0 deletions
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java new file mode 100644 index 0000000000..950820fbcd --- /dev/null +++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/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.jsr223;
+
+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.easy.EasyImplementation;
+import org.apache.tuscany.container.easy.EasyImplementationLoader;
+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.osoa.sca.annotations.Constructor;
+
+/**
+ * Loader for handling implementation.script elements.
+ *
+ * <implementation.script script="path/foo.py" class="myclass">
+ *
+ */
+public class ScriptImplementationLoader extends EasyImplementationLoader {
+
+ 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;
+ }
+
+ @Override
+ public EasyImplementation load(CompositeComponent parent, 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);
+
+ EasyImplementation implementation = new EasyImplementation();
+ implementation.setResourceName(scriptName);
+ implementation.setScriptInstanceFactory(instanceFactory);
+
+ registry.loadComponentType(parent, implementation, deploymentContext);
+
+ return implementation;
+ }
+}
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java new file mode 100644 index 0000000000..67a380f0d8 --- /dev/null +++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.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.jsr223;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.script.Invocable;
+
+import org.apache.tuscany.container.easy.EasyInstance;
+
+/**
+ * An invokable instance of a script
+ *
+ * Basically just a wrapper around a BSF engine with an optional script class object.
+ */
+public class ScriptInstance implements EasyInstance {
+
+ protected Invocable invocableEngine;
+
+ public ScriptInstance(Invocable invocableEngine) {
+ this.invocableEngine = invocableEngine;
+ }
+
+ public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException {
+ try {
+
+ return invocableEngine.call(operationName, args);
+
+ } catch (Exception e) {
+ throw new InvocationTargetException(e);
+ }
+ }
+
+}
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java new file mode 100644 index 0000000000..11e2728fd3 --- /dev/null +++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.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.jsr223;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.tuscany.container.easy.EasyInstanceFactory;
+import org.apache.tuscany.spi.ObjectCreationException;
+
+/**
+ * ScriptInstanceFactory creates ScriptInstances for a script
+ */
+public class ScriptInstanceFactory extends EasyInstanceFactory<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 {
+
+ ScriptEngineManager engineMgr = new ScriptEngineManager();
+ ScriptEngine engine = engineMgr.getEngineByExtension(getScriptExtension());
+ engine.eval(scriptSource);
+ return new ScriptInstance((Invocable) engine);
+
+ } catch (ScriptException e) {
+ throw new ObjectCreationException(e);
+ }
+ }
+
+ protected String getScriptExtension() {
+ int lastDot = resourceName.lastIndexOf('.');
+ if (lastDot == -1) {
+ return null;
+ }
+ return resourceName.substring(lastDot+1);
+ }
+
+}
|