diff options
author | Victor Mocanu | 2017-08-08 12:49:01 +0200 |
---|---|---|
committer | Victor Mocanu | 2017-08-08 12:49:01 +0200 |
commit | 9318d3be9bc34a1550f9c370d51e1e3b68e953a5 (patch) | |
tree | 31e0b8f86aae1a53dada71d5eeb16c8c56cd9a1c /src/main | |
parent | [VBox] added handling for vdi files (diff) | |
download | master-sync-shared-9318d3be9bc34a1550f9c370d51e1e3b68e953a5.tar.gz master-sync-shared-9318d3be9bc34a1550f9c370d51e1e3b68e953a5.tar.xz master-sync-shared-9318d3be9bc34a1550f9c370d51e1e3b68e953a5.zip |
[VBox] changed and implemented the functionality of VboxConfig s.t. the process of reading a .vbox
file works and an config object is created
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/openslx/util/vm/DiskImage.java | 2 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxConfig.java | 216 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxMetaData.java | 193 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/vm/VmMetaData.java | 39 |
4 files changed, 234 insertions, 216 deletions
diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java index cc0be88..7590c6d 100644 --- a/src/main/java/org/openslx/util/vm/DiskImage.java +++ b/src/main/java/org/openslx/util/vm/DiskImage.java @@ -4,8 +4,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; import org.openslx.bwlp.thrift.iface.Virtualizer; import org.openslx.util.Util; diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java index 7d2d850..d64348b 100644 --- a/src/main/java/org/openslx/util/vm/VboxConfig.java +++ b/src/main/java/org/openslx/util/vm/VboxConfig.java @@ -2,35 +2,53 @@ package org.openslx.util.vm; import java.io.File; import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; import java.util.Map; import java.util.TreeMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +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; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.apache.log4j.Logger; +import org.apache.log4j.varia.StringMatchFilter; import org.openslx.util.vm.VmwareConfig.ConfigEntry; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import org.openslx.util.vm.VmMetaData.DriveBusType; +import org.openslx.util.vm.VmMetaData.HardDisk; public class VboxConfig { - - private static final Logger LOGGER = Logger.getLogger( VmwareConfig.class ); + private static final Logger LOGGER = Logger.getLogger( VboxConfig.class ); + XPath xPath = XPathFactory.newInstance().newXPath(); private Document doc = null; - - public VboxConfig() - { - // (void) - } + private String displayNameExpression = "/VirtualBox/Machine/@name"; + private String displayName; + private String osTypeExpression = "/VirtualBox/Machine/@OSType"; + private String osName = new String();; + + private String hddsExpression = "/VirtualBox/Machine/MediaRegistry/HardDisks/*"; + private ArrayList<HardDisk> hddsArray; + + // a black list of sorts of tags that need to be removed from the .vbox file + private static String[] blackList = {"ExtraData"}; + public VboxConfig ( File file ) throws IOException, UnsupportedVirtualizerFormatException { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); @@ -44,12 +62,190 @@ public class VboxConfig } } catch ( ParserConfigurationException | SAXException | IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + LOGGER.debug( "Could not parse .Vbox", e ); + throw new UnsupportedVirtualizerFormatException( "Konnte VBoxConfig nicht erstellen!" ); + } + displayName = new String(); + hddsArray = new ArrayList<HardDisk>(); + } + // returns definition document public Document getConfigDoc() { return doc; } + + // initialization function + public void init(){ + try { + setMachineName(); + setOsType(); + setHdds(); + removeBlackListedTags(); + } catch ( XPathExpressionException e ) { + LOGGER.debug( "Konnte VBoxConfig nicht initializieren", e ); + return; + } + } + + public void setMachineName() throws XPathExpressionException + { + String name = xPath.compile( displayNameExpression ).evaluate( this.doc ); + if ( !name.isEmpty() ) { + displayName = name; + } + } + + public void setOsType() throws XPathExpressionException + { + String os = xPath.compile( osTypeExpression ).evaluate( this.doc ); + if ( !os.isEmpty() ) { + LOGGER.debug( os); + osName = os; + } + } + + public void setHdds() throws XPathExpressionException + { + XPathExpression hddsExpr = xPath.compile( hddsExpression ); + Object result = hddsExpr.evaluate( this.doc , XPathConstants.NODESET ); + // take all the hdd nodes + NodeList nodes = (NodeList) result; + + // TODO find all HardDisk under HardDisks, give them to the hdds + // foreach hdd in the hddnodes do: + for ( int i = 0; i < nodes.getLength(); i++) { + // have the node + // take the uuid + // look under <AttachedDevice if found and do stuff with it + Element hddElement = (Element) nodes.item(i); + // read the filePath + String fileName = hddElement.getAttribute( "location" ); + // take the uuid + 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 hast the given uuid + String pathToParent = givePathToStorageController( uuid ); + XPathExpression attachedDevicesExpr = xPath.compile( pathToParent ); + Object devicesResult = attachedDevicesExpr.evaluate( this.doc , XPathConstants.NODESET ); + NodeList devicesNodes = (NodeList) devicesResult; + + // TODO -- ehm...should only have 1 element...what do when there are more? + if ( devicesNodes.getLength() > 1 ) return; + + Element deviceElement = (Element) devicesNodes.item( 0 ); + String controllerDevice = deviceElement.getAttribute( "type" ); + String bus = deviceElement.getAttribute( "name" ); + + DriveBusType busType = null; + if ( bus.equals( "IDE" ) ) { + busType = DriveBusType.IDE; + } else if ( bus.equals( "SCSI" ) ) { + busType = DriveBusType.SCSI; + } else if ( bus.equals( "SATA" ) ) { + busType = DriveBusType.SATA; + } + // add them together + hddsArray.add( new HardDisk( controllerDevice, busType, fileName ) ); + } + } + + + public void changeAttribute(String targetTag, String attribute, String newValue) + { + changeAttribute(targetTag, attribute, newValue, null, null); + } + + public void changeAttribute(String targetTag, String targetAttr, String newValue, String refAttr, String refVal) + { + String path = ".//"+targetTag; + XPathExpression expr; + NodeList nodes = null; + try { + expr = xPath.compile( path ); + Object nodesObject = expr.evaluate( this.doc , XPathConstants.NODESET ); + nodes = (NodeList) nodesObject; + } catch ( XPathExpressionException e ) { + e.printStackTrace(); + } + + for ( int i = 0; i < nodes.getLength(); i++) + { + Element element = (Element) nodes.item(i); + if (refAttr != null && refVal != null) + { + if (element.getAttribute( refAttr ).equals( refVal )) + { + element.setAttribute( targetAttr, newValue ); + break; + } + } + else + { + if ( nodes.getLength() > 1) + { + LOGGER.warn( "Aktion würde an mehreren Knotten die Value ändern; unterbrochen!" ); + return; + } + LOGGER.debug( element.toString() ); + element.setAttribute( targetAttr, newValue ); + } + } + } + + // cleanup part here + private void removeBlackListedTags() throws XPathExpressionException + { + // iterate over the blackList + for ( String blackedTag : blackList ) { + String blackedExpression = ".//"+blackedTag; + XPathExpression blackedExpr = xPath.compile( blackedExpression ); + Object blackedResult = blackedExpr.evaluate( this.doc , XPathConstants.NODESET ); + NodeList blackedNodes = (NodeList) blackedResult; + for ( int i = 0; i < blackedNodes.getLength(); i++) { + Element blackedListedElement = (Element) blackedNodes.item(i); + blackedListedElement.getParentNode().removeChild( blackedListedElement ); + } + } + } + + private String givePathToStorageController( String uuid ) + { + // StorageController is the parent of the parent of node with given uuid + return "//Image[contains(@uuid, \'"+uuid+"\')]/../.."; + } + + public String getDisplayName() + { + return displayName; + } + + public String getOsName() + { + return osName; + } + + public ArrayList<HardDisk> getHdds() + { + return hddsArray; + } + + public String toString() + { + try { + StringWriter sw = new StringWriter(); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + + transformer.transform(new DOMSource(doc), new StreamResult(sw)); + return sw.toString(); + } catch (Exception ex) { + 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 ceb8734..3cc54a5 100644 --- a/src/main/java/org/openslx/util/vm/VboxMetaData.java +++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java @@ -1,221 +1,64 @@ package org.openslx.util.vm; -import java.io.Console; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; 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; +/* --TODO needed later 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; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; - -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; -import org.w3c.dom.Node; -import org.w3c.dom.Element; -import org.apache.log4j.Logger; -import org.openslx.bwlp.thrift.iface.MasterServer.AsyncProcessor.invalidateSession; -import org.openslx.util.vm.VmMetaData.DriveBusType; -import org.openslx.util.vm.VmMetaData.HardDisk; +*/ 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( VmwareMetaData.class ); - // private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" ); + 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 { super( osList ); - // TODO Auto-generated constructor stub - // TODO open vbox file read it - // TODO try to open vmdk file and read it - // TODO this.config = new VboxConfig( file ); init(); } - private String givePathToStorageController( String uuid ) { - // StorageController is the parent of the parent of node with given uuid - return "//Image[contains(@uuid, \'"+uuid+"\')]/../.."; - } private void init() { - XPath xPath = XPathFactory.newInstance().newXPath(); - - String displayNameExpression = "/VirtualBox/Machine/@name"; - String osTypeExpression = "/VirtualBox/Machine/@OSType"; - String hddsExpression = "/VirtualBox/Machine/MediaRegistry/HardDisks/*"; + //LOGGER.debug( "text" ); + this.config.init(); - try { - // go to the hdds - XPathExpression hddsExpr = xPath.compile( hddsExpression ); - Object result = hddsExpr.evaluate( config.getConfigDoc(), XPathConstants.NODESET ); - // take all the hdd nodes - NodeList nodes = (NodeList) result; - - // TODO find all HardDisk under HardDisks, give them to the hdds - // foreach hdd in the hddnodes do: - for ( int i = 0; i < nodes.getLength(); i++) { - // have the node - // take the uuid - // look under <AttachedDevice if found and do stuff with it - Element hddElement = (Element) nodes.item(i); - // read the filePath - String fileName = hddElement.getAttribute( "location" ); - // take the uuid - 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 hast the given uuid - String pathToParent = givePathToStorageController( uuid ); - XPathExpression attachedDevicesExpr = xPath.compile( pathToParent ); - Object devicesResult = attachedDevicesExpr.evaluate( config.getConfigDoc(), XPathConstants.NODESET ); - NodeList devicesNodes = (NodeList) devicesResult; - - // TODO -- ehm...should only have 1 element...what do when there are more? - if ( devicesNodes.getLength() > 1 ) return; - - Element deviceElement = (Element) devicesNodes.item( 0 ); - String controllerDevice = deviceElement.getAttribute( "type" ); - String bus = deviceElement.getAttribute( "name" ); - - DriveBusType busType = null; - if ( bus.equals( "IDE" ) ) { - busType = DriveBusType.IDE; - } else if ( bus.equals( "SCSI" ) ) { - busType = DriveBusType.SCSI; - } else if ( bus.equals( "SATA" ) ) { - busType = DriveBusType.SATA; - } - - // add them together - hdds.add( new HardDisk( controllerDevice, busType, fileName ) ); - } - /*NodeList nodeList = (NodeList) xPath.compile( expression ).evaluate( config.getConfigDoc(), XPathConstants.NODESET); - for (int i = 0; i < nodeList.getLength(); i++) { - System.out.println( nodeList.item( i ).getNodeName()); - }*/ - - - - String name = xPath.compile( displayNameExpression ).evaluate( config.getConfigDoc()); - - if ( !name.isEmpty() ) { - displayName = name; - } - - String osType = xPath.compile( osTypeExpression ).evaluate( config.getConfigDoc() ); - - if ( !osType.isEmpty() ) { - setOs( "virtualbox", osType ); - } - - removeBlackListedTags(xPath); - changeAttribute(xPath, "Memory", "RAMSize", "1024"); - - WriteToFile(); - //OperatingSystem localOperatingSystem = getOs(); - - //OperatingSystem localOperatingSystem2 = getOs(); - - /* if(localOperatingSystem == null) { - System.out.println("localOperatingSystem is null and localOperatingSystem2 is also null"); - }*/ - - /*NodeList nodeList = (NodeList)xPath.compile( expression ).evaluate( config.getConfigDoc(), XPathConstants.NODESET); - for (int i = 0; i < nodeList.getLength(); i++) { - if (nodeList.item( i ).getNodeName().equals( "Machine" )) { - for (int j = 0; j < nodeList.item( i ).getAttributes().getLength(); j++) - System.out.println( nodeList.item( i ).getAttributes().item( j )); - } - */ - //} - //System.out.println( name ); - } 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(); - } - } + displayName = config.getDisplayName(); + + setOs( "virtualbox", config.getOsName() ); - - // 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 changeAttribute(XPath xPath, String targetTag, String attribute, String newValue) throws XPathExpressionException - { - String path = ".//"+targetTag; - XPathExpression blackedExpr = xPath.compile( path ); - Object nodesObject = blackedExpr.evaluate( config.getConfigDoc(), XPathConstants.NODESET ); - NodeList nodes = (NodeList) nodesObject; - for ( int i = 0; i < nodes.getLength(); i++) { - Element element = (Element) nodes.item(i); - System.out.println( element.getAttribute( attribute ) ); - element.setAttribute( attribute, newValue ); - System.out.println( element.getAttributeNode( attribute ) ); + for ( HardDisk hardDisk : config.getHdds() ) { + hdds.add( hardDisk ); } } - + /* --TODO will be needed later again 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() { - return new Virtualizer( "vbox", "VirtualBox" ); + return new Virtualizer( "virtualbox", "VirtualBox" ); } @Override @@ -233,7 +76,9 @@ public class VboxMetaData extends VmMetaData public byte[] getFilteredDefinitionArray() { // TODO Auto-generated method stub - return null; + return config.toString().getBytes( StandardCharsets.UTF_8 ); } - + + + // 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 b9c21da..f1315d3 100644 --- a/src/main/java/org/openslx/util/vm/VmMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmMetaData.java @@ -10,6 +10,7 @@ import java.util.Map.Entry; import javax.swing.JOptionPane; +import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.Virtualizer; @@ -19,6 +20,7 @@ import org.openslx.bwlp.thrift.iface.Virtualizer; */ public abstract class VmMetaData { + private static final Logger LOGGER = Logger.getLogger( VmMetaData.class ); /* * Helper types */ @@ -55,7 +57,6 @@ public abstract class VmMetaData private OperatingSystem os = null; protected String displayName = null; - /* * Guettas */ @@ -134,9 +135,9 @@ public abstract class VmMetaData } } - if(!x) { - System.out.println( "os with " + virtId + " " + virtOsId + " is not in list"); - } + /*if(!x) { + //LOGGER.debug( "os with " + virtId + " " + virtOsId + " is not in list"); + }*/ this.os = lazyMatch; } @@ -152,45 +153,23 @@ public abstract class VmMetaData public static VmMetaData getInstance(List<OperatingSystem> osList, File file) throws IOException { - - // throw new IOException(); - - // TODO file object check nach "Pattern" - // TODO make it genericsher - - // unknown file > try to init vmware, vbox, ... until one works - - - - - - // String fileName = file.getName(); - + VmMetaData meta = null; try { meta = new VmwareMetaData(osList, file); } catch ( UnsupportedVirtualizerFormatException e ) { - // TODO Auto-generated catch block - // e.printStackTrace(); + LOGGER.debug( "datei war nicht .vmx; versuche mit VBox"); try { meta = new VboxMetaData(osList, file); } catch (UnsupportedVirtualizerFormatException ex) { - // TODO: handle exception - ex.printStackTrace(); + LOGGER.debug( "datei war nicht .vbox; unterbrochen!" , ex); + // TODO ok so or throw new IOException? } } - if (meta != null) { return meta; } - - - - /*if (meta != null) { - return meta; - }*/ - return null; } } |