summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx
diff options
context:
space:
mode:
authorVictor Mocanu2017-10-05 15:14:21 +0200
committerVictor Mocanu2017-10-05 15:14:21 +0200
commit054d78a59ebfc6c32a705f08cfd1d2fafe949472 (patch)
tree3c5061578ae2edc4d0a31a04cf1f44c6f0894b0a /src/main/java/org/openslx
parent[VBox] working coppy in case further changes result in catastrophical failure (diff)
downloadmaster-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')
-rw-r--r--src/main/java/org/openslx/util/vm/VboxConfig.java184
-rw-r--r--src/main/java/org/openslx/util/vm/VboxMetaData.java12
2 files changed, 154 insertions, 42 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
diff --git a/src/main/java/org/openslx/util/vm/VboxMetaData.java b/src/main/java/org/openslx/util/vm/VboxMetaData.java
index da6dc33..60e760a 100644
--- a/src/main/java/org/openslx/util/vm/VboxMetaData.java
+++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java
@@ -18,6 +18,7 @@ import org.apache.log4j.Logger;
import org.apache.log4j.rewrite.ReflectionRewritePolicy;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
+import org.w3c.dom.Node;
public class VboxMetaData extends VmMetaData
@@ -57,6 +58,7 @@ public class VboxMetaData extends VmMetaData
hdds.add( hardDisk );
}
try {
+ addFloppy( 0, null, true );
WriteToFile();
} catch ( TransformerFactoryConfigurationError | TransformerException e ) {
// WriteToFile exceptions here...not important for the the LOGGER
@@ -94,7 +96,6 @@ public class VboxMetaData extends VmMetaData
@Override
public byte[] getFilteredDefinitionArray()
{
- // TODO Auto-generated method stub
return config.toString().getBytes( StandardCharsets.UTF_8 );
}
@@ -108,7 +109,7 @@ public class VboxMetaData extends VmMetaData
@Override
public boolean addDefaultNat()
{
- config.addSomeNewNode( "Adapter", "NAT", false );
+ config.addNewNode( "Adapter", "NAT", false );
return true;
}
@@ -144,7 +145,10 @@ public class VboxMetaData extends VmMetaData
@Override
public void addFloppy( int index, String image, boolean readOnly )
{
- // TODO Auto-generated method stub
+
+ if ( image == null ) {
+ } else {
+ }
}
@@ -160,6 +164,4 @@ public class VboxMetaData extends VmMetaData
{
typeOf = TypeOf.VBOX;
}
-
- // TODO: the part after getting the byte[] from the Server and doing stuff with it...SoundCard types etc...
}