--- src/org/libvirt/Connect.java | 86 +++++++++++++++++++++++++----------- src/org/libvirt/Error.java | 8 ++- src/org/libvirt/ErrorHandler.java | 16 ++++--- src/org/libvirt/Network.java | 24 +++++++++- src/org/libvirt/jna/Libvirt.java | 12 ++--- src/org/libvirt/jna/virError.java | 7 ++- src/test.java | 8 ++-- 7 files changed, 108 insertions(+), 53 deletions(-) diff --git a/src/org/libvirt/Connect.java b/src/org/libvirt/Connect.java index b559b52..218bda3 100644 --- a/src/org/libvirt/Connect.java +++ b/src/org/libvirt/Connect.java @@ -23,7 +23,12 @@ public class Connect { // Load the native part static { Libvirt.INSTANCE.virInitialize() ; - Libvirt.INSTANCE.virSetErrorFunc(0, ErrorHandler.INSTANCE) ; + try { + ErrorHandler.processError(Libvirt.INSTANCE) ; + } + catch (Exception e) { + e.printStackTrace() ; + } } /** @@ -63,8 +68,7 @@ public class Connect { VCP = libvirt.virConnectOpen(uri) ; } // Check for an error -// processError(libvirt.virGetLastError()) ; - libvirt.virConnSetErrorFunc(VCP,0,ErrorHandler.INSTANCE) ; + processError() ; } /** @@ -91,10 +95,11 @@ public class Connect { */ public Connect(String uri) throws LibvirtException { VCP = libvirt.virConnectOpen(uri) ; + processError() ; } public void finalize() throws LibvirtException { - close(); + close(); } @@ -105,6 +110,7 @@ public class Connect { */ public void close() throws LibvirtException { libvirt.virConnectClose(VCP) ; + processError() ; // If leave an invalid pointer dangling around JVM crashes and burns if // someone tries to call a method on us // We rely on the underlying libvirt error handling to detect that it's called with a null virConnectPointer @@ -121,7 +127,9 @@ public class Connect { * */ public String getCapabilities() throws LibvirtException { - return libvirt.virConnectGetCapabilities(VCP) ; + String returnValue = libvirt.virConnectGetCapabilities(VCP) ; + processError() ; + return returnValue ; } @@ -135,6 +143,7 @@ public class Connect { */ public String getHostName() throws LibvirtException { String returnValue = libvirt.virConnectGetHostname(VCP) ; + processError() ; return returnValue ; } @@ -149,7 +158,9 @@ public class Connect { * @throws LibvirtException */ public int getMaxVcpus(String type) throws LibvirtException { - return libvirt.virConnectGetMaxVcpus(VCP, type) ; + int returnValue = libvirt.virConnectGetMaxVcpus(VCP, type) ; + processError() ; + return returnValue ; } @@ -160,7 +171,9 @@ public class Connect { * @throws LibvirtException */ public String getType() throws LibvirtException { - return libvirt.virConnectGetType(VCP) ; + String returnValue = libvirt.virConnectGetType(VCP) ; + processError() ; + return returnValue ; } @@ -174,7 +187,9 @@ public class Connect { * @throws LibvirtException */ public String getURI() throws LibvirtException { - return libvirt.virConnectGetURI(VCP) ; + String returnValue = libvirt.virConnectGetURI(VCP) ; + processError() ; + return returnValue ; } @@ -190,6 +205,7 @@ public class Connect { public long getVersion() throws LibvirtException { LongByReference hvVer = new LongByReference() ; libvirt.virConnectGetVersion(VCP, hvVer) ; + processError() ; return hvVer.getValue(); } @@ -204,6 +220,7 @@ public class Connect { LongByReference libVer = new LongByReference() ; LongByReference typeVer = new LongByReference() ; libvirt.virGetVersion(libVer, null, typeVer) ; + processError() ; return libVer.getValue(); } @@ -220,6 +237,7 @@ public class Connect { LongByReference libVer = new LongByReference() ; LongByReference typeVer = new LongByReference() ; libvirt.virGetVersion(libVer, type, typeVer) ; + processError() ; return libVer.getValue(); } @@ -233,8 +251,9 @@ public class Connect { public String[] listDefinedDomains() throws LibvirtException { int maxnames = this.numOfDefinedDomains() ; String[] names = new String[maxnames] ; - if (maxnames > 0) { + if (maxnames > 0) { libvirt.virConnectListDefinedDomains(VCP, names, maxnames) ; + processError() ; } return names ; } @@ -252,6 +271,7 @@ public class Connect { if (maxnames > 0) { libvirt.virConnectListDefinedNetworks(VCP, names, maxnames) ; + processError() ; } return names ; } @@ -269,6 +289,7 @@ public class Connect { if (maxids > 0) { libvirt.virConnectListDomains(VCP, ids, maxids) ; + processError() ; } return ids ; } @@ -286,6 +307,7 @@ public class Connect { if (maxnames > 0) { libvirt.virConnectListNetworks(VCP, names, maxnames) ; + processError() ; } return names ; } @@ -298,7 +320,9 @@ public class Connect { * @throws LibvirtException */ public int numOfDefinedDomains() throws LibvirtException { - return libvirt.virConnectNumOfDefinedDomains(VCP) ; + int returnValue = libvirt.virConnectNumOfDefinedDomains(VCP) ; + processError() ; + return returnValue ; } @@ -310,7 +334,9 @@ public class Connect { * @throws LibvirtException */ public int numOfDefinedNetworks() throws LibvirtException { - return libvirt.virConnectNumOfDefinedNetworks(VCP) ; + int returnValue = libvirt.virConnectNumOfDefinedNetworks(VCP) ; + processError() ; + return returnValue ; } @@ -321,7 +347,9 @@ public class Connect { * @throws LibvirtException */ public int numOfDomains() throws LibvirtException { - return libvirt.virConnectNumOfDomains(VCP) ; + int returnValue = libvirt.virConnectNumOfDomains(VCP) ; + processError() ; + return returnValue ; } @@ -332,7 +360,9 @@ public class Connect { * @throws LibvirtException */ public int numOfNetworks() throws LibvirtException { - return libvirt.virConnectNumOfNetworks(VCP) ; + int returnValue = libvirt.virConnectNumOfNetworks(VCP) ; + processError() ; + return returnValue ; } @@ -349,6 +379,7 @@ public class Connect { throws LibvirtException { Network returnValue = null ; Pointer ptr = libvirt.virNetworkLookupByName(VCP, name) ; + processError() ; if (ptr != null) { returnValue = new Network(this, ptr) ; } @@ -372,6 +403,7 @@ public class Connect { } Network returnValue = null ; Pointer ptr = libvirt.virNetworkLookupByUUID(VCP, uuidString.toString()) ; + processError() ; if (ptr != null) { returnValue = new Network(this, ptr) ; } @@ -390,6 +422,7 @@ public class Connect { throws LibvirtException { Network returnValue = null ; Pointer ptr = libvirt.virNetworkLookupByUUIDString(VCP, UUID); + processError() ; if (ptr != null) { returnValue = new Network(this, ptr) ; } @@ -410,6 +443,7 @@ public class Connect { throws LibvirtException { Network returnValue = null ; Pointer ptr = libvirt.virNetworkCreateXML(VCP, xmlDesc) ; + processError() ; if (ptr != null) { returnValue = new Network(this, ptr) ; } @@ -430,6 +464,7 @@ public class Connect { throws LibvirtException { Network returnValue = null ; Pointer ptr = libvirt.virNetworkDefineXML(VCP, xmlDesc) ; + processError() ; if (ptr != null) { returnValue = new Network(this, ptr) ; } @@ -447,6 +482,7 @@ public class Connect { public Domain domainLookupByID(int id) throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainLookupByID(VCP, id) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -464,6 +500,7 @@ public class Connect { public Domain domainLookupByName(String name) throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainLookupByName(VCP, name) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -486,6 +523,7 @@ public class Connect { } Domain returnValue = null ; Pointer ptr = libvirt.virDomainLookupByUUID(VCP, uuidString.toString()) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -503,6 +541,7 @@ public class Connect { throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainLookupByUUIDString(VCP, UUID) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -525,6 +564,7 @@ public class Connect { throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainCreateLinux(VCP, xmlDesc, flags) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -543,6 +583,7 @@ public class Connect { public Domain domainDefineXML(String xmlDesc) throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainDefineXML(VCP, xmlDesc) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -561,6 +602,7 @@ public class Connect { public Domain domainCreateXML(String xmlDesc, int flags) throws LibvirtException { Domain returnValue = null ; Pointer ptr = libvirt.virDomainCreateXML(VCP, xmlDesc, flags) ; + processError() ; if (ptr != null) { returnValue = new Domain(this, ptr) ; } @@ -591,6 +633,7 @@ public class Connect { public NodeInfo nodeInfo() throws LibvirtException { virNodeInfo vInfo = new virNodeInfo(); libvirt.virNodeGetInfo(VCP,vInfo) ; + processError() ; return new NodeInfo(vInfo) ; } @@ -773,17 +816,8 @@ public class Connect { // private native long _virStorageVolLookupByPath(long VCP, String path); -// @Override -// public void handleError(Pointer userData, virError error) throws LibvirtException { -// System.out.println("Hello") ; -// this.processError(error) ; -// } -// -// protected void processError(virError vError) throws LibvirtException { -// System.out.println("Hello") ; -// if (vError != null) { -// Error error = new Error(vError) ; -// throw new LibvirtException(error) ; -// } -// } + + protected void processError() throws LibvirtException { + ErrorHandler.processError(libvirt, VCP) ; + } } diff --git a/src/org/libvirt/Error.java b/src/org/libvirt/Error.java index ada7be8..3ba8644 100644 --- a/src/org/libvirt/Error.java +++ b/src/org/libvirt/Error.java @@ -4,6 +4,8 @@ import java.io.Serializable; import org.libvirt.jna.virError; +import com.sun.jna.Pointer; + public class Error implements Serializable { public static enum ErrorDomain { @@ -302,14 +304,14 @@ public class Error implements Serializable { ErrorDomain domain; String message; ErrorLevel level; - long VCP; /* Deprecated */ - long VDP; /* Deprecated */ + Pointer VCP; /* Deprecated */ + Pointer VDP; /* Deprecated */ String str1; String str2; String str3; int int1; int int2; - long VNP; /* Deprecated */ + Pointer VNP; /* Deprecated */ public Error() { diff --git a/src/org/libvirt/ErrorHandler.java b/src/org/libvirt/ErrorHandler.java index dca47a5..0e322ba 100644 --- a/src/org/libvirt/ErrorHandler.java +++ b/src/org/libvirt/ErrorHandler.java @@ -5,16 +5,20 @@ import org.libvirt.jna.virError; import com.sun.jna.Pointer; -public class ErrorHandler implements Libvirt.virErrorFunc +public class ErrorHandler { - public static ErrorHandler INSTANCE = new ErrorHandler() ; - @Override - public void handleError(Pointer userData, virError vError) throws LibvirtException + public static void processError(Libvirt libvirt) throws LibvirtException { + + } + public static void processError(Libvirt libvirt, Pointer conn) throws LibvirtException { - System.out.println("Hello") ; - if (vError != null) { + + virError vError = new virError() ; + int errorCode = libvirt.virConnCopyLastError(conn, vError) ; + if (errorCode > 0) { Error error = new Error(vError) ; + libvirt.virConnResetLastError(conn) ; throw new LibvirtException(error) ; } diff --git a/src/org/libvirt/Network.java b/src/org/libvirt/Network.java index 063345c..ce80f6f 100644 --- a/src/org/libvirt/Network.java +++ b/src/org/libvirt/Network.java @@ -49,7 +49,9 @@ public class Network { * @throws LibvirtException */ public String getXMLDesc(int flags) throws LibvirtException{ - return libvirt.virNetworkGetXMLDesc(VNP, flags) ; + String returnValue = libvirt.virNetworkGetXMLDesc(VNP, flags) ; + processError() ; + return returnValue ; } @@ -62,6 +64,7 @@ public class Network { public boolean getAutostart() throws LibvirtException{ IntByReference autoStart = new IntByReference() ; libvirt.virNetworkGetAutostart(VNP, autoStart) ; + processError() ; return autoStart.getValue() != 0 ? true : false ; } @@ -76,6 +79,7 @@ public class Network { public void setAutostart(boolean autostart) throws LibvirtException{ int autoValue = autostart ? 1 : 0 ; libvirt.virNetworkSetAutostart(VNP, autoValue) ; + processError() ; } @@ -86,7 +90,9 @@ public class Network { * @throws LibvirtException */ public String getBridgeName() throws LibvirtException{ - return libvirt.virNetworkGetBridgeName(VNP) ; + String returnValue = libvirt.virNetworkGetBridgeName(VNP) ; + processError() ; + return returnValue ; } @@ -108,7 +114,9 @@ public class Network { * @throws LibvirtException */ public String getName() throws LibvirtException{ - return libvirt.virNetworkGetName(VNP); + String returnValue = libvirt.virNetworkGetName(VNP); + processError() ; + return returnValue ; } @@ -122,6 +130,7 @@ public class Network { public int[] getUUID() throws LibvirtException{ byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN] ; int success = libvirt.virNetworkGetUUID(VNP, bytes) ; + processError() ; int[] returnValue = new int[0] ; if (success == 0) { returnValue = new int[Libvirt.VIR_UUID_BUFLEN] ; @@ -142,6 +151,7 @@ public class Network { public String getUUIDString() throws LibvirtException{ byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN] ; int success = libvirt.virNetworkGetUUIDString(VNP, bytes) ; + processError() ; String returnValue = null ; if (success == 0) { returnValue = new String(bytes) ; @@ -158,6 +168,7 @@ public class Network { */ public void create() throws LibvirtException{ libvirt.virNetworkCreate(VNP) ; + processError() ; } @@ -171,6 +182,7 @@ public class Network { */ public void destroy() throws LibvirtException{ libvirt.virNetworkDestroy(VNP) ; + processError() ; } @@ -183,6 +195,7 @@ public class Network { */ public void free() throws LibvirtException{ libvirt.virNetworkFree(VNP) ; + processError() ; VNP=null; } @@ -194,7 +207,12 @@ public class Network { */ public void undefine() throws LibvirtException{ libvirt.virNetworkUndefine(VNP) ; + processError() ; } + + protected void processError() throws LibvirtException { + virConnect.processError() ; + } } diff --git a/src/org/libvirt/jna/Libvirt.java b/src/org/libvirt/jna/Libvirt.java index 30ab49d..8727447 100644 --- a/src/org/libvirt/jna/Libvirt.java +++ b/src/org/libvirt/jna/Libvirt.java @@ -18,19 +18,15 @@ public interface Libvirt extends Library public static int VIR_UUID_BUFLEN = 16 ; public static int VIR_UUID_STRING_BUFLEN = (36+1) ; - //Callbacks - interface virErrorFunc extends Callback { - void handleError(Pointer userData, virError error) throws Exception ; - } - // Global functions + public int virCopyLastError(virError error) ; public virError virGetLastError() ; public int virGetVersion(LongByReference libVer, String type, LongByReference typeVer) ; public int virInitialize() ; - public void virSetErrorFunc(long userData, virErrorFunc handler) ; + public void virResetLastError() ; //Connection Functions - public int virConnCopyLastError(Pointer virConnectPtr) ; + public int virConnCopyLastError(Pointer virConnectPtr, virError to) ; public int virConnectClose(Pointer virConnectPtr) ; public String virConnectGetCapabilities(Pointer virConnectPtr) ; public String virConnectGetHostname(Pointer virConnectPtr) ; @@ -47,9 +43,9 @@ public interface Libvirt extends Library public int virConnectNumOfDefinedDomains(Pointer virConnectPtr) ; public int virConnectNumOfDefinedNetworks(Pointer virConnectPtr) ; public int virConnectNumOfNetworks(Pointer virConnectPtr) ; + public int virConnResetLastError(Pointer virConnectPtr) ; public Pointer virConnectOpen(String name) ; public Pointer virConnectOpenReadOnly(String name) ; - public void virConnSetErrorFunc(Pointer virConnectPtr, long userData, virErrorFunc handler) ; // Node functions public int virNodeGetInfo(Pointer virConnectPtr, virNodeInfo virNodeInfo) ; diff --git a/src/org/libvirt/jna/virError.java b/src/org/libvirt/jna/virError.java index db462fa..10529fe 100644 --- a/src/org/libvirt/jna/virError.java +++ b/src/org/libvirt/jna/virError.java @@ -1,5 +1,6 @@ package org.libvirt.jna; +import com.sun.jna.Pointer; import com.sun.jna.Structure ; public class virError extends Structure @@ -8,12 +9,12 @@ public class virError extends Structure public int domain ; public String message ; public int level ; - public long conn ; - public long dom ; + public Pointer conn ; + public Pointer dom ; public String str1 ; public String str2 ; public String str3 ; public int int1 ; public int int2 ; - public long net ; + public Pointer net ; } diff --git a/src/test.java b/src/test.java index 34d3518..a7d7ec8 100644 --- a/src/test.java +++ b/src/test.java @@ -142,7 +142,7 @@ public class test { testNetwork = conn.networkLookupByName("deftest") ; System.out.println("networkLookupByName: " + testNetwork.getName()) ; System.out.println(FIXME) ; - testNetwork=conn.networkLookupByUUID(UUID); +// testNetwork=conn.networkLookupByUUID(UUID); // System.out.println("networkLookupByUUID: " + testNetwork.getName()) ; testNetwork = conn.networkLookupByUUIDString("004b96e1-2d78-c30f-5aa5-f03c87d21e67"); System.out.println("networkLookupByUUIDString: " + testNetwork.getName()) ; @@ -166,10 +166,10 @@ public class test { System.out.println("exception caught:"+e); System.out.println(e.getError()); } - //This should raise an excpetion - System.out.println(FIXME) ; + //This should raise an excpetion try{ - System.out.println("virNetworkCreate:"); testNetwork.create(); + System.out.println("virNetworkCreate (should error):"); + testNetwork.create(); } catch (LibvirtException e){ System.out.println("exception caught:"+e); System.out.println(e.getError()); -- 1.6.0.6 -- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list