import org.libvirt.Connect;
import org.libvirt.Domain;
import org.libvirt.LibvirtException;

public class TestRedef {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connect conn = null;
		int flags = 0;

		try {
			conn = new Connect("qemu:///system", false);

			String dumpxml = "<domain type='kvm'>"+
			  "<name>tt2</name>"+
			  "<memory>524288</memory>"+
			  "<currentMemory>524288</currentMemory>"+
			  "<vcpu>1</vcpu>"+
			  "<os>"+
			  "  <type arch='x86_64'>hvm</type>"+
			  "  <boot dev='hd'/>"+
			  "</os>"+
			  "<features>"+
			  "  <acpi/>"+
			  "  <apic/>"+
			   " <pae/>"+
			  "</features>"+
			  "<clock offset='utc'/>"+
			  "<on_poweroff>destroy</on_poweroff>"+
			  "<on_reboot>restart</on_reboot>"+
			  "<on_crash>restart</on_crash>"+
			  "<devices>"+
			  "  <disk type='file' device='disk'>"+
			  "    <driver name='qemu' cache='none'/>"+
			  "    <source file='/var/lib/libvirt/images/tt.img'/>"+
			  "    <target dev='hda' bus='ide'/>"+
			  "  </disk>"+
			  "  <interface type='network'>"+
			  "    <mac address='54:52:00:02:02:2c'/>"+
			  "    <source network='default'/>"+
			  "  </interface>"+
			  "  <serial type='pty'>"+
			  "    <target port='0'/>"+
			  "  </serial>"+
			  "  <console type='pty'>"+
			  "    <target port='0'/>"+
			  "  </console>"+
			  "  <input type='mouse' bus='ps2'/>"+
			  "  <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>"+
			  "</devices>"+
			"</domain>";

			Domain dm = null;

			System.out.println("lookup of tt2...");
			try {
				dm = conn.domainLookupByName("tt2");
			} catch (LibvirtException e) {
				// TODO: handle exception
			}
			System.out.println("lookup of tt2... done");

			if (dm != null) {
				System.out.println("undefine tt2...");
				dm.undefine();
				dm.free();
				System.out.println("undefine tt2... done");
			}

			System.out.println("first define/undefine tt2...");
			Domain dm1 = conn.domainDefineXML(dumpxml);
			System.out.println("first define/undefine tt2... defined");
			dm1.undefine();
			dm1.free();
			System.out.println("first define/undefine tt2... undefined");

			System.out.println("second define/undefine tt2...");
			Domain dm2 = conn.domainDefineXML(dumpxml);
			System.out.println("second define/undefine tt2... defined");
			dm2.undefine();
			dm2.free();
			System.out.println("second define/undefine tt2... undefined");
		} catch (LibvirtException e) {
			System.out.println("exception caught:" + e);
			System.out.println(e.getError());
			return;
		}
	}
}
