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/java/org/openslx/util/vm/VboxMetaData.java | |
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/java/org/openslx/util/vm/VboxMetaData.java')
-rw-r--r-- | src/main/java/org/openslx/util/vm/VboxMetaData.java | 193 |
1 files changed, 19 insertions, 174 deletions
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... } |