From d0bc412b23162e22a489adae75d22860672b4683 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Fri, 20 Apr 2018 14:32:46 +0200 Subject: [*] add support for machine snapshot detection --- src/main/java/org/openslx/util/vm/VboxConfig.java | 43 +++++++++++++++------- .../java/org/openslx/util/vm/VboxMetaData.java | 2 +- src/main/java/org/openslx/util/vm/VmMetaData.java | 7 ++++ .../java/org/openslx/util/vm/VmwareMetaData.java | 2 + 4 files changed, 39 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java index bca9183..db82cc4 100644 --- a/src/main/java/org/openslx/util/vm/VboxConfig.java +++ b/src/main/java/org/openslx/util/vm/VboxConfig.java @@ -48,7 +48,7 @@ public class VboxConfig private String hddsExpression = "/VirtualBox/Machine/MediaRegistry/HardDisks/*"; private ArrayList hddsArray = new ArrayList(); - + private boolean isMachineSnapshot = false; // list of nodes to automatically remove when reading the vbox file private static String[] blackList = { "SharedFolders", "HID", "USB", "ExtraData", "Adapter", "GuestProperties", "LPT", "StorageController", "FloppyImages", "DVDImages", "AttachedDevice", "RemoteDisplay", "VideoCapture" }; @@ -147,6 +147,7 @@ public class VboxConfig } try { setMachineName(); + checkForMachineSnapshots(); ensureHardwareUuid(); setOsType(); if ( checkForPlaceholders() ) { @@ -161,17 +162,24 @@ public class VboxConfig } } + private void checkForMachineSnapshots() throws XPathExpressionException { + // check if the vbox configuration file contains some machine snapshots. + // by looking at the existance of /VirtualBox/Machine/Snapshot + NodeList machineSnapshots = findNodes("/VirtualBox/Machine/Snapshot"); + isMachineSnapshot = (machineSnapshots != null && machineSnapshots.getLength() > 0 ); + } + private void ensureHardwareUuid() throws XPathExpressionException { - NodeList hwNodes = findNodes( "Hardware" ); - int count = hwNodes.getLength(); // we will need the machine uuid, so get it String machineUuid = xPath.compile( "/VirtualBox/Machine/@uuid" ).evaluate( this.doc ); if ( machineUuid.isEmpty() ) { LOGGER.error( "Machine UUID empty, should never happen!" ); return; } - // now check if we had a node, which we always should + + NodeList hwNodes = findNodes( "/VirtualBox/Machine/Hardware" ); + int count = hwNodes.getLength(); if ( count == 1 ) { Element hw = (Element)hwNodes.item( 0 ); String hwUuid = hw.getAttribute( "uuid" ); @@ -186,9 +194,8 @@ public class VboxConfig LOGGER.info( "Saved machine UUID as hardware UUID." ); } } else { - // zero or more than 1 were found, fatal either way - // HACK: hijack XPathExpressionException ... - throw new XPathExpressionException( "Zero or more than one node found, should never happen!" ); + // HACK: hijack XPathExpressionException ... + throw new XPathExpressionException( "Zero or more '/VirtualBox/Machine/Hardware' node were found, should never happen!" ); } } @@ -330,17 +337,21 @@ public class VboxConfig } /** - * Function used to find nodes in the document - * Function returnes a NodeList of Nodes...not just a Node...even when the wanted Node is a - * single - * Node, you get a NodeList with just one element + * Search through the doc for nodes with the given name. + * If the given name starts with '/', assume a full XPath was + * given and do not do a full search * - * @param targetTag as String + * @param identifier * @return nodes as NodeList */ - public NodeList findNodes( String targetTag ) + public NodeList findNodes( String identifier ) { - String path = ".//" + targetTag; + String path; + if ( identifier.startsWith( "/", 0 ) ) + path = identifier; + else + path = ".//" + identifier; + XPathExpression expr; NodeList nodes = null; try { @@ -617,4 +628,8 @@ public class VboxConfig throw new RuntimeException( "Error converting to String", ex ); } } + + public boolean isMachineSnapshot() { + return isMachineSnapshot; + } } diff --git a/src/main/java/org/openslx/util/vm/VboxMetaData.java b/src/main/java/org/openslx/util/vm/VboxMetaData.java index 81ffc5b..f95a2c9 100644 --- a/src/main/java/org/openslx/util/vm/VboxMetaData.java +++ b/src/main/java/org/openslx/util/vm/VboxMetaData.java @@ -100,7 +100,7 @@ public class VboxMetaData extends VmMetaData protected String displayName = null; + protected boolean isMachineSnapshot; /* * Getters for virtual hardware */ @@ -185,6 +186,12 @@ public abstract class VmMetaData return displayName; } + /* + * Getter for isMachineSnapshot + */ + public boolean isMachineSnapshot() { + return isMachineSnapshot; + } /** * This method should return a minimal representation of the input meta data. * The representation is platform dependent, and should be stripped of all diff --git a/src/main/java/org/openslx/util/vm/VmwareMetaData.java b/src/main/java/org/openslx/util/vm/VmwareMetaData.java index c8e4716..f1efb8c 100644 --- a/src/main/java/org/openslx/util/vm/VmwareMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmwareMetaData.java @@ -144,6 +144,8 @@ public class VmwareMetaData extends VmMetaData