tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r427641 - in /incubator/tuscany/java/sca/containers/container.javascript/src: main/java/org/apache/tuscany/container/javascript/ test/java/org/apache/tuscany/container/javascript/function/ test/resources/org/apache/tuscany/container/javascr...
Date Tue, 01 Aug 2006 16:39:55 GMT
Author: antelder
Date: Tue Aug  1 09:39:54 2006
New Revision: 427641

URL: http://svn.apache.org/viewvc?rev=427641&view=rev
Log:
First cut at JavaScript introspection
This required fixing up the ComponentType code, it now uses the ComponentType class not a
JavaScript specific ComponentType (though will need to revisit that to support scope and lifecycle)
Added an introspectable script testcase, see the testcase and IntrospectablehelloWorld.js
for how introsepction works

Added:
    incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js
Removed:
    incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentType.java
Modified:
    incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentBuilder.java
    incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentTypeLoader.java
    incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementation.java
    incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementationLoader.java
    incubator/tuscany/java/sca/containers/container.javascript/src/test/java/org/apache/tuscany/container/javascript/function/HelloWorldTestCase.java
    incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/helloworld.scdl

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentBuilder.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentBuilder.java?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentBuilder.java
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentBuilder.java
Tue Aug  1 09:39:54 2006
@@ -14,6 +14,7 @@
 import org.apache.tuscany.spi.deployer.DeploymentContext;
 import org.apache.tuscany.spi.extension.ComponentBuilderExtension;
 import org.apache.tuscany.spi.model.ComponentDefinition;
+import org.apache.tuscany.spi.model.ComponentType;
 import org.apache.tuscany.spi.model.ServiceDefinition;
 
 /**
@@ -31,7 +32,7 @@
 
         String name = componentDefinition.getName();
         JavaScriptImplementation implementation = componentDefinition.getImplementation();
-        JavaScriptComponentType componentType = implementation.getComponentType();
+        ComponentType componentType = implementation.getComponentType();
 
         // get list of services provided by this component
         Collection<ServiceDefinition> collection = componentType.getServices().values();
@@ -40,8 +41,7 @@
             services.add(serviceDefinition.getServiceContract().getInterfaceClass());
         }
 
-        String script = implementation.getScript();
-        RhinoScript rhinoScript = new RhinoScript(name, script);
+        RhinoScript rhinoScript = implementation.getRhinoScript();
 
         //TODO properties
         Map<String, Object> properties = new HashMap<String, Object>();

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentTypeLoader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentTypeLoader.java?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentTypeLoader.java
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptComponentTypeLoader.java
Tue Aug  1 09:39:54 2006
@@ -18,7 +18,9 @@
 
 import java.net.URL;
 
+import org.apache.tuscany.container.javascript.rhino.RhinoScript;
 import org.apache.tuscany.core.implementation.IntrospectionRegistry;
+import org.apache.tuscany.core.implementation.Introspector;
 import org.apache.tuscany.core.implementation.JavaServiceContract;
 import org.apache.tuscany.core.implementation.ProcessingException;
 import org.apache.tuscany.spi.annotation.Autowire;
@@ -27,15 +29,17 @@
 import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension;
 import org.apache.tuscany.spi.loader.LoaderException;
 import org.apache.tuscany.spi.loader.LoaderRegistry;
+import org.apache.tuscany.spi.model.ComponentType;
 import org.apache.tuscany.spi.model.ServiceContract;
 import org.apache.tuscany.spi.model.ServiceDefinition;
+import org.mozilla.javascript.Scriptable;
 import org.osoa.sca.annotations.Constructor;
 
 /**
  * @version $Rev: 423297 $ $Date: 2006-07-19 00:56:32 +0100 (Wed, 19 Jul 2006) $
  */
 public class JavaScriptComponentTypeLoader extends ComponentTypeLoaderExtension<JavaScriptImplementation>
{
-    // private Introspector introspector;
+    private Introspector introspector;
 
     @Constructor( { "registry", "introspector" })
     public JavaScriptComponentTypeLoader(@Autowire LoaderRegistry loaderRegistry, @Autowire
IntrospectionRegistry introspector) {
@@ -50,48 +54,50 @@
 
     public void load(CompositeComponent<?> parent, JavaScriptImplementation implementation,
DeploymentContext deploymentContext)
             throws LoaderException {
-        String scriptName = implementation.getScriptName();
+        String scriptName = implementation.getRhinoScript().getScriptName();
         URL resource = implementation.getClassLoader().getResource(getBaseName(scriptName)
+ ".componentType");
+        ComponentType componentType;
         if (resource == null) {
-            throw new RuntimeException("can't find .componentType side file for " + getBaseName(scriptName));
+            componentType = loadByIntrospection(parent, implementation, deploymentContext);
+            if (componentType == null) {
+                throw new RuntimeException("must use .componentType side file or JS SCA variable
definition");
+            }
+        } else {
+            componentType = loadFromSidefile(resource, deploymentContext);
         }
 
-        JavaScriptComponentType componentType = createDummyComponentType(implementation);
-        // if (resource == null) {
-        // componentType = loadByIntrospection(parent, implementation, deploymentContext);
-        // } else {
-        // componentType = loadFromSidefile(resource, deploymentContext);
-        // }
-
         implementation.setComponentType(componentType);
     }
 
-    @SuppressWarnings("unchecked")
-    private JavaScriptComponentType createDummyComponentType(JavaScriptImplementation implementation)
{
-        JavaScriptComponentType componentType = new JavaScriptComponentType();
-        ServiceDefinition service = new ServiceDefinition();
-        ServiceContract sc = new JavaServiceContract();
-        try {
-            sc.setInterfaceClass(Class.forName("helloworld.HelloWorldService", true, implementation.getClassLoader()));
-        } catch (ClassNotFoundException e) {
-            throw new RuntimeException(e);
+    protected ComponentType loadByIntrospection(CompositeComponent<?> parent, JavaScriptImplementation
implementation,
+            DeploymentContext deploymentContext) throws ProcessingException {
+        ComponentType componentType = null;
+
+        RhinoScript rhinoScript = implementation.getRhinoScript();
+        Scriptable scope = rhinoScript.getScriptScope();
+        Scriptable sca = (Scriptable) scope.get("SCA", scope);
+        if (sca != null) {
+            componentType = new ComponentType();
+
+            Object serviceClass = sca.get("serviceClass", scope);
+            if (serviceClass != null) {
+                ServiceDefinition service = new ServiceDefinition();
+                ServiceContract sc = new JavaServiceContract();
+                try {
+                    sc.setInterfaceClass(Class.forName(serviceClass.toString(), true, implementation.getClassLoader()));
+                } catch (ClassNotFoundException e) {
+                    throw new RuntimeException(e);
+                }
+                service.setServiceContract(sc);
+                componentType.add(service);
+            }
         }
-        service.setServiceContract(sc);
-        componentType.add(service);
-        return componentType;
-    }
 
-    protected JavaScriptComponentType loadByIntrospection(CompositeComponent<?> parent,
JavaScriptImplementation implementation,
-            DeploymentContext deploymentContext) throws ProcessingException {
-        // PojoComponentType componentType = new PojoComponentType();
-        // Class<?> implClass = implementation.getImplementationClass();
-        // introspector.introspect(parent, implClass, componentType, deploymentContext);
-        // return componentType;
-        return null;
+        return componentType;
     }
 
-    protected JavaScriptComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext)
throws LoaderException {
-        return loaderRegistry.load(null, url, JavaScriptComponentType.class, deploymentContext);
+    protected ComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext)
throws LoaderException {
+        return loaderRegistry.load(null, url, ComponentType.class, deploymentContext);
     }
 
     private String getBaseName(String scriptName) {

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementation.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementation.java?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementation.java
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementation.java
Tue Aug  1 09:39:54 2006
@@ -16,53 +16,40 @@
  */
 package org.apache.tuscany.container.javascript;
 
+import org.apache.tuscany.container.javascript.rhino.RhinoScript;
 import org.apache.tuscany.spi.model.AtomicImplementation;
+import org.apache.tuscany.spi.model.ComponentType;
 
 /**
  * Model object for a JavaScript implementation.
  */
-public class JavaScriptImplementation extends AtomicImplementation<JavaScriptComponentType>
{
+public class JavaScriptImplementation extends AtomicImplementation<ComponentType> {
 
-    private String script;
-    private String scriptName;
+    private RhinoScript rhinoScript;
     private ClassLoader cl;
-    private JavaScriptComponentType componentType;
+    private ComponentType componentType;
 
-    /**
-     * Returns the JavaScript source to be executed.
-     */
-    public String getScript() {
-        return script;
-    }
-
-    /**
-     * Sets the JavaScript source to be executed.
-     */
-    public void setScript(String script) {
-        this.script = script;
-    }
-
-    public JavaScriptComponentType getComponentType() {
+    public ComponentType getComponentType() {
         return componentType;
     }
 
-    public void setComponentType(JavaScriptComponentType componentType) {
+    public void setComponentType(ComponentType componentType) {
         this.componentType =  componentType;
     }
 
-    public String getScriptName() {
-        return scriptName;
-    }
-
-    public void setScriptName(String scriptName) {
-        this.scriptName = scriptName;
-    }
-
     public ClassLoader getClassLoader() {
         return cl;
     }
 
     public void setClassLoader(ClassLoader cl) {
         this.cl = cl;
+    }
+
+    public RhinoScript getRhinoScript() {
+        return rhinoScript;
+    }
+
+    public void setRhinoScript(RhinoScript rhinoScript) {
+        this.rhinoScript = rhinoScript;
     }
 }

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementationLoader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementationLoader.java?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementationLoader.java
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/main/java/org/apache/tuscany/container/javascript/JavaScriptImplementationLoader.java
Tue Aug  1 09:39:54 2006
@@ -25,6 +25,7 @@
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.tuscany.container.javascript.rhino.RhinoScript;
 import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.component.CompositeComponent;
 import org.apache.tuscany.spi.deployer.DeploymentContext;
@@ -64,8 +65,8 @@
         LoaderUtil.skipToEndElement(reader);
 
         JavaScriptImplementation implementation = new JavaScriptImplementation();
-        implementation.setScript(source);
-        implementation.setScriptName(script);
+        RhinoScript rhinoScript = new RhinoScript(script, source, null, cl);
+        implementation.setRhinoScript(rhinoScript);
         implementation.setClassLoader(cl);
         registry.loadComponentType(parent, implementation, deploymentContext);
         return implementation;

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/test/java/org/apache/tuscany/container/javascript/function/HelloWorldTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/test/java/org/apache/tuscany/container/javascript/function/HelloWorldTestCase.java?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/test/java/org/apache/tuscany/container/javascript/function/HelloWorldTestCase.java
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/test/java/org/apache/tuscany/container/javascript/function/HelloWorldTestCase.java
Tue Aug  1 09:39:54 2006
@@ -28,6 +28,7 @@
 public class HelloWorldTestCase extends SCATestCase {
 
     private HelloWorldService helloWorldService;
+    private HelloWorldService introspectableService;
 
     protected void setUp() throws Exception {
         addExtension("JavaScriptContainer", getClass().getClassLoader().getResource("META-INF/sca/default.scdl"));
@@ -36,9 +37,14 @@
 
         CompositeContext context = CurrentCompositeContext.getContext();
         helloWorldService = context.locateService(HelloWorldService.class, "HelloWorldComponent");
+        introspectableService = context.locateService(HelloWorldService.class, "IntrospectableHelloWorldComponent");
     }
 
     public void testHelloWorld() throws Exception {
         assertEquals(helloWorldService.sayHello("petra"), "Hello petra");
+    }
+
+    public void testIntrospectedHelloWorld() throws Exception {
+        assertEquals(introspectableService.sayHello("petra"), "Hello petra");
     }
 }

Added: incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js?rev=427641&view=auto
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js
(added)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js
Tue Aug  1 09:39:54 2006
@@ -0,0 +1,7 @@
+SCA = {
+	'serviceClass' : 'helloworld.HelloWorldService'
+}
+
+function sayHello(s) {
+	return "Hello " + s;
+}

Modified: incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/helloworld.scdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/helloworld.scdl?rev=427641&r1=427640&r2=427641&view=diff
==============================================================================
--- incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/helloworld.scdl
(original)
+++ incubator/tuscany/java/sca/containers/container.javascript/src/test/resources/org/apache/tuscany/container/javascript/function/helloworld.scdl
Tue Aug  1 09:39:54 2006
@@ -23,4 +23,8 @@
 		<js:implementation.js script="org/apache/tuscany/container/javascript/function/HelloWorld.js"/>
     </component>
 
+    <component name="IntrospectableHelloWorldComponent">
+		<js:implementation.js script="org/apache/tuscany/container/javascript/function/IntrospectableHelloWorld.js"/>
+    </component>
+
 </composite>



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


Mime
View raw message