diff options
author | Victor Mocanu | 2017-10-05 15:14:21 +0200 |
---|---|---|
committer | Victor Mocanu | 2017-10-05 15:14:21 +0200 |
commit | 054d78a59ebfc6c32a705f08cfd1d2fafe949472 (patch) | |
tree | 3c5061578ae2edc4d0a31a04cf1f44c6f0894b0a /src/main/java/org/openslx/util/vm/VboxConfig.java | |
parent | [VBox] working coppy in case further changes result in catastrophical failure (diff) | |
download | master-sync-shared-054d78a59ebfc6c32a705f08cfd1d2fafe949472.tar.gz master-sync-shared-054d78a59ebfc6c32a705f08cfd1d2fafe949472.tar.xz master-sync-shared-054d78a59ebfc6c32a705f08cfd1d2fafe949472.zip |
[VBox] improved the create new tag and insert attribute functions
added forgotten javadocs
Diffstat (limited to 'src/main/java/org/openslx/util/vm/VboxConfig.java')
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxConfig.java | 184 |
1 files changed, 147 insertions, 37 deletions
diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java index ef17f5c..a7c0e00 100644 --- a/src/main/java/org/openslx/util/vm/VboxConfig.java +++ b/src/main/java/org/openslx/util/vm/VboxConfig.java @@ -8,9 +8,12 @@ import java.util.ArrayList; import java.util.Map; import java.util.TreeMap; +import javax.annotation.processing.ProcessingEnvironment; +import javax.swing.plaf.basic.BasicToolBarUI.DockingListener; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.soap.Text; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; @@ -37,6 +40,10 @@ import org.xml.sax.SAXException; import org.openslx.util.vm.VmMetaData.DriveBusType; import org.openslx.util.vm.VmMetaData.HardDisk; +/** + * @author victorm + * + */ public class VboxConfig { private static final Logger LOGGER = Logger.getLogger( VboxConfig.class ); @@ -53,13 +60,15 @@ public class VboxConfig 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" }; + private static String[] blackList = { "ExtraData", "Adapter", "GuestProperties", "LPT", "StorageController", "FloppyImages", "DVDImages", "AttachedDevice" }; - /* + /** * constructor with input xml file * used to set the doc variable of this class when creating vm - * @args: file as File - the input xml File - * @return: no return value + * + * @param file as File - the input xml File + * @throws IOException + * @throws UnsupportedVirtualizerFormatException */ public VboxConfig( File file ) throws IOException, UnsupportedVirtualizerFormatException { @@ -81,11 +90,13 @@ public class VboxConfig displayName = new String(); } - /* + /** * constructor with input string from server * used to set the doc variable of this class when rebuilding the doc - * @args: filtered as String - sent from server - * @return: no return value + * + * @param filtered as String - sent from server + * @param length + * @throws IOException */ public VboxConfig( byte[] filtered, int length ) throws IOException { @@ -106,15 +117,24 @@ public class VboxConfig } } - // returns definition document + /** + * getter for the xmldoc + * + * @return definition document + */ public Document getConfigDoc() { return doc; } - // initialization function + /** + * initialization function + */ public void init() { + if ( doc.getChildNodes().item( 0 ).getNodeType() == 8 ) { + doc.removeChild( doc.getChildNodes().item( 0 ) ); + } try { setMachineName(); setOsType(); @@ -131,18 +151,23 @@ public class VboxConfig } } - - private boolean checkForPlaceholders(){ + /** + * Function checks if the placeholders are present + * + * @return true if the placeholders are present, false otherwise + */ + private boolean checkForPlaceholders() + { NodeList hdds = findANode( "HardDisk" ); - for ( int i = 0; i < hdds.getLength(); i++) { - Element hdd =(Element) hdds.item( i ); + 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; } } return false; } - + public void setMachineName() throws XPathExpressionException { String name = xPath.compile( displayNameExpression ).evaluate( this.doc ); @@ -179,7 +204,7 @@ public class VboxConfig String uuid = hddElement.getAttribute( "uuid" ); // do the loop the loop - // search in the xml thingy and give back the parent of the parent of the node that is called Image and has the given uuid + // search in the xml object and give back the parent of the parent of the node that is called Image and has the given uuid String pathToParent = givePathToStorageController( uuid ); XPathExpression attachedDevicesExpr = xPath.compile( pathToParent ); Object devicesResult = attachedDevicesExpr.evaluate( this.doc, XPathConstants.NODESET ); @@ -218,13 +243,36 @@ public class VboxConfig // placeholder for the CPU changeAttribute( "CPU", "count", "#some_fancy_CPU_place_holder" ); - } - public void changeAttribute( String targetTag, String attribute, String newValue ) - { - changeAttribute( targetTag, attribute, newValue, null, null ); + // add placeholder for the uuid of the virtual harddrive. + // must be added on 2 positions...in the HardDisk tag and the attachedDevice tag + // first find the uuid + NodeList hdds = findANode( "HardDisk" ); + for ( int i = 0; i < hdds.getLength(); i++ ) { + Element hdd = (Element)findANode( "HardDisk" ).item( i ); + String uuid = hdd.getAttribute( "uuid" ); + hdd.setAttribute( "uuid", "#some_fancy_HDDUUID_" + i + "_placeholder" ); + NodeList images = findANode( "Image" ); + Element image; + for ( int j = 0; j < images.getLength(); j++ ) { + if ( ( (Element)images.item( j ) ).getAttribute( "uuid" ).equals( uuid ) ) { + image = (Element)images.item( j ); + image.setAttribute( "uuid", "#some_fancy_HDDUUID_" + i + "_placeholder" ); + break; + } + } + } } + /** + * Function used to find a node in the document + * Function returnes a NodeList of Nodes...not just a Node...even when the wanted Node a single + * Node is + * you get a NodeList with just one element + * + * @param targetTag as String + * @return nodes as NodeList + */ public NodeList findANode( String targetTag ) { String path = ".//" + targetTag; @@ -240,6 +288,30 @@ public class VboxConfig return nodes; } + /** + * Function used to change the value of an attribute + * Use this function if you know the targetNode is unique + * + * @param targetTag + * @param attribute + * @param newValue + */ + public void changeAttribute( String targetTag, String attribute, String newValue ) + { + changeAttribute( targetTag, attribute, newValue, null, null ); + } + + /** + * Function used to change the value of an attribute + * Use this function if you are not sure if the targetNode is unique + * Use refAttr and refVal to address the right node + * + * @param targetTag + * @param targetAttr + * @param newValue + * @param refAttr + * @param refVal + */ public void changeAttribute( String targetTag, String targetAttr, String newValue, String refAttr, String refVal ) { NodeList nodes = findANode( targetTag ); @@ -261,31 +333,65 @@ 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" ); + public void addAttributeToNode( Node targetNode, String attrName, String value ) + { + if ( targetNode == null ) { + LOGGER.warn( "Knotten ist null; unterbrochen!" ); return; } - - String helperStr = ""; - if ( justTheTip ) { - helperStr = "/"; + ( (Element)targetNode ).setAttribute( attrName, value ); + } + + public Node addNewNode( String nameOfParent, String nameOfnewNode, boolean oneLiner ) + { + return addNewNode( nameOfParent, nameOfnewNode, oneLiner, null, null ); + } + + public Node addNewNode( String nameOfParent, String nameOfnewNode, boolean oneLiner, String refAttr, String refVal ) + { + Node parent = null; + NodeList posibleParents = findANode( nameOfParent ); + if ( posibleParents.getLength() > 1 ) { + // if we have more then 1 parent we need to have an sanityArg s.t. we insert our new attribute in the right tag + if ( refAttr == null ) { + LOGGER.warn( "Aktion würde an mehreren Knotten einen neuen Knoten hinzufügen; unterbrochen!" ); + return null; + } + for ( int i = 1; i < posibleParents.getLength(); i++ ) { + if ( ( (Element)posibleParents.item( i ) ).getAttribute( refAttr ).equals( refVal ) ) { + parent = posibleParents.item( i ); + break; + } + } + } else { + parent = posibleParents.item( 0 ); + } + + if ( parent == null ) { + LOGGER.warn( "Node: '" + nameOfParent + "' could not be found" ); + return null; + } + Element newTag = doc.createElement( nameOfnewNode ); + + if ( !oneLiner ) { + org.w3c.dom.Text a = doc.createTextNode( "\n" ); + newTag.appendChild( a ); } - Element newTag = doc.createElement( nameOfnewNode + helperStr ); parent.appendChild( newTag ); - //Element newT = doc.create - + + return newTag; } + // function removes a given child and the format childNode - private void removeNode( Node node ) { + 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 + + // HACK remove empty lines // format children have type 3 if ( previousSibling.getNodeType() == 3 ) { // the value of these Nodes are characters @@ -301,6 +407,7 @@ public class VboxConfig parent.removeChild( previousSibling ); } } + // cleanup part here private void removeBlackListedTags() throws XPathExpressionException { @@ -318,12 +425,16 @@ public class VboxConfig 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" ) ) { + if ( child.getChildNodes().item( 0 ).getNodeName().equals( "#text" ) && !child.getChildNodes().item( 1 ).getNodeName().equals( "#text" ) ) { removeNode( child.getChildNodes().item( 1 ) ); } continue; } + + if ( ( child.getTagName().equals( "StorageController" ) && !child.getAttribute( "name" ).equals( "Floppy" ) ) + || ( child.getTagName().equals( "AttachedDevice" ) && child.getAttribute( "type" ).equals( "HardDisk" ) ) ) { + 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 removeNode( child ); @@ -331,7 +442,6 @@ public class VboxConfig } } - //private void removeTag private String givePathToStorageController( String uuid ) { // StorageController is the parent of the parent of node with given uuid |