tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r527508 - in /incubator/tuscany/java/sca/modules/implementation-script/src: main/java/org/apache/tuscany/implementation/script/ test/resources/ test/resources/org.apache.tuscany.implementation.script.itests.javascript/
Date Wed, 11 Apr 2007 14:46:44 GMT
Author: antelder
Date: Wed Apr 11 07:46:43 2007
New Revision: 527508

URL: http://svn.apache.org/viewvc?view=rev&rev=527508
Log:
Attempt at supporting .componentType sidefiles for scipt components

Added:
    incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/helloworld.componentType
      - copied unchanged from r527480, incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/org.apache.tuscany.implementation.script.itests.javascript/helloworld.componentType
    incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/helloworld.js
      - copied unchanged from r527480, incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/org.apache.tuscany.implementation.script.itests.javascript/helloworld.js
Removed:
    incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/org.apache.tuscany.implementation.script.itests.javascript/helloworld.componentType
    incubator/tuscany/java/sca/modules/implementation-script/src/test/resources/org.apache.tuscany.implementation.script.itests.javascript/helloworld.js
Modified:
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptArtifactProcessor.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptComponent.java
    incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptImplementation.java

Modified: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptArtifactProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptArtifactProcessor.java?view=diff&rev=527508&r1=527507&r2=527508
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptArtifactProcessor.java
(original)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptArtifactProcessor.java
Wed Apr 11 07:46:43 2007
@@ -21,6 +21,10 @@
 
 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
 
+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;
@@ -28,6 +32,8 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.tuscany.assembly.ComponentType;
+import org.apache.tuscany.assembly.impl.DefaultAssemblyFactory;
 import org.apache.tuscany.assembly.xml.Constants;
 import org.apache.tuscany.services.spi.contribution.ArtifactResolver;
 import org.apache.tuscany.services.spi.contribution.ContributionReadException;
@@ -49,9 +55,8 @@
 
         try {
 
-            ScriptImplementation scriptImplementation = new ScriptImplementation();
-            scriptImplementation.setUnresolved(true);
-            scriptImplementation.setName(reader.getAttributeValue(null, SCRIPT));
+            String scriptName = reader.getAttributeValue(null, SCRIPT);
+            ScriptImplementation scriptImplementation = new ScriptImplementation(scriptName);
 
             // Skip to end element
             while (reader.hasNext()) {
@@ -59,6 +64,9 @@
                     break;
                 }
             }
+
+            processComponentType(scriptImplementation);
+
             return scriptImplementation;
 
         } catch (XMLStreamException e) {
@@ -66,6 +74,22 @@
         }
     }
 
+    private void processComponentType(ScriptImplementation scriptImplementation) {
+        // Form the URI of the expected .componentType file;
+
+        String ctName = scriptImplementation.getName();
+        int lastDot = ctName.lastIndexOf('.');
+        ctName = ctName.substring(0, lastDot) + ".componentType";
+        
+        String uri = ctName;
+
+        // Create a ComponentType and mark it unresolved
+        ComponentType componentType = new DefaultAssemblyFactory().createComponentType();
+        componentType.setURI(uri);
+        componentType.setUnresolved(true);
+        scriptImplementation.setComponentType(componentType);
+    }
+
     public void write(ScriptImplementation scriptImplementation, XMLStreamWriter writer)
throws ContributionWriteException {
         try {
 
@@ -81,26 +105,14 @@
     }
 
     public void resolve(ScriptImplementation scriptImplementation, ArtifactResolver resolver)
throws ContributionResolveException {
-        try {
 
-            // TODO: implement
-            URL scriptSrcUrl = Thread.currentThread().getContextClassLoader().getResource(scriptImplementation.getName());
-//            Class javaClass = Class.forName(scriptImplementation.getName(), true, Thread.currentThread().getContextClassLoader());
-//            scriptImplementation.setJavaClass(javaClass);
-//            
-//            //FIXME JavaImplementationDefinition should not be mandatory 
-//            if (scriptImplementation instanceof ScriptImplementationDefinition) {
-//                introspectionRegistry.introspect(scriptImplementation.getJavaClass(), (ScriptImplementationDefinition)scriptImplementation);
-//                
-//                //FIXME the introspector should always create at least one service
-//                if (scriptImplementation.getServices().isEmpty()) {
-//                    scriptImplementation.getServices().add(new ServiceImpl());
-//                }
-//            }
+        scriptImplementation.setScriptSrc(readScript(scriptImplementation.getName()));
 
-        } catch (Exception e) {
-            throw new ContributionResolveException(e);
+        ComponentType componentType = resolver.resolve(ComponentType.class, scriptImplementation.getComponentType());
+        if (componentType.isUnresolved()) {
+            throw new ContributionResolveException("missing .componentType side file");
         }
+        scriptImplementation.setComponentType(componentType);
     }
 
     public void wire(ScriptImplementation model) throws ContributionWireException {
@@ -113,6 +125,43 @@
 
     public Class<ScriptImplementation> getModelType() {
         return ScriptImplementation.class;
+    }
+
+    protected String readScript(String scriptName) throws ContributionResolveException {
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        URL scriptSrcUrl = cl.getResource(scriptName);
+        if (scriptSrcUrl == null) {
+            throw new ContributionResolveException("No script: " + scriptName);
+        }
+
+        InputStream is;
+        try {
+            is = scriptSrcUrl.openStream();
+        } catch (IOException e) {
+            throw new ContributionResolveException(e);
+        }
+
+        try {
+
+            Reader reader = new InputStreamReader(is, "UTF-8");
+            char[] buffer = new char[1024];
+            StringBuilder source = new StringBuilder();
+            int count;
+            while ((count = reader.read(buffer)) > 0) {
+                source.append(buffer, 0, count);
+            }
+
+            return source.toString();
+
+        } catch (IOException e) {
+            throw new ContributionResolveException(e);
+        } finally {
+            try {
+                is.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
     }
 
 }

Modified: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptComponent.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptComponent.java?view=diff&rev=527508&r1=527507&r2=527508
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptComponent.java
(original)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptComponent.java
Wed Apr 11 07:46:43 2007
@@ -19,12 +19,9 @@
 
 package org.apache.tuscany.implementation.script;
 
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.net.URI;
-import java.net.URL;
 import java.util.List;
 
 import javax.script.ScriptEngine;

Modified: incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptImplementation.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptImplementation.java?view=diff&rev=527508&r1=527507&r2=527508
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptImplementation.java
(original)
+++ incubator/tuscany/java/sca/modules/implementation-script/src/main/java/org/apache/tuscany/implementation/script/ScriptImplementation.java
Wed Apr 11 07:46:43 2007
@@ -18,6 +18,7 @@
  */
 package org.apache.tuscany.implementation.script;
 
+import org.apache.tuscany.assembly.ComponentType;
 import org.apache.tuscany.assembly.Implementation;
 import org.apache.tuscany.assembly.impl.ComponentTypeImpl;
 
@@ -28,16 +29,15 @@
 
     private String scriptName;
     private String scriptSrc;
+    private ComponentType componentType;
 
-    public String getName() {
-        return scriptName;
+    public ScriptImplementation(String scriptName) {
+        this.scriptName = scriptName;
+        setUnresolved(true);
     }
 
-    public void setName(String scriptName) {
-        if (!isUnresolved()) {
-            throw new IllegalStateException();
-        }
-        this.scriptName = scriptName;
+    public String getName() {
+        return scriptName;
     }
 
     public String getScriptSrc() {
@@ -48,6 +48,14 @@
         this.scriptSrc = scriptSrc;
     }
 
+    public ComponentType getComponentType() {
+        return componentType;
+    }
+
+    public void setComponentType(ComponentType componentType) {
+        this.componentType = componentType;
+    }
+
     @Override
     public int hashCode() {
         return String.valueOf(getName()).hashCode();
@@ -62,4 +70,5 @@
         else
             return false;
     }
+
 }



---------------------------------------------------------------------
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