Author: gnodet
Date: Thu Sep  3 15:31:36 2009
New Revision: 810995

URL: http://svn.apache.org/viewvc?rev=810995&view=rev
Log:
SMX4-321: Component task result from ServiceUnitManager.deploy is ignored

Modified:
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceUnitImpl.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/utils/ManagementSupport.java

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceUnitImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceUnitImpl.java?rev=810995&r1=810994&r2=810995&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceUnitImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceUnitImpl.java
 Thu Sep  3 15:31:36 2009
@@ -17,6 +17,8 @@
 package org.apache.servicemix.jbi.deployer.artifacts;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.jbi.JBIException;
 import javax.jbi.management.ComponentLifeCycleMBean;
@@ -28,7 +30,9 @@
 import org.apache.servicemix.jbi.deployer.ServiceAssembly;
 import org.apache.servicemix.jbi.deployer.ServiceUnit;
 import org.apache.servicemix.jbi.deployer.descriptor.ServiceUnitDesc;
+import org.apache.servicemix.jbi.deployer.utils.ManagementSupport;
 
+import org.w3c.dom.Element;
 
 public class ServiceUnitImpl implements ServiceUnit {
 
@@ -97,7 +101,11 @@
         ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
         try {
             
Thread.currentThread().setContextClassLoader(component.getComponentClassLoader());
-            component.getComponent().getServiceUnitManager().deploy(getName(), 
getRootDir() != null ? getRootDir().getAbsolutePath() : null);
+            String resultMsg = 
component.getComponent().getServiceUnitManager().deploy(getName(), getRootDir() 
!= null ? getRootDir().getAbsolutePath() : null);
+            List<Element> results = new ArrayList<Element>();
+            if (!ManagementSupport.getComponentTaskResult(resultMsg, 
getComponentName(), results)) {
+                throw ManagementSupport.failure("deploy", results);
+            }
         } finally {
             Thread.currentThread().setContextClassLoader(oldCl);
         }
@@ -152,7 +160,11 @@
         ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
         try {
             
Thread.currentThread().setContextClassLoader(component.getComponentClassLoader());
-            
component.getComponent().getServiceUnitManager().undeploy(getName(), 
getRootDir() != null ? getRootDir().getAbsolutePath() : null);
+            String resultMsg = 
component.getComponent().getServiceUnitManager().undeploy(getName(), 
getRootDir() != null ? getRootDir().getAbsolutePath() : null);
+            List<Element> results = new ArrayList<Element>();
+            if (!ManagementSupport.getComponentTaskResult(resultMsg, 
getComponentName(), results)) {
+                throw ManagementSupport.failure("undeploy", results);
+            }
         } finally {
             Thread.currentThread().setContextClassLoader(oldCl);
         }

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/utils/ManagementSupport.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/utils/ManagementSupport.java?rev=810995&r1=810994&r2=810995&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/utils/ManagementSupport.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/utils/ManagementSupport.java
 Thu Sep  3 15:31:36 2009
@@ -16,24 +16,31 @@
  */
 package org.apache.servicemix.jbi.deployer.utils;
 
+import java.io.IOException;
 import java.io.PrintWriter;
+import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.List;
 
 import javax.jbi.management.DeploymentException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 /**
- * ManagementMessageHelper is a class that ease the building of management 
messages.
+ * ManagementMessageHelper is a class that ease the parsing and build of 
management messages.
  */
 public final class ManagementSupport {
 
@@ -66,6 +73,8 @@
     public static final String VERSION = "version";
     public static final String XMLNS = "xmlns";
 
+    private static final String WRAPPER = "wrapper";
+    
     private static final Log LOG = LogFactory.getLog(ManagementSupport.class);
 
     private ManagementSupport() {
@@ -326,5 +335,81 @@
         msg.setComponent(component);
         return createComponentMessage(msg);
     }
+    
+    public static boolean getComponentTaskResult(String resultMsg, String 
component, List<Element> results) {
+        Element result = null;
+        boolean success = true;
+
+        try {
+            Document doc = parse(wrap(resultMsg));
+            Element wrapper = getElement(doc, WRAPPER);
+            result = getChildElement(wrapper, COMPONENT_TASK_RESULT);
+            Element e = getChildElement(result, COMPONENT_TASK_RESULT_DETAILS);
+            e = getChildElement(e, TASK_RESULT_DETAILS);
+            e = getChildElement(e, TASK_RESULT);
+            String r = DOMUtil.getElementText(e);
+            if (!SUCCESS.equals(r)) {
+                success = false;
+            }
+        } catch (Exception e) {
+            // The component did not throw an exception upon deployment, but 
the
+            // result string is not compliant: issue a warning and consider 
this
+            // is a successful deployment
+            try {
+                if (success) {
+                    result = 
ManagementSupport.createComponentWarning("deploy", component,
+                                                                      "Unable 
to parse result string", e);
+                } else {
+                    result = 
ManagementSupport.createComponentFailure("deploy", component,
+                                                                      "Unable 
to parse result string", e);
+                }
+            } catch (Exception e2) {
+                LOG.error(e2);
+                result = null;
+            }
+        }
+        if (result != null) {
+            results.add(result);
+        }
+        return success;
+    }
+    
+    /**
+     * Wrap the result message string to set the default namespace. The JBI 
spec
+     * is a bit misleading here: the javadoc for ServiceUnitManager.deploy 
shows
+     * a result string that does not declare the namespace for the
+     * component-task-result element. That would be invalid, but we'll hack the
+     * result string to allow it.
+     */
+    private static String wrap(String resultMsg) {
+        String xmlDecl = "<?xml version=\"1.0\" encoding=\"UTF-8\"/?>";
+        int ix = 0;
+        if (resultMsg.startsWith("<?xml")) {
+            ix = resultMsg.indexOf("?>") + 2;
+            xmlDecl = resultMsg.substring(0, ix);
+        }
+        return xmlDecl + "<" + WRAPPER + " xmlns=\"" + 
HTTP_JAVA_SUN_COM_XML_NS_JBI_MANAGEMENT_MESSAGE
+               + "\"> " + resultMsg.substring(ix) + "</" + WRAPPER + ">";
+    }
+
+    private static Document parse(String result) throws 
ParserConfigurationException, SAXException,
+        IOException {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setIgnoringElementContentWhitespace(true);
+        factory.setIgnoringComments(true);
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        return builder.parse(new InputSource(new StringReader(result)));
+    }
+
+    private static Element getElement(Document doc, String name) {
+        NodeList l = 
doc.getElementsByTagNameNS(HTTP_JAVA_SUN_COM_XML_NS_JBI_MANAGEMENT_MESSAGE, 
name);
+        return (Element)l.item(0);
+    }
+
+    private static Element getChildElement(Element element, String name) {
+        NodeList l = 
element.getElementsByTagNameNS(HTTP_JAVA_SUN_COM_XML_NS_JBI_MANAGEMENT_MESSAGE, 
name);
+        return (Element)l.item(0);
+    }
 
 }


Reply via email to