Michael Kublin has uploaded a new change for review.

Change subject: engine: Improving export of xmls
......................................................................

engine: Improving export of xmls

The following patch is one of the first step which is done in order to improve 
our
xml read/write infrastructure.
The change contains:
1. During export of xml no need to create a temp file and after that load it, 
the following code was removed.
2. Clean up of appropriate files
3. removing un needed classes
Main benefits, perfromance should be improved on every export xml operation 
(removed i/o), code should become
more clean

Change-Id: If8206580740be92cac7ffdbdc1c2f53ac0021d2e
Signed-off-by: Michael Kublin <[email protected]>
---
D 
backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Formatting.java
M 
backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlDocument.java
M 
backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlTextWriter.java
D 
backend/manager/modules/compat/src/test/java/org/ovirt/engine/core/compat/backendcompat/XmlDocumentTest.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/IOvfBuilder.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfManager.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfReader.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfTemplateWriter.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfWriter.java
10 files changed, 54 insertions(+), 159 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/21/11221/1

diff --git 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Formatting.java
 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Formatting.java
deleted file mode 100644
index 601db94..0000000
--- 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Formatting.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package org.ovirt.engine.core.compat;
-
-public enum Formatting {
-    Indented
-
-}
diff --git 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlDocument.java
 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlDocument.java
index dc970fb..4217bd7 100644
--- 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlDocument.java
+++ 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlDocument.java
@@ -1,10 +1,5 @@
 package org.ovirt.engine.core.compat.backendcompat;
 
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.StringReader;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -14,8 +9,6 @@
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
-import org.ovirt.engine.core.compat.CompatException;
-import org.ovirt.engine.core.compat.Encoding;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -24,7 +17,7 @@
 public class XmlDocument {
 
     public Object NameTable;
-    public String OuterXml;
+    private String outerXml;
     public XmlNode[] ChildNodes;
 
     private Document doc;
@@ -44,10 +37,9 @@
                 ChildNodes[i] = new XmlNode(list.item(i));
             }
 
-            OuterXml = ovfstring;
+            outerXml = ovfstring;
         } catch (Exception e) {
-            CompatException ce = new CompatException(e.getMessage(), 
e.getCause());
-            throw ce;
+            throw new RuntimeException(e.getMessage(), e.getCause());
         }
     }
 
@@ -97,30 +89,7 @@
         }
     }
 
-    public void Load(String filename) {
-        InputStream is = null;
-        try {
-            is = new FileInputStream(filename);
-            BufferedReader r = new BufferedReader(new InputStreamReader(is, 
Encoding.UTF8.name()));
-            StringBuffer buffer = new StringBuffer();
-
-            String line;
-            while (null != (line = r.readLine())) {
-                buffer.append(line);
-                buffer.append("\n");
-            }
-
-            LoadXml(buffer.toString());
-        } catch (IOException e) {
-            throw new RuntimeException("Failed to load file: " + filename, e);
-        } finally {
-            if(is != null) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    //ignore
-                }
-            }
-        }
+    public String getOuterXml() {
+        return outerXml;
     }
 }
diff --git 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlTextWriter.java
 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlTextWriter.java
index 9e62815..f119d9b 100644
--- 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlTextWriter.java
+++ 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/XmlTextWriter.java
@@ -1,6 +1,6 @@
 package org.ovirt.engine.core.compat.backendcompat;
 
-import java.io.FileOutputStream;
+import java.io.StringWriter;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -12,18 +12,16 @@
 
 public class XmlTextWriter {
 
-    public Object Formatting;
-    public int Indentation;
+    private XMLStreamWriter writer;
+    private StringWriter stream = new StringWriter();
 
-    XMLStreamWriter writer;
-
-    public XmlTextWriter(String name, Encoding utf8) {
+    public XmlTextWriter(Encoding encoding) {
         try {
             XMLOutputFactory factory = XMLOutputFactory.newInstance();
-            writer = factory.createXMLStreamWriter(new FileOutputStream(name), 
"UTF-8");
-            writer.writeStartDocument("UTF-8", "1.0");
+            writer = factory.createXMLStreamWriter(stream);
+            writer.writeStartDocument(encoding.name(), "1.0");
         } catch (Exception e) {
-            throw new RuntimeException("Failed to initialize xml writer: " + 
name, e);
+            throw new RuntimeException("Failed to initialize xml writer: ", e);
         }
     }
 
@@ -125,17 +123,19 @@
         WriteEndElement();
     }
 
-    public void close() {
-        try {
-            writer.flush();
-            writer.close();
-        } catch (XMLStreamException e) {
-            throw new RuntimeException("Failed to close xml writer", e);
-        }
-    }
-
     public void WriteAttributeString(String namespaceURI, String localName, 
int value) {
         WriteAttributeString(namespaceURI, localName, Integer.toString(value));
     }
 
+    public String getStringXML() {
+        try {
+            writer.writeEndElement();
+            writer.flush();
+            writer.close();
+            return stream.getBuffer().toString();
+        } catch (XMLStreamException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 }
diff --git 
a/backend/manager/modules/compat/src/test/java/org/ovirt/engine/core/compat/backendcompat/XmlDocumentTest.java
 
b/backend/manager/modules/compat/src/test/java/org/ovirt/engine/core/compat/backendcompat/XmlDocumentTest.java
deleted file mode 100644
index bd6137e..0000000
--- 
a/backend/manager/modules/compat/src/test/java/org/ovirt/engine/core/compat/backendcompat/XmlDocumentTest.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package org.ovirt.engine.core.compat.backendcompat;
-
-import java.io.FileWriter;
-import java.io.IOException;
-
-import org.junit.Test;
-
-public class XmlDocumentTest {
-    @Test
-    public void Load() throws IOException {
-        final java.io.File temp = java.io.File.createTempFile("test-", ".xml");
-        try {
-            FileWriter writer = new FileWriter(temp);
-            writer.write("<?xml version=\"1.0\"?>\n <foo> <bar/> <!-- comment 
--> </foo>");
-            writer.close();
-
-            final XmlDocument document = new XmlDocument();
-            for (int i = 0; i < 2048; i++) {
-                document.Load(temp.getAbsolutePath());
-            }
-
-        } finally {
-            temp.delete();
-        }
-
-    }
-}
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/IOvfBuilder.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/IOvfBuilder.java
index f5d514e..31863ef 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/IOvfBuilder.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/IOvfBuilder.java
@@ -20,4 +20,6 @@
     void buildDisk();
 
     void buildVirtualSystem();
+
+    String getStringRepresentation();
 }
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfManager.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfManager.java
index 490d8a1..138ac4c 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfManager.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfManager.java
@@ -11,36 +11,19 @@
 import org.ovirt.engine.core.compat.backendcompat.XmlDocument;
 
 public class OvfManager {
-    /**
-     * EINAV TODO: DateTimeFormat is currently not in use. Need to find a way 
the DateTime.Parse/TryParse will surely
-     * work.
-     */
-    public static String DateTimeFormat = "dd/MM/yyy HH:mm:ss";
 
     public String ExportVm(VM vm, ArrayList<DiskImage> images) {
-        XmlDocument document = new XmlDocument();
-        OvfWriter ovf = new OvfVmWriter(document, vm, images);
-        try {
-            BuildOvf(ovf);
-        } finally {
-            ovf.dispose();
-        }
-        // document.outerxml will be valid only out of the using block
-        // because the Dispose closing the document
-        return document.OuterXml;
+        OvfWriter ovf = new OvfVmWriter(vm, images);
+        BuildOvf(ovf);
+
+        return ovf.getStringRepresentation();
     }
 
     public String ExportTemplate(VmTemplate vmTemplate, List<DiskImage> 
images) {
-        XmlDocument document = new XmlDocument();
-        OvfWriter ovf = new OvfTemplateWriter(document, vmTemplate, images);
-        try {
-            BuildOvf(ovf);
-        } finally {
-            ovf.dispose();
-        }
-        // document.outerxml will be valid only out of the using block
-        // because the Dispose closing the document
-        return document.OuterXml;
+        OvfWriter ovf = new OvfTemplateWriter(vmTemplate, images);
+        BuildOvf(ovf);
+
+        return ovf.getStringRepresentation();
     }
 
     public void ImportVm(String ovfstring,
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfReader.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfReader.java
index a8091b0..16b2376 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfReader.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfReader.java
@@ -555,7 +555,7 @@
                     if (valueNode.getNodeType() == Node.TEXT_NODE) {
                         returnValue.put(currNode.getNodeName(), 
valueNode.getNodeValue());
                     }
-                } else if (childNodes.getLength() > 1){
+                } else if (childNodes.getLength() > 1) {
                     // In this case, we have a nested map, so we parse it
                     returnValue.put(currNode.getNodeName(), getMapNode(new 
XmlNode(currNode)));
                 }
@@ -564,4 +564,9 @@
 
         return returnValue;
     }
+
+    @Override
+    public String getStringRepresentation() {
+        return _document.getOuterXml();
+    }
 }
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfTemplateWriter.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfTemplateWriter.java
index 0ce912a..7672b19 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfTemplateWriter.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfTemplateWriter.java
@@ -9,13 +9,12 @@
 import 
org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface;
 import org.ovirt.engine.core.common.config.Config;
 import org.ovirt.engine.core.common.config.ConfigValues;
-import org.ovirt.engine.core.compat.backendcompat.XmlDocument;
 
 public class OvfTemplateWriter extends OvfWriter {
     protected VmTemplate _vmTemplate;
 
-    public OvfTemplateWriter(XmlDocument document, VmTemplate vmTemplate, 
List<DiskImage> images) {
-        super(document, vmTemplate, images);
+    public OvfTemplateWriter(VmTemplate vmTemplate, List<DiskImage> images) {
+        super(vmTemplate, images);
         _vmTemplate = vmTemplate;
     }
 
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
index 10aa121..2991b93 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
@@ -17,13 +17,12 @@
 import org.ovirt.engine.core.compat.Regex;
 import org.ovirt.engine.core.compat.RegexOptions;
 import org.ovirt.engine.core.compat.StringHelper;
-import org.ovirt.engine.core.compat.backendcompat.XmlDocument;
 
 public class OvfVmWriter extends OvfWriter {
     private static final String EXPORT_ONLY_PREFIX = "exportonly_";
 
-    public OvfVmWriter(XmlDocument document, VM vm, List<DiskImage> images) {
-        super(document, vm.getStaticData(), images);
+    public OvfVmWriter(VM vm, List<DiskImage> images) {
+        super(vm.getStaticData(), images);
         _vm = vm;
     }
 
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfWriter.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfWriter.java
index 1442b02..d029e88 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfWriter.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfWriter.java
@@ -1,6 +1,5 @@
 package org.ovirt.engine.core.utils.ovf;
 
-import java.io.File;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -16,15 +15,12 @@
 import org.ovirt.engine.core.common.utils.VmDeviceCommonUtils;
 import org.ovirt.engine.core.common.utils.VmDeviceType;
 import org.ovirt.engine.core.compat.Encoding;
-import org.ovirt.engine.core.compat.Formatting;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.compat.StringHelper;
-import org.ovirt.engine.core.compat.backendcompat.Path;
 import org.ovirt.engine.core.compat.backendcompat.XmlDocument;
 import org.ovirt.engine.core.compat.backendcompat.XmlTextWriter;
 
 public abstract class OvfWriter implements IOvfBuilder {
-    protected String _fileName;
     protected int _instanceId;
     protected List<DiskImage> _images;
     protected XmlTextWriter _writer;
@@ -32,19 +28,16 @@
     protected VM _vm;
     protected VmBase vmBase;
 
-    public OvfWriter(XmlDocument document, VmBase vmBase, List<DiskImage> 
images) {
-        _fileName = Path.GetTempFileName();
-        _document = document;
+    public OvfWriter(VmBase vmBase, List<DiskImage> images) {
+        _document = new XmlDocument();
         _images = images;
-        _writer = new XmlTextWriter(_fileName, Encoding.UTF8);
+        _writer = new XmlTextWriter(Encoding.UTF8);
         this.vmBase = vmBase;
         WriteHeader();
     }
 
     private void WriteHeader() {
         _instanceId = 0;
-        _writer.Formatting = Formatting.Indented;
-        _writer.Indentation = 4;
         _writer.WriteStartDocument(false);
 
         _writer.SetPrefix(OVF_PREFIX, OVF_URI);
@@ -60,10 +53,6 @@
 
         // Setting the OVF version according to ENGINE (in 2.2 , version was 
set to "0.9")
         _writer.WriteAttributeString(OVF_URI, "version", Config.<String> 
GetValue(ConfigValues.VdcVersion));
-    }
-
-    private void CloseElements() {
-        _writer.WriteEndElement();
     }
 
     protected long BytesToGigabyte(long bytes) {
@@ -114,7 +103,9 @@
             _writer.WriteStartElement("Disk");
             _writer.WriteAttributeString(OVF_URI, "diskId", 
image.getImageId().toString());
             _writer.WriteAttributeString(OVF_URI, "size", 
String.valueOf(BytesToGigabyte(image.getsize())));
-            _writer.WriteAttributeString(OVF_URI, "actual_size", 
String.valueOf(BytesToGigabyte(image.getactual_size())));
+            _writer.WriteAttributeString(OVF_URI,
+                    "actual_size",
+                    String.valueOf(BytesToGigabyte(image.getactual_size())));
             _writer.WriteAttributeString(OVF_URI, "vm_snapshot_id", 
(image.getvm_snapshot_id() != null) ? image
                     .getvm_snapshot_id().getValue().toString() : "");
 
@@ -251,12 +242,6 @@
 
     protected abstract void WriteContentItems();
 
-    @Override
-    protected void finalize() throws Throwable {
-        dispose();
-        super.finalize();
-    }
-
     protected void writeManagedDeviceInfo(VmBase vmBase, XmlTextWriter writer, 
Guid deviceId) {
         VmDevice vmDevice = vmBase.getManagedDeviceMap().get(deviceId);
         if (deviceId != null && vmDevice != null && vmDevice.getAddress() != 
null) {
@@ -337,25 +322,6 @@
         }
     }
 
-    public void deleteTmpFile() {
-        try {
-            File tmpFile = new File(_fileName);
-            if (tmpFile.exists()) {
-                tmpFile.delete();
-            }
-        } catch (Exception e) {
-        }
-    }
-
-    public void dispose() {
-        if (_writer != null) {
-            CloseElements();
-            _writer.close();
-            _document.Load(_fileName);
-        }
-        deleteTmpFile();
-    }
-
     private void writeVmDeviceInfo(VmDevice vmDevice) {
         _writer.WriteStartElement(OvfProperties.VMD_TYPE);
         _writer.WriteRaw(String.valueOf(vmDevice.getType()));
@@ -384,4 +350,9 @@
             _writer.WriteEndElement();
         }
     }
+
+    @Override
+    public String getStringRepresentation() {
+        return _writer.getStringXML();
+    }
 }


--
To view, visit http://gerrit.ovirt.org/11221
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8206580740be92cac7ffdbdc1c2f53ac0021d2e
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Michael Kublin <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to