You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by sv...@apache.org on 2007/04/19 19:50:24 UTC

svn commit: r530498 - in /incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util: CompositeUtil.java PropertyUtil.java

Author: svkrish
Date: Thu Apr 19 10:50:21 2007
New Revision: 530498

URL: http://svn.apache.org/viewvc?view=rev&rev=530498
Log:
refactored code (breaking down length methods)

Modified:
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/CompositeUtil.java
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/PropertyUtil.java

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/CompositeUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/CompositeUtil.java?view=diff&rev=530498&r1=530497&r2=530498
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/CompositeUtil.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/CompositeUtil.java Thu Apr 19 10:50:21 2007
@@ -300,20 +300,12 @@
             reconcileComponentServices(component, implServices, compServices, problems);
             reconcileComponentReferences(component, implReferences, compReferences, problems);
             reconcileComponentProperties(component, implProperties, compProperties, problems);
-            try {
-                PropertyUtil.processProperties(composite, component);
-            } catch ( Exception e) {
-                problems.add(component);
-            }
         }
     }
-
-    private void wire(List<Base> problems) {
-
-        // Index and bind all component services and references
-        Map<String, ComponentService> componentServices = new HashMap<String, ComponentService>();
-        Map<String, ComponentReference> componentReferences = new HashMap<String, ComponentReference>();
-        
+    
+    private void indexAndBind(Map<String, ComponentService> componentServices,
+                              Map<String, ComponentReference> componentReferences,
+                              List<Base> problems) {
         for (Component component : composite.getComponents()) {
             int i =0;
             for (ComponentService componentService : component.getServices()) {
@@ -347,8 +339,11 @@
                 scaBinding.setComponent(component);
             }
         }
-
-        // Resolve promoted services and references
+        
+    }
+    
+    private void resolvePromotedServices(Map<String, ComponentService> componentServices,
+                                         List<Base> problems) {
         for (Service service : composite.getServices()) {
             CompositeService compositeService = (CompositeService)service;
             ComponentService componentService = compositeService.getPromotedService();
@@ -371,6 +366,10 @@
                 }
             }
         }
+    }
+    
+    private void resolvePromotedReferences(Map<String, ComponentReference> componentReferences,
+                                           List<Base> problems) {
         for (Reference reference : composite.getReferences()) {
             CompositeReference compositeReference = (CompositeReference)reference;
             List<ComponentReference> promotedReferences =
@@ -398,8 +397,11 @@
                 }
             }
         }
-
-        // Wire references to their targets
+    }
+    
+    private void wireCompRefToCompSvc(Map<String, ComponentService> componentServices,
+                                      Map<String, ComponentReference> componentReferences,
+                                      List<Base> problems) {
         for (ComponentReference componentReference : componentReferences.values()) {
             List<ComponentService> targets = componentReference.getTargets();
             if (!targets.isEmpty()) {
@@ -430,8 +432,11 @@
                 }
             }
         }
-
-        // Wire references as specified in wires
+    }
+    
+    private void resolveWireDefns(Map<String, ComponentService> componentServices,
+                                  Map<String, ComponentReference> componentReferences,
+                                  List<Base> problems) {
         List<Wire> wires = composite.getWires();
         for (int i = 0, n = wires.size(); i < n; i++) {
             Wire wire = wires.get(i);
@@ -466,6 +471,25 @@
                 resolvedReference.getTargets().add(resolvedService);
             }
         }
+    }
+
+    private void wire(List<Base> problems) {
+
+        // Index and bind all component services and references
+        Map<String, ComponentService> componentServices = new HashMap<String, ComponentService>();
+        Map<String, ComponentReference> componentReferences = new HashMap<String, ComponentReference>();
+        
+        indexAndBind(componentServices, componentReferences, problems);
+
+        // Resolve promoted services and references
+        resolvePromotedServices(componentServices, problems);
+        resolvePromotedReferences(componentReferences, problems);
+        
+        // Wire references to their targets
+        wireCompRefToCompSvc(componentServices, componentReferences, problems);
+
+        // Wire references as specified in wires
+        resolveWireDefns(componentServices, componentReferences, problems);
 
 
         // Validate that references are wired or promoted, according
@@ -482,5 +506,4 @@
         // Clear wires
         composite.getWires().clear();
     }
-
 }

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/PropertyUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/PropertyUtil.java?view=diff&rev=530498&r1=530497&r2=530498
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/PropertyUtil.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/assembly/util/PropertyUtil.java Thu Apr 19 10:50:21 2007
@@ -26,6 +26,7 @@
 import java.net.URL;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.parsers.DocumentBuilder;
@@ -64,11 +65,13 @@
     public static Document evaluate(NamespaceContext nsContext, Node node, String xPathExpression)
         throws XPathExpressionException, ParserConfigurationException {
         XPath path = XPATH_FACTORY.newXPath();
+        
         if (nsContext != null) {
             path.setNamespaceContext(nsContext);
         } else {
             path.setNamespaceContext(new DOMNamespeceContext(node));
         }
+        
         XPathExpression expression = path.compile(xPathExpression);
         Node result = (Node)expression.evaluate(node, XPathConstants.NODE);
         if (result == null) {
@@ -169,6 +172,69 @@
         }
     }
     
+    public static void sourceComponentProperties(Map<String, Property> compositeProperties,
+                                                 Component componentDefinition) throws InvalidValueException,
+                                                                               ParserConfigurationException,
+                                                                               XPathExpressionException,
+                                                                               TransformerException,
+                                                                               IOException {
+
+        List<ComponentProperty> componentProperties = componentDefinition.getProperties();
+        for (ComponentProperty aProperty : componentProperties) {
+            String source = aProperty.getSource();
+            String file = aProperty.getFile();
+            if (source != null) {
+                // $<name>/...
+                int index = source.indexOf('/');
+                if (index == -1) {
+                    // Tolerating $prop
+                    source = source + "/";
+                    index = source.length() - 1;
+                }
+                if (source.charAt(0) == '$') {
+                    String name = source.substring(1, index);
+                    Property compositeProp = compositeProperties.get(name);
+                    if (compositeProp == null) {
+                        InvalidValueException ex =
+                            new InvalidValueException(
+                                                      "The 'source' cannot be resolved to a composite property - " + source);
+                        throw ex;
+                    }
+
+                    boolean prependValue = false;
+                    DocumentBuilder builder =
+                        DocumentBuilderFactory.newInstance().newDocumentBuilder();
+                    Document compositePropDefValues = (Document)compositeProp.getValue();
+
+                    // Adding /value because the document root is "value"
+                    String path = source.substring(index);
+                    String xpath = null;
+
+                    if ("/".equals(path)) {
+                        // trailing / is not legal for xpath
+                        xpath = "/value";
+                    } else {
+                        xpath = "/value" + path;
+                    }
+
+                    // FIXME: How to deal with namespaces?
+                    Document node = evaluate(null, compositePropDefValues, xpath);
+
+                    if (node != null) {
+                        aProperty.setValue(node);
+                    }
+                } else {
+                    InvalidValueException ex =
+                        new InvalidValueException("The 'source' has an invalid value - " + source);
+                    throw ex;
+                }
+            } else if (file != null) {
+                aProperty.setValue(loadFromFile(aProperty.getFile()));
+
+            }
+        }
+    }
+    
     private static Property getPropertyByName(List<Property> properties, String propertyName) {
         for (Property property : properties) {
             if (property.getName().equals(propertyName)) {
@@ -191,12 +257,10 @@
         }
 
         public String getNamespaceURI(String prefix) {
-            //return "http://foo";
             return node.lookupNamespaceURI(prefix);
         }
 
         public String getPrefix(String namespaceURI) {
-            //return "foo";
             return node.lookupPrefix(namespaceURI);
         }
 
@@ -206,13 +270,18 @@
 
     }
     
-    private static void printNode(Node node) throws Exception {
-        javax.xml.transform.Transformer transformer =
-            TransformerFactory.newInstance().newTransformer();
-        StringWriter sw = new StringWriter();
-        transformer.transform(new DOMSource(node), new StreamResult(sw));
+    public static void printNode(Node node)  {
+        try {
+            javax.xml.transform.Transformer transformer =
+                TransformerFactory.newInstance().newTransformer();
+            StringWriter sw = new StringWriter();
+            transformer.transform(new DOMSource(node), new StreamResult(sw));
+            
+            System.out.println(sw.toString());
+        } catch ( Exception e ) {
+            e.printStackTrace();
+        }
         
-        System.out.println(sw.toString());
     }
 
 }



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