diff options
author | Victor Mocanu | 2017-07-12 15:11:29 +0200 |
---|---|---|
committer | Victor Mocanu | 2017-07-12 15:11:29 +0200 |
commit | 60781129a3f2c964bd454379f3706562d3824d71 (patch) | |
tree | a2504dbe8de478037dec80d8e2b60824e72ce3e4 /src/main/java/org/openslx | |
parent | [VBox] Added the VBoxConfig class and done some work on the VBoxMetaData class (diff) | |
download | master-sync-shared-60781129a3f2c964bd454379f3706562d3824d71.tar.gz master-sync-shared-60781129a3f2c964bd454379f3706562d3824d71.tar.xz master-sync-shared-60781129a3f2c964bd454379f3706562d3824d71.zip |
[VBOX] exceptionhandling and added method to delete unwanted nodes in the config file
Diffstat (limited to 'src/main/java/org/openslx')
-rw-r--r-- | src/main/java/org/openslx/util/vm/DiskImage.java | 54 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxMetaData.java | 49 |
2 files changed, 82 insertions, 21 deletions
diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java index 5f9756f..94d5d2c 100644 --- a/src/main/java/org/openslx/util/vm/DiskImage.java +++ b/src/main/java/org/openslx/util/vm/DiskImage.java @@ -54,31 +54,43 @@ public class DiskImage public final int hwVersion; public DiskImage( File disk ) throws FileNotFoundException, IOException, - UnknownImageFormatException, UnsupportedVirtualizerFormatException + UnknownImageFormatException { - // For now we only support VMDK... try ( RandomAccessFile file = new RandomAccessFile( disk, "r" ) ) { - if ( file.readInt() != VMDK_MAGIC ) - throw new UnknownImageFormatException(); - file.seek( 512 ); - byte[] buffer = new byte[ 2048 ]; - file.readFully( buffer ); - VmwareConfig config = new VmwareConfig( buffer, findNull( buffer ) ); - subFormat = config.get( "createType" ); - String parent = config.get( "parentCID" ); - this.isStandalone = isStandaloneCreateType( subFormat, parent ); - this.isCompressed = subFormat != null - && subFormat.equalsIgnoreCase( "streamOptimized" ); - this.isSnapshot = parent != null - && !parent.equalsIgnoreCase( "ffffffff" ); - this.format = ImageFormat.VMDK; - String hwv = config.get( "ddb.virtualHWVersion" ); - if (hwv == null ) { - this.hwVersion = 10; - } else { - this.hwVersion = Util.parseInt( hwv, 10 ); + // vmdk + if ( file.readInt() == VMDK_MAGIC ) { + file.seek( 512 ); + byte[] buffer = new byte[ 2048 ]; + file.readFully( buffer ); + VmwareConfig config; + try { + config = new VmwareConfig( buffer, findNull( buffer ) ); + } catch ( UnsupportedVirtualizerFormatException e ) { + config = null; + } + if (config != null) { + subFormat = config.get( "createType" ); + String parent = config.get( "parentCID" ); + this.isStandalone = isStandaloneCreateType( subFormat, parent ); + this.isCompressed = subFormat != null + && subFormat.equalsIgnoreCase( "streamOptimized" ); + this.isSnapshot = parent != null + && !parent.equalsIgnoreCase( "ffffffff" ); + this.format = ImageFormat.VMDK; + String hwv = config.get( "ddb.virtualHWVersion" ); + if (hwv == null ) { + this.hwVersion = 10; + } else { + this.hwVersion = Util.parseInt( hwv, 10 ); + } + return; + } } + // vdi + file.seek( 0 ); + // TODO } + throw new UnknownImageFormatException(); } private int findNull( byte[] buffer ) diff --git a/src/main/java/org/openslx/util/vm/VboxMetaData.java b/src/main/java/org/openslx/util/vm/VboxMetaData.java index 0b41fd7..ea6ea21 100644 --- a/src/main/java/org/openslx/util/vm/VboxMetaData.java +++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java @@ -1,14 +1,25 @@ package org.openslx.util.vm; +import java.io.Console; import java.io.File; import java.io.IOException; import java.util.List; +import java.util.regex.Pattern; import javax.lang.model.element.VariableElement; +import javax.naming.spi.DirStateFactory.Result; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; @@ -34,6 +45,9 @@ public class VboxMetaData extends VmMetaData // private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" ); private final VboxConfig config; + + // a black list of sorts of tags that need to be removed from the .vbox file + private static String[] blackList = {"ExtraData"}; public VboxMetaData( List<OperatingSystem> osList, File file ) throws IOException, UnsupportedVirtualizerFormatException { @@ -124,6 +138,9 @@ public class VboxMetaData extends VmMetaData setOs( "virtualbox", osType ); } + RemoveBlackListedTags(xPath); + + WriteToFile(); //OperatingSystem localOperatingSystem = getOs(); //OperatingSystem localOperatingSystem2 = getOs(); @@ -144,9 +161,41 @@ public class VboxMetaData extends VmMetaData } catch ( XPathExpressionException e ) { // TODO Auto-generated catch block e.printStackTrace(); + } catch ( TransformerFactoryConfigurationError e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( TransformerException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); } } + + // cleanup part here + private void RemoveBlackListedTags(XPath xPath) throws XPathExpressionException { + // iterate over the blackList + for ( String blackedTag : blackList ) { + String blackedExpression = ".//"+blackedTag; + XPathExpression blackedExpr = xPath.compile( blackedExpression ); + Object blackedResult = blackedExpr.evaluate( config.getConfigDoc(), XPathConstants.NODESET ); + NodeList blackedNodes = (NodeList) blackedResult; + for ( int i = 0; i < blackedNodes.getLength(); i++) { + Element blackedListedElement = (Element) blackedNodes.item(i); + System.out.println( blackedListedElement.toString() ); + blackedListedElement.getParentNode().removeChild( blackedListedElement ); + } + } + } + + + private void WriteToFile() throws TransformerFactoryConfigurationError, TransformerException + { + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + StreamResult output = new StreamResult(new File( "output.xml" )); + Source input = new DOMSource( config.getConfigDoc() ); + + transformer.transform( input, output ); + } @Override public Virtualizer getVirtualizer() { |