You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2007/06/03 20:12:00 UTC

svn commit: r543949 - in /incubator/tuscany/java/sca/modules/implementation-script: ./ src/main/java/org/apache/tuscany/implementation/ src/main/java/org/apache/tuscany/sca/implementation/script/ src/main/resources/META-INF/services/

Author: antelder
Date: Sun Jun  3 11:11:59 2007
New Revision: 543949

URL: http://svn.apache.org/viewvc?view=rev&rev=543949
Log:
Convert script impl to use simplified extensions

Added:
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java   (with props)
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java   (with props)
    incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.spi.ImplementationActivator
Removed:
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptArtifactProcessor.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementation.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationProvider.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvoker.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptModuleActivator.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
Modified:
    incubator/tuscany/java/sca/modules/implementation-script/pom.xml

Modified: incubator/tuscany/java/sca/modules/implementation-script/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/pom.xml?view=diff&rev=543949&r1=543948&r2=543949
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/pom.xml (original)
+++ incubator/tuscany/java/sca/modules/implementation-script/pom.xml Sun Jun  3 11:11:59 2007
@@ -34,13 +34,7 @@
 
         <dependency>
             <groupId>org.apache.tuscany.sca</groupId>
-            <artifactId>tuscany-host-embedded</artifactId>
-            <version>1.0-incubating-SNAPSHOT</version>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.tuscany.sca</groupId>
-            <artifactId>tuscany-databinding</artifactId>
+            <artifactId>tuscany-extension-helper</artifactId>
             <version>1.0-incubating-SNAPSHOT</version>
         </dependency>
 

Added: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java?view=auto&rev=543949
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java Sun Jun  3 11:11:59 2007
@@ -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.sca.implementation.script;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sca.assembly.xml.Constants;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.spi.ImplementationActivator;
+import org.apache.tuscany.sca.spi.InvokerFactory;
+import org.apache.tuscany.sca.spi.utils.PropertyValueObjectFactory;
+
+public class ScriptImplementationActivator implements ImplementationActivator<ScriptImplementation> {
+
+    private static final QName IMPLEMENTATION_SCRIPT_QNAME = new QName(Constants.SCA10_NS, "implementation.script");
+
+    // TODO: seems wrong to need PropertyValueObjectFactory, could it be on Property somehow? 
+    protected PropertyValueObjectFactory propertyFactory;
+
+    public ScriptImplementationActivator(PropertyValueObjectFactory propertyFactory) {
+        this.propertyFactory = propertyFactory;
+    }
+
+    public QName getSCDLQName() {
+        return IMPLEMENTATION_SCRIPT_QNAME;
+    }
+
+    public Class<ScriptImplementation> getImplementationClass() {
+        return ScriptImplementation.class;
+    }
+
+    public InvokerFactory createInvokerFactory(RuntimeComponent runtimeComponent, ScriptImplementation implementation) {
+        return new ScriptInvokerFactory(runtimeComponent, implementation, propertyFactory);
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptImplementationActivator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java?view=auto&rev=543949
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java Sun Jun  3 11:11:59 2007
@@ -0,0 +1,123 @@
+/*
+ * 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.sca.implementation.script;
+
+import java.io.StringReader;
+
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.bsf.xml.XMLHelper;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.Property;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.factory.ObjectCreationException;
+import org.apache.tuscany.sca.factory.ObjectFactory;
+import org.apache.tuscany.sca.implementation.script.engines.TuscanyJRubyScriptEngine;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.spi.InvokerFactory;
+import org.apache.tuscany.sca.spi.utils.PropertyValueObjectFactory;
+
+public class ScriptInvokerFactory implements InvokerFactory {
+
+    protected ScriptEngine scriptEngine;
+    protected XMLHelper xmlHelper;
+
+    public ScriptInvokerFactory(RuntimeComponent rc, ScriptImplementation implementation, PropertyValueObjectFactory propertyFactory) {
+        init(rc, implementation, propertyFactory);
+    }
+    
+    public Invoker createInvoker(Operation operation) {
+        return new ScriptInvoker(scriptEngine, xmlHelper, operation);
+    }
+    
+    protected void init(RuntimeComponent rc, ScriptImplementation implementation, PropertyValueObjectFactory propertyFactory) {
+        try {
+            scriptEngine = getScriptEngineByExtension(implementation.getScriptLanguage());
+            if (scriptEngine == null) {
+                throw new ObjectCreationException("no script engine found for language: " + implementation.getScriptLanguage());
+            }
+            if (!(scriptEngine instanceof Invocable)) {
+                throw new ObjectCreationException("script engine does not support Invocable: " + scriptEngine);
+            }
+            
+            for (Reference reference : implementation.getReferences()) {
+                scriptEngine.put(reference.getName(), createReferenceProxy(reference.getName(), rc));
+            }
+
+            for (Property property : implementation.getProperties()) {
+                ObjectFactory<?> propertyValueFactory = propertyFactory.createValueFactory(property);
+                if ( propertyValueFactory != null) {
+                    scriptEngine.put(property.getName(), propertyValueFactory.getInstance());
+                }
+            }
+            
+            scriptEngine.eval(new StringReader(implementation.getScriptSrc()));
+
+        } catch (ScriptException e) {
+            throw new ObjectCreationException(e);
+        }
+
+        // set the databinding and xmlhelper for wsdl interfaces
+        for (Service service : rc.getServices()) {
+            InterfaceContract ic = service.getInterfaceContract();
+            if (ic instanceof WSDLInterfaceContract) {
+                // Set to use the Axiom data binding
+                ic.getInterface().setDefaultDataBinding(OMElement.class.getName());
+                xmlHelper = XMLHelper.getArgHelper(scriptEngine);
+            }
+        }
+    }
+
+    /**
+     * TODO: RuntimeComponent should provide a method like this
+     */
+    @SuppressWarnings("unchecked")
+    protected Object createReferenceProxy(String name, RuntimeComponent component) {
+        for (ComponentReference reference : component.getReferences()) {
+            if (reference.getName().equals(name)) {
+                Class iface = ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass();
+                return component.getService(iface, name);
+            }
+        }
+        throw new IllegalArgumentException("reference " + name + " not found on component: " + component);
+    }
+
+    /**
+     * Hack for now to work around a problem with the JRuby script engine
+     */
+    protected ScriptEngine getScriptEngineByExtension(String scriptExtn) {
+        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
+        if ("rb".equals(scriptExtn)) {
+            return new TuscanyJRubyScriptEngine();
+        } else {
+            return scriptEngineManager.getEngineByExtension(scriptExtn);
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/sca/implementation/script/ScriptInvokerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.spi.ImplementationActivator
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.spi.ImplementationActivator?view=auto&rev=543949
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.spi.ImplementationActivator (added)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/resources/META-INF/services/org.apache.tuscany.sca.spi.ImplementationActivator Sun Jun  3 11:11:59 2007
@@ -0,0 +1,18 @@
+# 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.
+# Implementation class for the ExtensionActivator
+org.apache.tuscany.sca.implementation.script.ScriptImplementationActivator



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org