diff options
author | Victor Mocanu | 2017-06-22 14:48:07 +0200 |
---|---|---|
committer | Victor Mocanu | 2017-06-22 14:48:07 +0200 |
commit | f42c56fd0792d1b34313627a28f5202545f1abe5 (patch) | |
tree | 1564882ba54d2934542f92e53a96349dd0663490 /src/main/java/org | |
parent | [VBOX] restructured generic VmMetaData for comming VBox support (diff) | |
download | master-sync-shared-f42c56fd0792d1b34313627a28f5202545f1abe5.tar.gz master-sync-shared-f42c56fd0792d1b34313627a28f5202545f1abe5.tar.xz master-sync-shared-f42c56fd0792d1b34313627a28f5202545f1abe5.zip |
[VBox] Added the VBoxConfig class and done some work on the VBoxMetaData class
Diffstat (limited to 'src/main/java/org')
5 files changed, 214 insertions, 12 deletions
diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java new file mode 100644 index 0000000..7d2d850 --- /dev/null +++ b/src/main/java/org/openslx/util/vm/VboxConfig.java @@ -0,0 +1,55 @@ +package org.openslx.util.vm; + +import java.io.File; +import java.io.IOException; +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.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.apache.log4j.Logger; +import org.openslx.util.vm.VmwareConfig.ConfigEntry; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class VboxConfig +{ + + private static final Logger LOGGER = Logger.getLogger( VmwareConfig.class ); + + private Document doc = null; + + public VboxConfig() + { + // (void) + } + + public VboxConfig ( File file ) throws IOException, UnsupportedVirtualizerFormatException { + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + + doc = dBuilder.parse( file ); + + // TODO - does this test suffice?? + if ( !doc.getDocumentElement().getNodeName().equals( "VirtualBox" ) ) { + throw new UnsupportedVirtualizerFormatException( "not VirtualBox image format" ); + } + + } catch ( ParserConfigurationException | SAXException | IOException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public Document getConfigDoc() { + return doc; + } +} diff --git a/src/main/java/org/openslx/util/vm/VboxMetaData.java b/src/main/java/org/openslx/util/vm/VboxMetaData.java index 5fa5cba..0b41fd7 100644 --- a/src/main/java/org/openslx/util/vm/VboxMetaData.java +++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java @@ -1,18 +1,150 @@ package org.openslx.util.vm; + import java.io.File; +import java.io.IOException; import java.util.List; +import javax.lang.model.element.VariableElement; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +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 { - public VboxMetaData( List<OperatingSystem> osList, File file ) throws UnsupportedVirtualizerFormatException + private static final Logger LOGGER = Logger.getLogger( VmwareMetaData.class ); + // private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" ); + + private final VboxConfig config; + + 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/*"; + + 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 ); + } + + //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(); + } } @Override @@ -26,14 +158,12 @@ public class VboxMetaData extends VmMetaData public void enableUsb( boolean enabled ) { // TODO Auto-generated method stub - } @Override public void applySettingsForLocalEdit() { // TODO Auto-generated method stub - } @Override public byte[] getFilteredDefinitionArray() @@ -41,5 +171,5 @@ public class VboxMetaData extends VmMetaData // TODO Auto-generated method stub return null; } - + } diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java index 7530039..b9c21da 100644 --- a/src/main/java/org/openslx/util/vm/VmMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmMetaData.java @@ -117,6 +117,7 @@ public abstract class VmMetaData protected final void setOs( String virtId, String virtOsId ) { OperatingSystem lazyMatch = null; + boolean x = false; for ( OperatingSystem os : osList ) { if ( os.getVirtualizerOsId() == null ) continue; @@ -125,12 +126,17 @@ public abstract class VmMetaData continue; if ( entry.getKey().equals( virtId ) ) { this.os = os; + x = true; return; } else { lazyMatch = os; } } } + + if(!x) { + System.out.println( "os with " + virtId + " " + virtOsId + " is not in list"); + } this.os = lazyMatch; } @@ -158,23 +164,33 @@ public abstract class VmMetaData - String fileName = file.getName(); + // String fileName = file.getName(); VmMetaData meta = null; try { meta = new VmwareMetaData(osList, file); } catch ( UnsupportedVirtualizerFormatException e ) { // TODO Auto-generated catch block - e.printStackTrace(); + // e.printStackTrace(); + try { + meta = new VboxMetaData(osList, file); + } + catch (UnsupportedVirtualizerFormatException ex) { + // TODO: handle exception + ex.printStackTrace(); + } } + if (meta != null) { return meta; } - /*try { - meta = new VboxMetaData(osList, file); - } catch ( UnsupportedVirtualizerFormatException e ) { - + + + + /*if (meta != null) { + return meta; }*/ + return null; } } diff --git a/src/main/java/org/openslx/util/vm/VmwareConfig.java b/src/main/java/org/openslx/util/vm/VmwareConfig.java index f425b23..38db8a8 100644 --- a/src/main/java/org/openslx/util/vm/VmwareConfig.java +++ b/src/main/java/org/openslx/util/vm/VmwareConfig.java @@ -1,9 +1,7 @@ package org.openslx.util.vm; -import java.awt.RenderingHints.Key; import java.io.BufferedReader; import java.io.ByteArrayInputStream; -import java.io.Console; import java.io.File; import java.io.FileInputStream; import java.io.IOException; diff --git a/src/main/java/org/openslx/util/vm/VmwareMetaData.java b/src/main/java/org/openslx/util/vm/VmwareMetaData.java index 2697fc1..4e7d926 100644 --- a/src/main/java/org/openslx/util/vm/VmwareMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmwareMetaData.java @@ -1,5 +1,6 @@ package org.openslx.util.vm; +import java.io.Console; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -10,6 +11,8 @@ import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.sql.ConnectionEventListener; + import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.Virtualizer; |