diff options
author | Victor Mocanu | 2017-10-02 13:34:41 +0200 |
---|---|---|
committer | Victor Mocanu | 2017-10-02 13:34:41 +0200 |
commit | 51ebb850c035e34446acff8d6ddcc11fcf6bcdaa (patch) | |
tree | 0c00429daa367e4e6f521d9a0492b231a30830de /src/main/java/org/openslx/util/vm/VboxConfig.java | |
parent | [VBox] changes to the VmWrapper class, it asks now for an VmMetaData child, i... (diff) | |
download | master-sync-shared-51ebb850c035e34446acff8d6ddcc11fcf6bcdaa.tar.gz master-sync-shared-51ebb850c035e34446acff8d6ddcc11fcf6bcdaa.tar.xz master-sync-shared-51ebb850c035e34446acff8d6ddcc11fcf6bcdaa.zip |
[VBox] working coppy in case further changes result in catastrophical failure
Diffstat (limited to 'src/main/java/org/openslx/util/vm/VboxConfig.java')
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxConfig.java | 113 |
1 files changed, 68 insertions, 45 deletions
diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java index d009bc1..ef17f5c 100644 --- a/src/main/java/org/openslx/util/vm/VboxConfig.java +++ b/src/main/java/org/openslx/util/vm/VboxConfig.java @@ -22,10 +22,12 @@ import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; +import org.apache.commons.logging.Log; import org.apache.log4j.Logger; import org.apache.log4j.varia.StringMatchFilter; import org.junit.runners.ParentRunner; import org.openslx.util.vm.VmwareConfig.ConfigEntry; +import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -48,7 +50,7 @@ public class VboxConfig private String osName = new String();; private String hddsExpression = "/VirtualBox/Machine/MediaRegistry/HardDisks/*"; - private ArrayList<HardDisk> hddsArray; + private ArrayList<HardDisk> hddsArray = new ArrayList<HardDisk>();; // a black list of sorts of tags that need to be removed from the .vbox file private static String[] blackList = { "ExtraData", "Adapter", "GuestProperties", "LPT" }; @@ -73,11 +75,10 @@ public class VboxConfig } } catch ( ParserConfigurationException | SAXException | IOException e ) { - LOGGER.debug( "Could not parse .Vbox", e ); + LOGGER.warn( "Could not parse .Vbox", e ); throw new UnsupportedVirtualizerFormatException( "Konnte VBoxConfig nicht erstellen!" ); } displayName = new String(); - hddsArray = new ArrayList<HardDisk>(); } /* @@ -86,22 +87,22 @@ public class VboxConfig * @args: filtered as String - sent from server * @return: no return value */ - public VboxConfig( String filtered, int length ) throws IOException + public VboxConfig( byte[] filtered, int length ) throws IOException { // TODO test this spoon try { + String filteredString = new String( filtered ); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); //dbFactory.setValidating( true ); //dbFactory.setIgnoringElementContentWhitespace( true ); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - InputSource is = new InputSource( new StringReader( filtered ) ); + InputSource is = new InputSource( new StringReader( filteredString ) ); doc = dBuilder.parse( is ); } catch ( ParserConfigurationException | SAXException e ) { - LOGGER.debug( "Could not recreate the dom", e ); + LOGGER.warn( "Could not recreate the dom", e ); } } @@ -117,36 +118,31 @@ public class VboxConfig try { setMachineName(); setOsType(); + if ( checkForPlaceholders() ) { + LOGGER.debug( "we have placeholders" ); + return; + } setHdds(); removeBlackListedTags(); addPlaceHolders(); - //removeEmptyText(); } catch ( XPathExpressionException e ) { LOGGER.debug( "Konnte VBoxConfig nicht initializieren", e ); return; } } - public void removeEmptyText() - { - try { - XPathFactory xpathFactory = XPathFactory.newInstance(); - - XPathExpression xpathExp = xpathFactory.newXPath().compile( "//text()[normalize-space(.) = '']" ); - NodeList emptyTextNodes = (NodeList)xpathExp.evaluate( doc, XPathConstants.NODESET ); - // Remove each empty text node from document. - for ( int i = 0; i < emptyTextNodes.getLength(); ++i ) { - Node emptyTextNode = emptyTextNodes.item( i ); - emptyTextNode.getParentNode().removeChild( emptyTextNode ); + private boolean checkForPlaceholders(){ + NodeList hdds = findANode( "HardDisk" ); + for ( int i = 0; i < hdds.getLength(); i++) { + Element hdd =(Element) hdds.item( i ); + if ( hdd.getAttribute( "location" ).equals( "#some_fancy_HDD_place_holder" ) ) { + return true; } - - } catch ( XPathExpressionException e ) { - LOGGER.debug( "Could not remove white spaces...", e ); } - + return false; } - + public void setMachineName() throws XPathExpressionException { String name = xPath.compile( displayNameExpression ).evaluate( this.doc ); @@ -265,6 +261,46 @@ public class VboxConfig } } + public void addSomeNewNode( String nameOfParent ,String nameOfnewNode , boolean justTheTip) { + Node parent = findANode( nameOfParent ).item( 0 ); + if ( !parent.getNodeName().equals( nameOfParent )) { + LOGGER.warn( "parent could not be found... exiting" ); + return; + } + + String helperStr = ""; + if ( justTheTip ) { + helperStr = "/"; + } + Element newTag = doc.createElement( nameOfnewNode + helperStr ); + parent.appendChild( newTag ); + //Element newT = doc.create + + } + // function removes a given child and the format childNode + private void removeNode( Node node ) { + Node parent = node.getParentNode(); + // this node here is usually a type3 Node used only for the formating of the vbox file + Node previousSibling = node.getPreviousSibling(); + + parent.removeChild( node ); + + // HACK remove empty lines + // format children have type 3 + if ( previousSibling.getNodeType() == 3 ) { + // the value of these Nodes are characters + String tmp = previousSibling.getNodeValue(); + boolean shouldDelete = true; + for ( int foo = 0; foo < tmp.length(); foo++ ) { + if ( !Character.isWhitespace( tmp.charAt( foo ) ) ) { + shouldDelete = false; + break; + } + } + if ( shouldDelete ) + parent.removeChild( previousSibling ); + } + } // cleanup part here private void removeBlackListedTags() throws XPathExpressionException { @@ -278,33 +314,19 @@ public class VboxConfig for ( int i = 0; i < blackedNodes.getLength(); i++ ) { // get the child node Element child = (Element)blackedNodes.item( i ); - // get the parent node - Node parent = (Node)child.getParentNode(); // remove child if ( child.getTagName().equals( "Adapter" ) && child.getAttribute( "enabled" ).equals( "true" ) ) { + // we need to remove the children of the active network adapter + // these are the mode of network connection and disabled modes...they go together -> see wiki + if ( child.getChildNodes().item( 0 ).getNodeName().equals( "#text" ) && + !child.getChildNodes().item( 1 ).getNodeName().equals( "#text" ) ) { + removeNode( child.getChildNodes().item( 1 ) ); + } continue; } - // the structure of the xml Document is achieved through children nodes that are made up of just /n and spaces // when a child node is deleted we get an empty line there the old child node used to be - Node prevChild = child.getPreviousSibling(); - parent.removeChild( child ); - - // HACK remove empty lines - // format children have type 3 - if ( prevChild.getNodeType() == 3 ) { - // the value of these Nodes are characters - String tmp = prevChild.getNodeValue(); - boolean shouldDelete = true; - for ( int foo = 0; foo < tmp.length(); foo++ ) { - if ( !Character.isWhitespace( tmp.charAt( foo ) ) ) { - shouldDelete = false; - break; - } - } - if ( shouldDelete ) - parent.removeChild( prevChild ); - } + removeNode( child ); } } } @@ -348,4 +370,5 @@ public class VboxConfig throw new RuntimeException( "Error converting to String", ex ); } } + } |