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 | |
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')
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxConfig.java | 113 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxMetaData.java | 44 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VmMetaData.java | 17 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VmwareMetaData.java | 15 |
4 files changed, 131 insertions, 58 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 ); } } + } diff --git a/src/main/java/org/openslx/util/vm/VboxMetaData.java b/src/main/java/org/openslx/util/vm/VboxMetaData.java index dac8e78..da6dc33 100644 --- a/src/main/java/org/openslx/util/vm/VboxMetaData.java +++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; +import javax.print.attribute.standard.PrinterMessageFromOperator; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; @@ -13,11 +14,15 @@ import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +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; + public class VboxMetaData extends VmMetaData { + private static final Logger LOGGER = Logger.getLogger( VboxMetaData.class ); private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" ); private final VboxConfig config; @@ -34,9 +39,10 @@ public class VboxMetaData extends VmMetaData public VboxMetaData( List<OperatingSystem> osList, byte[] vmContent, int length ) throws IOException, UnsupportedVirtualizerFormatException { super( osList ); - this.config = new VboxConfig( vmContent.toString(), length ); - + this.config = new VboxConfig( vmContent, length ); + setTypeOf(); init(); + } private void init() @@ -53,7 +59,7 @@ public class VboxMetaData extends VmMetaData try { WriteToFile(); } catch ( TransformerFactoryConfigurationError | TransformerException e ) { - // TODO Auto-generated catch block + // WriteToFile exceptions here...not important for the the LOGGER e.printStackTrace(); } } @@ -95,17 +101,25 @@ public class VboxMetaData extends VmMetaData @Override public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ) { - // TODO Auto-generated method stub - return false; + config.changeAttribute( "HardDisk", "location", diskImagePath ); + return true; } @Override public boolean addDefaultNat() { - // TODO Auto-generated method stub - return false; + config.addSomeNewNode( "Adapter", "NAT", false ); + return true; + } + + public void reWrite() { + try { + WriteToFile(); + } catch ( TransformerFactoryConfigurationError | TransformerException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - @Override public void setOs( String vendorOsId ) { @@ -116,15 +130,15 @@ public class VboxMetaData extends VmMetaData @Override public boolean addDisplayName( String name ) { - // TODO Auto-generated method stub - return false; + config.changeAttribute( "Machine", "name", name ); + return true; } @Override public boolean addRam( int mem ) { - // TODO Auto-generated method stub - return false; + config.changeAttribute( "Memory", "RAMSize", Integer.toString( mem ) ); + return true; } @Override @@ -141,5 +155,11 @@ public class VboxMetaData extends VmMetaData return false; } + @Override + public void setTypeOf() + { + typeOf = TypeOf.VBOX; + } + // TODO: the part after getting the byte[] from the Server and doing stuff with it...SoundCard types etc... } diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java index 7e67fe6..27b0983 100644 --- a/src/main/java/org/openslx/util/vm/VmMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmMetaData.java @@ -8,8 +8,10 @@ import java.util.Collections; import java.util.List; import java.util.Map.Entry; +import javax.print.attribute.standard.PrinterMessageFromOperator; import javax.swing.JOptionPane; +import org.apache.log4j.FileAppender; import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.Virtualizer; @@ -25,6 +27,12 @@ public abstract class VmMetaData * Helper types */ + public static enum TypeOf { + VMX, + VBOX; + } + + public static TypeOf typeOf = null; public static enum DriveBusType { SCSI, @@ -140,7 +148,8 @@ public abstract class VmMetaData }*/ this.os = lazyMatch; } - + + public abstract void reWrite(); public abstract Virtualizer getVirtualizer(); public abstract void enableUsb(boolean enabled); @@ -208,4 +217,10 @@ public abstract class VmMetaData public abstract void addFloppy( int index, String image, boolean readOnly ); public abstract boolean addCdrom( String image ); + + public abstract void setTypeOf ( ); + + public TypeOf getTypeOf () { + return typeOf; + } } diff --git a/src/main/java/org/openslx/util/vm/VmwareMetaData.java b/src/main/java/org/openslx/util/vm/VmwareMetaData.java index 4e7d926..4a5266c 100644 --- a/src/main/java/org/openslx/util/vm/VmwareMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmwareMetaData.java @@ -17,6 +17,7 @@ import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.Virtualizer; import org.openslx.util.Util; +import org.openslx.util.vm.VmMetaData.TypeOf; import org.openslx.util.vm.VmwareConfig.ConfigEntry; public class VmwareMetaData extends VmMetaData @@ -56,6 +57,7 @@ public class VmwareMetaData extends VmMetaData { super( osList ); this.config = new VmwareConfig( vmxContent, length ); // still unfiltered + setTypeOf(); init(); // now filtered } @@ -553,4 +555,17 @@ public class VmwareMetaData extends VmMetaData } return EthernetDevType.AUTO; } + + @Override + public void setTypeOf() + { + typeOf = TypeOf.VMX; + } + + @Override + public void reWrite() + { + // TODO Auto-generated method stub + + } } |