|
|
package org.openslx.util.vm;
import java.io.Console;
import java.io.File;
import java.io.IOException;
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;
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 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/*";
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();
}
}
// 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 ) );
}
}
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" );
}
@Override
public void enableUsb( boolean enabled )
{
// TODO Auto-generated method stub
}
@Override
public void applySettingsForLocalEdit()
{
// TODO Auto-generated method stub
}
@Override
public byte[] getFilteredDefinitionArray()
{
// TODO Auto-generated method stub
return null;
}
}
|