Author: lresende
Date: Mon Mar 10 23:50:43 2008
New Revision: 635820

URL: http://svn.apache.org/viewvc?rev=635820&view=rev
Log:
Adding support for Properties to implementation.widget

Modified:
    
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementation.java
    
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationIntrospector.java
    
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationProcessor.java
    
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java
    
incubator/tuscany/java/sca/modules/implementation-widget/src/test/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationTestCase.java
    
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/content/store.html
    
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/widget.composite

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementation.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementation.java?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementation.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementation.java
 Mon Mar 10 23:50:43 2008
@@ -19,6 +19,7 @@
 package org.apache.tuscany.sca.implementation.widget;
 
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -40,10 +41,9 @@
  * @version $Rev$ $Date$
  */
 public class WidgetImplementation implements Implementation {
-    private AssemblyFactory assemblyFactory;
-    
     private Service widgetService;
-    private List<Reference> references = null;
+    private List<Reference> references = new ArrayList<Reference>();
+    private List<Property> properties = new ArrayList<Property>();
     
     private String location;
     private URL url;
@@ -55,8 +55,6 @@
     WidgetImplementation(AssemblyFactory assemblyFactory,
                          JavaInterfaceFactory javaFactory) {
 
-        this.assemblyFactory = assemblyFactory;
-        
         // Resource implementation always provide a single service exposing
         // the Resource interface, and have no references and properties
         widgetService = assemblyFactory.createService();
@@ -96,8 +94,7 @@
     }
 
     public List<Property> getProperties() {
-        // The resource implementation does not support properties
-        return Collections.emptyList();
+        return properties;
     }
 
     public List<Service> getServices() {
@@ -106,10 +103,6 @@
     }
     
     public List<Reference> getReferences() {
-        if(this.references == null) {
-            WidgetImplementationIntrospector widgetIntrospector = new 
WidgetImplementationIntrospector(assemblyFactory, this);
-            references = widgetIntrospector.getReferences();
-        }
         return references;
     }
 

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationIntrospector.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationIntrospector.java?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationIntrospector.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationIntrospector.java
 Mon Mar 10 23:50:43 2008
@@ -20,15 +20,16 @@
 package org.apache.tuscany.sca.implementation.widget;
 
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Scanner;
 
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Property;
 import org.apache.tuscany.sca.assembly.Reference;
 
+
 class WidgetImplementationIntrospector {
     private static final String WEB_REFERENCE_ANNOTATION = "//@Reference";
+    private static final String WEB_PROPERTY_ANNOTATION = "//@Property";
     
     private AssemblyFactory assemblyFactory;
     private WidgetImplementation widgetImplementation;
@@ -40,23 +41,30 @@
     
     
     /**
-     * Introspect the References of a given htmlWidget
-     * @return
+     * Introspect and populate a given widget implementation
      */
-    List<Reference> getReferences() {
-        List<Reference> references = new ArrayList<Reference>();
+    public void introspectImplementation() {
         URL htmlWidget = widgetImplementation.getLocationURL();
         
         try {
             Scanner scanner = new Scanner(htmlWidget.openStream());
             while(scanner.hasNextLine()) {
                 String line = scanner.nextLine();
-                if(line.contains(WEB_REFERENCE_ANNOTATION)) {
+                if(line.contains(WEB_PROPERTY_ANNOTATION)) {
+                       //process the next line, as it has the property info
+                       if (scanner.hasNextLine()) {
+                               Property property = 
processPropertyScript(scanner.nextLine());
+                               if (property != null) {
+                                       
widgetImplementation.getProperties().add(property);
+                               }
+                       }
+                       
+                } else if(line.contains(WEB_REFERENCE_ANNOTATION)) {
                     //process the next line, as it has the reference info
-                    if(scanner.hasNextLine()) {
+                    if (scanner.hasNextLine()) {
                         Reference reference = 
processReferenceScript(scanner.nextLine());
                         if(reference != null){
-                            references.add(reference);
+                               
widgetImplementation.getReferences().add(reference);
                         }
                         
                     }
@@ -67,8 +75,36 @@
             
         }
         
-        return references;
+       
+    }
+    
+    
+    /**
+     * Process Property declaration in JavaScript code
+     * Supported ways:
+     *    //@Property
+     *    var locale = Property("locale");
+     *    
+     *    //@Property
+     *    locale = Property("locale");
+     *    
+     * @param scriptContent
+     * @return
+     */
+    private Property processPropertyScript(String scriptContent) {
+       Property property = null;
+        String propertyName = null;
+        
+        String tokens[] = scriptContent.split("=");
+        tokens = tokens[0].split(" ");
+        propertyName = tokens[tokens.length -1];
+        
+        if(propertyName != null) {
+            property = assemblyFactory.createProperty();
+            property.setName(propertyName);     
+        }
         
+        return property;
     }
     
     /**
@@ -98,8 +134,4 @@
         
         return reference;
     }
-    
-    
-
-
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationProcessor.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationProcessor.java?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationProcessor.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationProcessor.java
 Mon Mar 10 23:50:43 2008
@@ -28,6 +28,7 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
 import org.apache.tuscany.sca.assembly.xml.Constants;
 import org.apache.tuscany.sca.contribution.Artifact;
 import org.apache.tuscany.sca.contribution.ContributionFactory;
@@ -47,12 +48,14 @@
 public class WidgetImplementationProcessor implements 
StAXArtifactProcessor<WidgetImplementation> {
     private static final QName IMPLEMENTATION_WIDGET = new 
QName(Constants.SCA10_TUSCANY_NS, "implementation.widget");
     
+    private AssemblyFactory assemblyFactory;
     private ContributionFactory contributionFactory;
     private WidgetImplementationFactory implementationFactory;
     
     public WidgetImplementationProcessor(ModelFactoryExtensionPoint 
modelFactories) {
+       assemblyFactory = modelFactories.getFactory(AssemblyFactory.class);
         contributionFactory = 
modelFactories.getFactory(ContributionFactory.class);
-        implementationFactory = new 
WidgetImplementationFactory(modelFactories); 
//modelFactories.getFactory(WidgetImplementationFactory.class);
+        implementationFactory = new 
WidgetImplementationFactory(modelFactories); 
     }
 
     public QName getArtifactType() {
@@ -69,11 +72,10 @@
         
         // Read an <implementation.widget> element
 
-        // Read the location attribute specifying the location of the
-        // resources
+        // Read the location attribute specifying the location of the resources
         String location = reader.getAttributeValue(null, "location");
 
-        // Create an initialize the resource implementationmodel
+        // Create an initialize the resource implementation model
         WidgetImplementation implementation = 
implementationFactory.createWidgetImplementation();
         implementation.setLocation(location);
         implementation.setUnresolved(true);
@@ -98,6 +100,11 @@
             try {
                 implementation.setLocationURL(new URL(resolved.getLocation()));
                 implementation.setUnresolved(false);
+                
+                //introspect implementation
+                WidgetImplementationIntrospector widgetIntrospector = new 
WidgetImplementationIntrospector(assemblyFactory, implementation);
+                widgetIntrospector.introspectImplementation();
+
             } catch (IOException e) {
                 throw new ContributionResolveException(e);
             }

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java
 Mon Mar 10 23:50:43 2008
@@ -29,11 +29,15 @@
 import java.net.URL;
 
 import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.ComponentProperty;
 import org.apache.tuscany.sca.assembly.ComponentReference;
 import org.apache.tuscany.sca.assembly.OptimizableBinding;
+import org.apache.tuscany.sca.assembly.Property;
 import org.apache.tuscany.sca.invocation.Invoker;
 import org.apache.tuscany.sca.invocation.Message;
 import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 
 /**
@@ -92,6 +96,8 @@
     }
 
     /**
+     * This helper class concatenates the necessary javascript client code 
into a
+     * single javascript per component
      */
     private InputStream generateWidgetCode() throws IOException {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -110,6 +116,10 @@
             }
         }
         
+        //process properties
+        generateJavaScriptPropertyFunction(pw);
+        
+        //process references
         generateJavaScriptReferenceFunction(pw);
         
        
@@ -128,7 +138,7 @@
      */
     private void generateJavaScriptBindingProxy(PrintWriter pw, String 
bindingProxyName) throws IOException {
         
-        URL url = getClass().getClassLoader().getResource(bindingProxyName); 
//Thread.currentThread().getContextClassLoader().getResource(bindingProxyName);
+        URL url = getClass().getClassLoader().getResource(bindingProxyName);
         InputStream is = url.openStream();
         int i;
         while ((i = is.read()) != -1) {
@@ -138,6 +148,50 @@
         pw.println();
     }
     
+    /**
+     * Generate JavaScript code to inject SCA Properties
+     * @param pw
+     * @throws IOException
+     */
+    private void generateJavaScriptPropertyFunction(PrintWriter pw) throws 
IOException {
+
+        pw.println("var propertyMap = new String();");
+        for(ComponentProperty property : component.getProperties()) {
+            String propertyName = property.getName();
+
+            pw.println("propertyMap." + propertyName + " = \"" + 
getPropertyValue(property) + "\"");
+        }
+        
+        pw.println("function Property(name) {");
+        pw.println("    return propertyMap[name];");
+        pw.println("}");
+    }
+    
+    /**
+     * Convert property value to String
+     * @param property
+     * @return
+     */
+    private String getPropertyValue(ComponentProperty property) {
+       Document doc = (Document)property.getValue();
+       Element rootElement = doc.getDocumentElement();
+       
+       String value = null;
+       
+       //FIXME : Provide support for isMany and other property types
+       
+       if (rootElement.getChildNodes().getLength() > 0) {
+            value = rootElement.getChildNodes().item(0).getTextContent();
+        }
+       
+       return value;
+    }
+    
+    /**
+     * Generate JavaScript code to inject SCA References
+     * @param pw
+     * @throws IOException
+     */
     private void generateJavaScriptReferenceFunction (PrintWriter pw) throws 
IOException {
         
         pw.println("var referenceMap = new Object();");

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/test/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationTestCase.java?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/java/org/apache/tuscany/sca/implementation/widget/WidgetImplementationTestCase.java
 Mon Mar 10 23:50:43 2008
@@ -43,7 +43,7 @@
     
     public void testPing() throws Exception {
         new Socket("127.0.0.1", 8085);
-        //System.in.read();
+        System.in.read();
     }
 
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/content/store.html
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/content/store.html?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/content/store.html
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/content/store.html
 Mon Mar 10 23:50:43 2008
@@ -29,7 +29,10 @@
        var catalog = new Reference("Catalog");
        
        //@Reference
-       var shoppingCart = new Reference("ShoppingCart"); 
+       var shoppingCart = new Reference("ShoppingCart"); 
+       
+       //@Property
+       var locale = Property("locale");
 
        function catalog_getResponse(items) {
                var catalog = "";
@@ -85,7 +88,8 @@
                document.getElementById('shoppingCart').innerHTML = "";
                document.getElementById('total').innerHTML = "";        
        }       
-
+
+    alert(locale);
        catalog.get(catalog_getResponse);
        shoppingCart.get("", shoppingCart_getResponse);
 </script>

Modified: 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/widget.composite
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/widget.composite?rev=635820&r1=635819&r2=635820&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/widget.composite
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-widget/src/test/resources/widget.composite
 Mon Mar 10 23:50:43 2008
@@ -25,6 +25,7 @@
 
     <component name="store">
         <tuscany:implementation.widget location="content/store.html"/>
+        <property name="locale">en</property>
         <service name="Widget">
                <tuscany:binding.http uri="http://localhost:8085/store"/> 
         </service>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to