[libvirt] [libvirt-java] [PATCH 00/15] Refactor error handling

The error handling in libvirt-java is sort of a mess. Each and every class contains a processError() method which just forwards to ErrorHandler.processError(jna.Libvirt). Furthermore, this processError() method is often called unnecessarily after every libvirt call, although its return code did not indicate an error at all. So, this patchset removes the cruft with the added benefit of avoiding calls into native code when possible. Patch #1 starts the refactoring. It adds a few helper methods to the ErrorHandler class which will be removed by patch #15. Patch #2 to #13 are mostly mechanical, just wrapping calls of any libvirt function into a call to processError(int) or processError<T>(T). Patch #14 removes the obsolete processError(Libvirt) method. Patch #15 cleans up patch #1. Claudio Bley (15): Start refactoring of error handling Remove processError from Device class Remove processError from Domain class Remove processError from DomainSnapshot class Remove processError from Interface class Remove processError method from Network class Remove processError method from NetworkFilter class Remove processError method from Secret class Remove processError method from StoragePool class Remove processError method from StorageVol class Remove processError method from Stream class Remove processError method from Connect class Call processError only when virInitialize signalled an error Remove ErrorHandler.processError(Libvirt) method fixup! Start refactoring of error handling src/main/java/org/libvirt/Connect.java | 69 +----- src/main/java/org/libvirt/Device.java | 49 ++-- src/main/java/org/libvirt/Domain.java | 328 +++++++++----------------- src/main/java/org/libvirt/DomainSnapshot.java | 23 +- src/main/java/org/libvirt/ErrorHandler.java | 44 +++- src/main/java/org/libvirt/Interface.java | 43 +--- src/main/java/org/libvirt/Library.java | 4 +- src/main/java/org/libvirt/Network.java | 61 ++--- src/main/java/org/libvirt/NetworkFilter.java | 43 +--- src/main/java/org/libvirt/Secret.java | 61 ++--- src/main/java/org/libvirt/StoragePool.java | 95 +++----- src/main/java/org/libvirt/StorageVol.java | 56 ++--- src/main/java/org/libvirt/Stream.java | 64 ++--- 13 files changed, 297 insertions(+), 643 deletions(-) -- 1.8.5.2.msysgit.0

Almost every class contains a processError() method with an identical definition, just forwarding the call to ErrorHandler.processError(Libvirt). This function is always called after a libvirt function call (as per its javadoc comment). But, actually, there's no use in always calling processError when there was no error signalled by the libvirt function having been called. This is just a waste of CPU cycles. Furthermore, it's more than ugly that the error handling is littered all over the place in every class. This patch lays ground for generalizing the error handling in a common place and removing those functions from the individual classes. Basically, this copies the processError(int) and processError<T>(T) methods from the Connect class to the ErrorHandler class as static methods. It deprecates the processError(Libvirt) method, which will be removed eventually in a later patch. --- During refactoring, in order to disambiguate between the processError method taking a Libvirt object as a parameter and the generic processError<T> method, the processError method is specialized for any type extending from PointerType (such as DomainPointer, ConnectionPointer et cetera), for Pointer and for String. This will be removed later. src/main/java/org/libvirt/ErrorHandler.java | 47 ++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/libvirt/ErrorHandler.java b/src/main/java/org/libvirt/ErrorHandler.java index 434c85d..8dcac8f 100644 --- a/src/main/java/org/libvirt/ErrorHandler.java +++ b/src/main/java/org/libvirt/ErrorHandler.java @@ -3,6 +3,11 @@ package org.libvirt; import org.libvirt.jna.Libvirt; import org.libvirt.jna.virError; +import static org.libvirt.Library.libvirt; + +import com.sun.jna.Pointer; +import com.sun.jna.PointerType; + /** * Utility class which processes the last error from the libvirt library. It * turns errors into Libvirt Exceptions. @@ -18,7 +23,12 @@ public class ErrorHandler { * the active connection * @throws LibvirtException */ - public static void processError(Libvirt libvirt) throws LibvirtException { + @Deprecated + static void processError(Libvirt libvirt) throws LibvirtException { + processError(); + } + + private static final void processError() throws LibvirtException { virError vError = libvirt.virGetLastError(); if (vError != null) { Error error = new Error(vError); @@ -30,4 +40,39 @@ public class ErrorHandler { } } } + + /** + * Calls {@link #processError()} when the given libvirt return code + * indicates an error. + * + * @param ret libvirt return code, indicating error if -1. + * @return {@code ret} + * @throws LibvirtException + */ + static final int processError(int ret) throws LibvirtException { + if (ret == -1) processError(); + return ret; + } + + /** + * Calls {@link #processError()} if {@code arg} is null. + * + * @param arg An arbitrary object returned by libvirt. + * @return {@code arg} + * @throws LibvirtException + */ + static final <T extends PointerType> T processError(T arg) throws LibvirtException { + if (arg == null) processError(); + return arg; + } + + static final Pointer processError(Pointer arg) throws LibvirtException { + if (arg == null) processError(); + return arg; + } + + static final String processError(String str) throws LibvirtException { + if (str == null) processError(); + return str; + } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). --- Note, that this patch depends on another patch[1] still awaiting review. [1] http://www.redhat.com/archives/libvir-list/2014-January/msg00397.html src/main/java/org/libvirt/Device.java | 49 ++++++++++------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/libvirt/Device.java b/src/main/java/org/libvirt/Device.java index d55321e..a3b20b8 100644 --- a/src/main/java/org/libvirt/Device.java +++ b/src/main/java/org/libvirt/Device.java @@ -2,6 +2,9 @@ package org.libvirt; import org.libvirt.jna.DevicePointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; + +import com.sun.jna.Pointer; import com.sun.jna.Pointer; @@ -43,8 +46,7 @@ public class Device { public int destroy() throws LibvirtException { int success = 0; if (VDP != null) { - success = libvirt.virNodeDeviceDestroy(VDP); - processError(); + success = processError(libvirt.virNodeDeviceDestroy(VDP)); VDP = null; } @@ -58,9 +60,7 @@ public class Device { * @throws LibvirtException */ public int detach() throws LibvirtException { - int num = libvirt.virNodeDeviceDettach(VDP); - processError(); - return num; + return processError(libvirt.virNodeDeviceDettach(VDP)); } @Override @@ -78,8 +78,7 @@ public class Device { public int free() throws LibvirtException { int success = 0; if (VDP != null) { - success = libvirt.virNodeDeviceFree(VDP); - processError(); + success = processError(libvirt.virNodeDeviceFree(VDP)); VDP = null; } @@ -92,9 +91,7 @@ public class Device { * @throws LibvirtException */ public String getName() throws LibvirtException { - String name = libvirt.virNodeDeviceGetName(VDP); - processError(); - return name; + return processError(libvirt.virNodeDeviceGetName(VDP)); } /** @@ -103,9 +100,7 @@ public class Device { * @throws LibvirtException */ public int getNumberOfCapabilities() throws LibvirtException { - int num = libvirt.virNodeDeviceNumOfCaps(VDP); - processError(); - return num; + return processError(libvirt.virNodeDeviceNumOfCaps(VDP)); } /** @@ -114,9 +109,7 @@ public class Device { * @throws LibvirtException */ public String getParent() throws LibvirtException { - String parent = libvirt.virNodeDeviceGetParent(VDP); - processError(); - return parent; + return processError(libvirt.virNodeDeviceGetParent(VDP)); } /** @@ -125,9 +118,7 @@ public class Device { * @throws LibvirtException */ public String getXMLDescription() throws LibvirtException { - String desc = libvirt.virNodeDeviceGetXMLDesc(VDP); - processError(); - return desc; + return processError(libvirt.virNodeDeviceGetXMLDesc(VDP)); } /** @@ -140,8 +131,8 @@ public class Device { if (maxCaps > 0) { Pointer[] ptrs = new Pointer[maxCaps]; - int got = libvirt.virNodeDeviceListCaps(VDP, ptrs, maxCaps); - processError(); + int got = processError(libvirt.virNodeDeviceListCaps(VDP, ptrs, maxCaps)); + return Library.toStringArray(ptrs, got); } else { return Library.NO_STRINGS; @@ -149,22 +140,12 @@ public class Device { } /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); - } - - /** * ReAttach a device to the node. * * @throws LibvirtException */ public int reAttach() throws LibvirtException { - int num = libvirt.virNodeDeviceReAttach(VDP); - processError(); - return num; + return processError(libvirt.virNodeDeviceReAttach(VDP)); } /** @@ -174,8 +155,6 @@ public class Device { * @throws LibvirtException */ public int reset() throws LibvirtException { - int num = libvirt.virNodeDeviceReset(VDP); - processError(); - return num; + return processError(libvirt.virNodeDeviceReset(VDP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also update erroneous javadoc comments stating that methods would return a value in case an error occurs. In case of a libvirt error, a LibvirtException is thrown. Add processErrorIfZero(long) to ErrorHandler class to handle special libvirt return codes, such as for virDomainGetMaxMemory. Use it in Domain.getMaxMemory(). --- src/main/java/org/libvirt/Domain.java | 328 +++++++++------------------- src/main/java/org/libvirt/ErrorHandler.java | 5 + 2 files changed, 113 insertions(+), 220 deletions(-) diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java index 2f70bf2..4860770 100644 --- a/src/main/java/org/libvirt/Domain.java +++ b/src/main/java/org/libvirt/Domain.java @@ -12,6 +12,8 @@ import org.libvirt.jna.virDomainMemoryStats; import org.libvirt.jna.virSchedParameter; import org.libvirt.jna.virVcpuInfo; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; +import static org.libvirt.ErrorHandler.processErrorIfZero; import com.sun.jna.Native; import com.sun.jna.NativeLong; @@ -159,13 +161,11 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainAbortJob">Libvirt * Documentation</a> - * @return 0 in case of success and -1 in case of failure. + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int abortJob() throws LibvirtException { - int returnValue = libvirt.virDomainAbortJob(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainAbortJob(VDP)); } /** @@ -179,8 +179,7 @@ public class Domain { * @throws LibvirtException */ public void attachDevice(String xmlDesc) throws LibvirtException { - libvirt.virDomainAttachDevice(VDP, xmlDesc); - processError(); + processError(libvirt.virDomainAttachDevice(VDP, xmlDesc)); } /** @@ -196,8 +195,7 @@ public class Domain { * @throws LibvirtException */ public void attachDeviceFlags(String xmlDesc, int flags) throws LibvirtException { - libvirt.virDomainAttachDeviceFlags(VDP, xmlDesc, flags); - processError(); + processError(libvirt.virDomainAttachDeviceFlags(VDP, xmlDesc, flags)); } /** @@ -206,14 +204,13 @@ public class Domain { * * @param path * the path to the block device - * @return the info, or null if an error + * @return the info * @throws LibvirtException */ public DomainBlockInfo blockInfo(String path) throws LibvirtException { virDomainBlockInfo info = new virDomainBlockInfo(); - int success = libvirt.virDomainGetBlockInfo(VDP, path, info, 0); - processError(); - return success == 0 ? new DomainBlockInfo(info) : null; + processError(libvirt.virDomainGetBlockInfo(VDP, path, info, 0)); + return new DomainBlockInfo(info); } /** @@ -233,9 +230,8 @@ public class Domain { */ public DomainBlockStats blockStats(String path) throws LibvirtException { virDomainBlockStats stats = new virDomainBlockStats(); - int success = libvirt.virDomainBlockStats(VDP, path, stats, stats.size()); - processError(); - return success == 0 ? new DomainBlockStats(stats) : null; + processError(libvirt.virDomainBlockStats(VDP, path, stats, stats.size())); + return new DomainBlockStats(stats); } /** @@ -250,8 +246,7 @@ public class Domain { * @throws LibvirtException */ public void blockResize(String disk, long size, int flags) throws LibvirtException { - int returnValue = libvirt.virDomainBlockResize(VDP, disk, size, flags); - processError(); + processError(libvirt.virDomainBlockResize(VDP, disk, size, flags)); } @@ -266,8 +261,7 @@ public class Domain { * @throws LibvirtException */ public void coreDump(String to, int flags) throws LibvirtException { - libvirt.virDomainCoreDump(VDP, to, flags); - processError(); + processError(libvirt.virDomainCoreDump(VDP, to, flags)); } /** @@ -282,12 +276,11 @@ public class Domain { * Launches this defined domain. If the call succeed the domain moves from * the defined to the running domains pools. * + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int create() throws LibvirtException { - int returnValue = libvirt.virDomainCreate(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainCreate(VDP)); } /** @@ -295,12 +288,11 @@ public class Domain { * If the call succeed the domain moves from * the defined to the running domains pools. * + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int create(int flags) throws LibvirtException { - int returnValue = libvirt.virDomainCreateWithFlags(VDP, flags); - processError(); - return returnValue; + return processError(libvirt.virDomainCreateWithFlags(VDP, flags)); } /** @@ -312,8 +304,7 @@ public class Domain { * @throws LibvirtException */ public void destroy() throws LibvirtException { - libvirt.virDomainDestroy(VDP); - processError(); + processError(libvirt.virDomainDestroy(VDP)); } /** @@ -327,8 +318,7 @@ public class Domain { * @throws LibvirtException */ public void detachDevice(String xmlDesc) throws LibvirtException { - libvirt.virDomainDetachDevice(VDP, xmlDesc); - processError(); + processError(libvirt.virDomainDetachDevice(VDP, xmlDesc)); } /** @@ -342,8 +332,7 @@ public class Domain { * @throws LibvirtException */ public void detachDeviceFlags(String xmlDesc, int flags) throws LibvirtException { - libvirt.virDomainDetachDeviceFlags(VDP, xmlDesc, flags); - processError(); + processError(libvirt.virDomainDetachDeviceFlags(VDP, xmlDesc, flags)); } @Override @@ -356,13 +345,12 @@ public class Domain { * structure is freed and should not be used thereafter. * * @throws LibvirtException - * @return number of references left (>= 0) for success, -1 for failure. + * @return number of references left (>= 0) */ public int free() throws LibvirtException { int success = 0; if (VDP != null) { - success = libvirt.virDomainFree(VDP); - processError(); + success = processError(libvirt.virDomainFree(VDP)); VDP = null; } @@ -378,8 +366,7 @@ public class Domain { */ public boolean getAutostart() throws LibvirtException { IntByReference autoStart = new IntByReference(); - libvirt.virDomainGetAutostart(VDP, autoStart); - processError(); + processError(libvirt.virDomainGetAutostart(VDP, autoStart)); return autoStart.getValue() != 0 ? true : false; } @@ -399,9 +386,7 @@ public class Domain { * @throws LibvirtException */ public int getID() throws LibvirtException { - int returnValue = libvirt.virDomainGetID(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainGetID(VDP)); } /** @@ -417,14 +402,9 @@ public class Domain { * @throws LibvirtException */ public DomainInfo getInfo() throws LibvirtException { - DomainInfo returnValue = null; virDomainInfo vInfo = new virDomainInfo(); - int success = libvirt.virDomainGetInfo(VDP, vInfo); - processError(); - if (success == 0) { - returnValue = new DomainInfo(vInfo); - } - return returnValue; + processError(libvirt.virDomainGetInfo(VDP, vInfo)); + return new DomainInfo(vInfo); } /** @@ -438,14 +418,9 @@ public class Domain { * @throws LibvirtException */ public DomainJobInfo getJobInfo() throws LibvirtException { - DomainJobInfo returnValue = null; virDomainJobInfo vInfo = new virDomainJobInfo(); - int success = libvirt.virDomainGetJobInfo(VDP, vInfo); - processError(); - if (success == 0) { - returnValue = new DomainJobInfo(vInfo); - } - return returnValue; + processError(libvirt.virDomainGetJobInfo(VDP, vInfo)); + return new DomainJobInfo(vInfo); } /** @@ -455,9 +430,9 @@ public class Domain { * @throws LibvirtException */ public long getMaxMemory() throws LibvirtException { + // the memory size in kibibytes (blocks of 1024 bytes), or 0 in case of error. NativeLong returnValue = libvirt.virDomainGetMaxMemory(VDP); - processError(); - return returnValue.longValue(); + return processErrorIfZero(returnValue.longValue()); } /** @@ -470,21 +445,17 @@ public class Domain { * @throws LibvirtException */ public int getMaxVcpus() throws LibvirtException { - int returnValue = libvirt.virDomainGetMaxVcpus(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainGetMaxVcpus(VDP)); } /** * Gets the public name for this domain * - * @return the name - * @throws LibvirtException + * @return the name, null if there is no name + * @throws LibvirtException <em>never</em> */ public String getName() throws LibvirtException { - String returnValue = libvirt.virDomainGetName(VDP); - processError(); - return returnValue; + return libvirt.virDomainGetName(VDP); } /** @@ -494,8 +465,7 @@ public class Domain { * @throws LibvirtException */ public String getOSType() throws LibvirtException { - Pointer ptr = libvirt.virDomainGetOSType(VDP); - processError(); + Pointer ptr = processError(libvirt.virDomainGetOSType(VDP)); try { return Library.getString(ptr); } finally { @@ -512,18 +482,14 @@ public class Domain { public SchedParameter[] getSchedulerParameters() throws LibvirtException { IntByReference nParams = new IntByReference(); SchedParameter[] returnValue = new SchedParameter[0]; - Pointer pScheduler = libvirt.virDomainGetSchedulerType(VDP, nParams); - processError(); - if (pScheduler != null) { - String scheduler = Library.getString(pScheduler); - Library.free(pScheduler); - virSchedParameter[] nativeParams = new virSchedParameter[nParams.getValue()]; - returnValue = new SchedParameter[nParams.getValue()]; - libvirt.virDomainGetSchedulerParameters(VDP, nativeParams, nParams); - processError(); - for (int x = 0; x < nParams.getValue(); x++) { - returnValue[x] = SchedParameter.create(nativeParams[x]); - } + Pointer pScheduler = processError(libvirt.virDomainGetSchedulerType(VDP, nParams)); + String scheduler = Library.getString(pScheduler); + Library.free(pScheduler); + virSchedParameter[] nativeParams = new virSchedParameter[nParams.getValue()]; + returnValue = new SchedParameter[nParams.getValue()]; + processError(libvirt.virDomainGetSchedulerParameters(VDP, nativeParams, nParams)); + for (int x = 0; x < nParams.getValue(); x++) { + returnValue[x] = SchedParameter.create(nativeParams[x]); } return returnValue; @@ -541,8 +507,7 @@ public class Domain { */ public String[] getSchedulerType() throws LibvirtException { IntByReference nParams = new IntByReference(); - Pointer pScheduler = libvirt.virDomainGetSchedulerType(VDP, nParams); - processError(); + Pointer pScheduler = processError(libvirt.virDomainGetSchedulerType(VDP, nParams)); String[] array = new String[1]; array[0] = Library.getString(pScheduler); Library.free(pScheduler); @@ -558,13 +523,8 @@ public class Domain { */ public int[] getUUID() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN]; - int success = libvirt.virDomainGetUUID(VDP, bytes); - processError(); - int[] returnValue = new int[0]; - if (success == 0) { - returnValue = Connect.convertUUIDBytes(bytes); - } - return returnValue; + processError(libvirt.virDomainGetUUID(VDP, bytes)); + return Connect.convertUUIDBytes(bytes); } /** @@ -576,13 +536,8 @@ public class Domain { */ public String getUUIDString() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN]; - int success = libvirt.virDomainGetUUIDString(VDP, bytes); - processError(); - String returnValue = null; - if (success == 0) { - returnValue = Native.toString(bytes); - } - return returnValue; + processError(libvirt.virDomainGetUUIDString(VDP, bytes)); + return Native.toString(bytes); } /** @@ -602,8 +557,7 @@ public class Domain { virVcpuInfo[] infos = new virVcpuInfo[cpuCount]; returnValue = new int[cpuCount * maplength]; byte[] cpumaps = new byte[cpuCount * maplength]; - libvirt.virDomainGetVcpus(VDP, infos, cpuCount, cpumaps, maplength); - processError(); + processError(libvirt.virDomainGetVcpus(VDP, infos, cpuCount, cpumaps, maplength)); for (int x = 0; x < cpuCount * maplength; x++) { returnValue[x] = cpumaps[x]; } @@ -621,8 +575,7 @@ public class Domain { int cpuCount = getMaxVcpus(); VcpuInfo[] returnValue = new VcpuInfo[cpuCount]; virVcpuInfo[] infos = new virVcpuInfo[cpuCount]; - libvirt.virDomainGetVcpus(VDP, infos, cpuCount, null, 0); - processError(); + processError(libvirt.virDomainGetVcpus(VDP, infos, cpuCount, null, 0)); for (int x = 0; x < cpuCount; x++) { returnValue[x] = new VcpuInfo(infos[x]); } @@ -641,8 +594,7 @@ public class Domain { * Description format </a> */ public String getXMLDesc(int flags) throws LibvirtException { - Pointer ptr = libvirt.virDomainGetXMLDesc(VDP, flags); - processError(); + Pointer ptr = processError(libvirt.virDomainGetXMLDesc(VDP, flags)); try { return Library.getString(ptr); } finally { @@ -655,13 +607,11 @@ public class Domain { * * @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainHasCurrentSnapshot>Libvi * r t Documentation</a> - * @return 1 if running, 0 if inactive, -1 on error + * @return 1 if running, 0 if inactive * @throws LibvirtException */ public int hasCurrentSnapshot() throws LibvirtException { - int returnValue = libvirt.virDomainHasCurrentSnapshot(VDP, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainHasCurrentSnapshot(VDP, 0)); } /** @@ -674,9 +624,7 @@ public class Domain { * @throws LibvirtException */ public int hasManagedSaveImage() throws LibvirtException { - int returnValue = libvirt.virDomainHasManagedSaveImage(VDP, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainHasManagedSaveImage(VDP, 0)); } /** @@ -694,8 +642,7 @@ public class Domain { */ public DomainInterfaceStats interfaceStats(String path) throws LibvirtException { virDomainInterfaceStats stats = new virDomainInterfaceStats(); - libvirt.virDomainInterfaceStats(VDP, path, stats, stats.size()); - processError(); + processError(libvirt.virDomainInterfaceStats(VDP, path, stats, stats.size())); return new DomainInterfaceStats(stats); } @@ -705,13 +652,11 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainIsActive">Libvirt * Documentation</a> - * @return 1 if running, 0 if inactive, -1 on error + * @return 1 if running, 0 if inactive * @throws LibvirtException */ public int isActive() throws LibvirtException { - int returnValue = libvirt.virDomainIsActive(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainIsActive(VDP)); } /** @@ -721,13 +666,11 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainIsPersistent">Libvirt * Documentation</a> - * @return 1 if persistent, 0 if transient, -1 on error + * @return 1 if persistent, 0 if transient * @throws LibvirtException */ public int isPersistent() throws LibvirtException { - int returnValue = libvirt.virDomainIsPersistent(VDP); - processError(); - return returnValue; + return processError(libvirt.virDomainIsPersistent(VDP)); } /** @@ -736,13 +679,11 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainManagedSave">Libvirt * Documentation</a> - * @return 0 in case of success or -1 in case of failure + * @return always 0 * @throws LibvirtException */ public int managedSave() throws LibvirtException { - int returnValue = libvirt.virDomainManagedSave(VDP, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainManagedSave(VDP, 0)); } /** @@ -751,13 +692,11 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainManagedSaveRemove">Libvirt * Documentation</a> - * @return 0 in case of success, and -1 in case of error + * @return always 0 * @throws LibvirtException */ public int managedSaveRemove() throws LibvirtException { - int returnValue = libvirt.virDomainManagedSaveRemove(VDP, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainManagedSaveRemove(VDP, 0)); } /** @@ -765,19 +704,16 @@ public class Domain { * * @param number * the number of stats to retrieve - * @return the collection of stats, or null if an error occurs. + * @return the collection of stats * @throws LibvirtException */ public MemoryStatistic[] memoryStats(int number) throws LibvirtException { virDomainMemoryStats[] stats = new virDomainMemoryStats[number]; MemoryStatistic[] returnStats = null; - int result = libvirt.virDomainMemoryStats(VDP, stats, number, 0); - processError(); - if (result >= 0) { - returnStats = new MemoryStatistic[result]; - for (int x = 0; x < result; x++) { - returnStats[x] = new MemoryStatistic(stats[x]); - } + int result = processError(libvirt.virDomainMemoryStats(VDP, stats, number, 0)); + returnStats = new MemoryStatistic[result]; + for (int x = 0; x < result; x++) { + returnStats[x] = new MemoryStatistic(stats[x]); } return returnStats; } @@ -851,8 +787,8 @@ public class Domain { * @throws LibvirtException if the migration fails */ public Domain migrate(Connect dconn, long flags, String dxml, String dname, String uri, long bandwidth) throws LibvirtException { - DomainPointer newPtr = libvirt.virDomainMigrate2(VDP, dconn.VCP, dxml, new NativeLong(flags), dname, uri, new NativeLong(bandwidth)); - processError(); + DomainPointer newPtr = + processError(libvirt.virDomainMigrate2(VDP, dconn.VCP, dxml, new NativeLong(flags), dname, uri, new NativeLong(bandwidth))); return new Domain(dconn, newPtr); } @@ -895,14 +831,13 @@ public class Domain { * (optional) dest hostname/URI as seen from the source host * @param bandwidth * optional) specify migration bandwidth limit in Mbps - * @return the new domain object if the migration was successful, or NULL in - * case of error. Note that the new domain object exists in the - * scope of the destination connection (dconn). + * @return the new domain object if the migration was successful. Note that + * the new domain object exists in the scope of the destination + * connection (dconn). * @throws LibvirtException */ public Domain migrate(Connect dconn, long flags, String dname, String uri, long bandwidth) throws LibvirtException { - DomainPointer newPtr = libvirt.virDomainMigrate(VDP, dconn.VCP, new NativeLong(flags), dname, uri, new NativeLong(bandwidth)); - processError(); + DomainPointer newPtr = processError(libvirt.virDomainMigrate(VDP, dconn.VCP, new NativeLong(flags), dname, uri, new NativeLong(bandwidth))); return new Domain(dconn, newPtr); } @@ -915,13 +850,11 @@ public class Domain { * Documentation</a> * @param downtime * the time to be down - * @return 0 in case of success, -1 otherwise. + * @return always 0 * @throws LibvirtException */ public int migrateSetMaxDowntime(long downtime) throws LibvirtException { - int returnValue = libvirt.virDomainMigrateSetMaxDowntime(VDP, downtime, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainMigrateSetMaxDowntime(VDP, downtime, 0)); } /** @@ -955,9 +888,9 @@ public class Domain { * @throws LibvirtException */ public int migrateToURI(String dconnuri, String miguri, String dxml, long flags, String dname, long bandwidth) throws LibvirtException { - int returnValue = libvirt.virDomainMigrateToURI2(VDP, dconnuri, miguri, dxml, new NativeLong(flags), dname, new NativeLong(bandwidth)); - processError(); - return returnValue; + return processError(libvirt.virDomainMigrateToURI2(VDP, dconnuri, miguri, + dxml, new NativeLong(flags), + dname, new NativeLong(bandwidth))); } /** @@ -980,9 +913,7 @@ public class Domain { * @throws LibvirtException */ public int migrateToURI(String uri, long flags, String dname, long bandwidth) throws LibvirtException { - int returnValue = libvirt.virDomainMigrateToURI(VDP, uri, new NativeLong(flags), dname, new NativeLong(bandwidth)); - processError(); - return returnValue; + return processError(libvirt.virDomainMigrateToURI(VDP, uri, new NativeLong(flags), dname, new NativeLong(bandwidth))); } /** @@ -1004,16 +935,7 @@ public class Domain { for (int x = 0; x < cpumap.length; x++) { packedMap[x] = (byte) cpumap[x]; } - libvirt.virDomainPinVcpu(VDP, vcpu, packedMap, cpumap.length); - processError(); - } - - /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + processError(libvirt.virDomainPinVcpu(VDP, vcpu, packedMap, cpumap.length)); } /** @@ -1026,8 +948,7 @@ public class Domain { * @throws LibvirtException */ public void reboot(int flags) throws LibvirtException { - libvirt.virDomainReboot(VDP, flags); - processError(); + processError(libvirt.virDomainReboot(VDP, flags)); } /** @@ -1038,8 +959,7 @@ public class Domain { * @throws LibvirtException */ public void resume() throws LibvirtException { - libvirt.virDomainResume(VDP); - processError(); + processError(libvirt.virDomainResume(VDP)); } /** @@ -1050,13 +970,11 @@ public class Domain { * >Libvirt Documentation</> * @param snapshot * the snapshot to revert to - * @return 0 if the creation is successful, -1 on error. + * @return 0 if the creation is successful * @throws LibvirtException */ public int revertToSnapshot(DomainSnapshot snapshot) throws LibvirtException { - int returnCode = libvirt.virDomainRevertToSnapshot(snapshot.VDSP, 0); - processError(); - return returnCode; + return processError(libvirt.virDomainRevertToSnapshot(snapshot.VDSP, 0)); } /** @@ -1070,8 +988,7 @@ public class Domain { * @throws LibvirtException */ public void save(String to) throws LibvirtException { - libvirt.virDomainSave(VDP, to); - processError(); + processError(libvirt.virDomainSave(VDP, to)); } /** @@ -1083,8 +1000,7 @@ public class Domain { */ public void setAutostart(boolean autostart) throws LibvirtException { int autoValue = autostart ? 1 : 0; - libvirt.virDomainSetAutostart(VDP, autoValue); - processError(); + processError(libvirt.virDomainSetAutostart(VDP, autoValue)); } /** @@ -1096,8 +1012,7 @@ public class Domain { * @throws LibvirtException */ public void setMaxMemory(long memory) throws LibvirtException { - libvirt.virDomainSetMaxMemory(VDP, new NativeLong(memory)); - processError(); + processError(libvirt.virDomainSetMaxMemory(VDP, new NativeLong(memory))); } /** @@ -1110,8 +1025,7 @@ public class Domain { * @throws LibvirtException */ public void setMemory(long memory) throws LibvirtException { - libvirt.virDomainSetMemory(VDP, new NativeLong(memory)); - processError(); + processError(libvirt.virDomainSetMemory(VDP, new NativeLong(memory))); } /** @@ -1126,8 +1040,7 @@ public class Domain { for (int x = 0; x < params.length; x++) { input[x] = SchedParameter.toNative(params[x]); } - libvirt.virDomainSetSchedulerParameters(VDP, input, params.length); - processError(); + processError(libvirt.virDomainSetSchedulerParameters(VDP, input, params.length)); } /** @@ -1141,8 +1054,7 @@ public class Domain { * @throws LibvirtException */ public void setVcpus(int nvcpus) throws LibvirtException { - libvirt.virDomainSetVcpus(VDP, nvcpus); - processError(); + processError(libvirt.virDomainSetVcpus(VDP, nvcpus)); } /** @@ -1154,8 +1066,7 @@ public class Domain { * @throws LibvirtException */ public void shutdown() throws LibvirtException { - libvirt.virDomainShutdown(VDP); - processError(); + processError(libvirt.virDomainShutdown(VDP)); } /** @@ -1169,17 +1080,12 @@ public class Domain { * string containing an XML description of the domain * @param flags * flags for creating the snapshot, see the virDomainSnapshotCreateFlags for the flag options - * @return the snapshot, or null on Error + * @return the snapshot * @throws LibvirtException */ public DomainSnapshot snapshotCreateXML(String xmlDesc, int flags) throws LibvirtException { - DomainSnapshotPointer ptr = libvirt.virDomainSnapshotCreateXML(VDP, xmlDesc, flags); - processError(); - DomainSnapshot returnValue = null; - if (ptr != null) { - returnValue = new DomainSnapshot(virConnect, ptr); - } - return returnValue; + DomainSnapshotPointer ptr = processError(libvirt.virDomainSnapshotCreateXML(VDP, xmlDesc, flags)); + return new DomainSnapshot(virConnect, ptr); } /** @@ -1208,17 +1114,12 @@ public class Domain { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotCurrent">Libvirt * Documentation</a> - * @return the snapshot, or null on Error + * @return the snapshot * @throws LibvirtException */ public DomainSnapshot snapshotCurrent() throws LibvirtException { - DomainSnapshotPointer ptr = libvirt.virDomainSnapshotCurrent(VDP, 0); - processError(); - DomainSnapshot returnValue = null; - if (ptr != null) { - returnValue = new DomainSnapshot(virConnect, ptr); - } - return returnValue; + DomainSnapshotPointer ptr = processError(libvirt.virDomainSnapshotCurrent(VDP, 0)); + return new DomainSnapshot(virConnect, ptr); } /** @@ -1236,8 +1137,7 @@ public class Domain { if (num >= 0) { returnValue = new String[num]; if (num > 0) { - libvirt.virDomainSnapshotListNames(VDP, returnValue, num, flags); - processError(); + processError(libvirt.virDomainSnapshotListNames(VDP, returnValue, num, flags)); } } return returnValue; @@ -1268,17 +1168,12 @@ public class Domain { * Documentation</a> * @param name * the name - * @return The located snapshot, or null if an error + * @return The located snapshot * @throws LibvirtException */ public DomainSnapshot snapshotLookupByName(String name) throws LibvirtException { - DomainSnapshotPointer ptr = libvirt.virDomainSnapshotLookupByName(VDP, name, 0); - processError(); - DomainSnapshot returnValue = null; - if (ptr != null) { - returnValue = new DomainSnapshot(virConnect, ptr); - } - return returnValue; + DomainSnapshotPointer ptr = processError(libvirt.virDomainSnapshotLookupByName(VDP, name, 0)); + return new DomainSnapshot(virConnect, ptr); } /** @@ -1289,9 +1184,7 @@ public class Domain { * Documentation</a> */ public int snapshotNum() throws LibvirtException { - int returnValue = libvirt.virDomainSnapshotNum(VDP, 0); - processError(); - return returnValue; + return processError(libvirt.virDomainSnapshotNum(VDP, 0)); } /** @@ -1303,8 +1196,7 @@ public class Domain { * @throws LibvirtException */ public void suspend() throws LibvirtException { - libvirt.virDomainSuspend(VDP); - processError(); + processError(libvirt.virDomainSuspend(VDP)); } /** @@ -1313,8 +1205,7 @@ public class Domain { * @throws LibvirtException */ public void undefine() throws LibvirtException { - libvirt.virDomainUndefine(VDP); - processError(); + processError(libvirt.virDomainUndefine(VDP)); } /** @@ -1326,8 +1217,7 @@ public class Domain { * @throws LibvirtException */ public void undefine(int flags) throws LibvirtException { - libvirt.virDomainUndefineFlags(VDP, flags); - processError(); + processError(libvirt.virDomainUndefineFlags(VDP, flags)); } /** @@ -1338,13 +1228,11 @@ public class Domain { * the xml to update with * @param flags * controls the update - * @return 0 in case of success, -1 in case of failure. + * @return always 0 * @throws LibvirtException */ public int updateDeviceFlags(String xml, int flags) throws LibvirtException { - int returnValue = libvirt.virDomainUpdateDeviceFlags(VDP, xml, flags); - processError(); - return returnValue; + return processError(libvirt.virDomainUpdateDeviceFlags(VDP, xml, flags)); } } diff --git a/src/main/java/org/libvirt/ErrorHandler.java b/src/main/java/org/libvirt/ErrorHandler.java index 8dcac8f..01e25d6 100644 --- a/src/main/java/org/libvirt/ErrorHandler.java +++ b/src/main/java/org/libvirt/ErrorHandler.java @@ -75,4 +75,9 @@ public class ErrorHandler { if (str == null) processError(); return str; } + + static final long processErrorIfZero(long ret) throws LibvirtException { + if (ret == 0) processError(); + return ret; + } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error happens. --- src/main/java/org/libvirt/DomainSnapshot.java | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/libvirt/DomainSnapshot.java b/src/main/java/org/libvirt/DomainSnapshot.java index 9409fef..4736a71 100644 --- a/src/main/java/org/libvirt/DomainSnapshot.java +++ b/src/main/java/org/libvirt/DomainSnapshot.java @@ -2,6 +2,7 @@ package org.libvirt; import org.libvirt.jna.DomainSnapshotPointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; public class DomainSnapshot { @@ -28,14 +29,13 @@ public class DomainSnapshot { * Documentation</a> * @param flags * controls the deletion - * @return 0 if the selected snapshot(s) were successfully deleted, -1 on error. + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int delete(int flags) throws LibvirtException { int success = 0; if (VDSP != null) { - success = libvirt.virDomainSnapshotDelete(VDSP, flags); - processError(); + success = processError(libvirt.virDomainSnapshotDelete(VDSP, flags)); VDSP = null; } @@ -52,13 +52,12 @@ public class DomainSnapshot { * exist. * * @throws LibvirtException - * @return 0 on success, or -1 on error. + * @return 0 on success */ public int free() throws LibvirtException { int success = 0; if (VDSP != null) { - success = libvirt.virDomainSnapshotFree(VDSP); - processError(); + success = processError(libvirt.virDomainSnapshotFree(VDSP)); VDSP = null; } @@ -72,16 +71,6 @@ public class DomainSnapshot { * @return the XML document */ public String getXMLDesc() throws LibvirtException { - String returnValue = libvirt.virDomainSnapshotGetXMLDesc(VDSP, 0); - processError(); - return returnValue; - } - - /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virDomainSnapshotGetXMLDesc(VDSP, 0)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/Interface.java | 43 ++++++++------------------------ 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/libvirt/Interface.java b/src/main/java/org/libvirt/Interface.java index 6ea21b6..e0ddc3f 100644 --- a/src/main/java/org/libvirt/Interface.java +++ b/src/main/java/org/libvirt/Interface.java @@ -2,6 +2,7 @@ package org.libvirt; import org.libvirt.jna.InterfacePointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Pointer; @@ -51,9 +52,7 @@ public class Interface { * @throws LibvirtException */ public int create() throws LibvirtException { - int returnValue = libvirt.virInterfaceCreate(VIP); - processError(); - return returnValue; + return processError(libvirt.virInterfaceCreate(VIP)); } /** @@ -72,9 +71,7 @@ public class Interface { * @throws LibvirtException */ public int destroy() throws LibvirtException { - int returnValue = libvirt.virInterfaceDestroy(VIP); - processError(); - return returnValue; + return processError(libvirt.virInterfaceDestroy(VIP)); } @Override @@ -87,13 +84,12 @@ public class Interface { * structure is freed and should not be used thereafter. * * @throws LibvirtException - * @return number of references left (>= 0) for success, -1 for failure. + * @return number of references left (>= 0) */ public int free() throws LibvirtException { int success = 0; if (VIP != null) { - success = libvirt.virInterfaceFree(VIP); - processError(); + success = processError(libvirt.virInterfaceFree(VIP)); VIP = null; } @@ -106,9 +102,7 @@ public class Interface { * @throws LibvirtException */ public String getMACString() throws LibvirtException { - String name = libvirt.virInterfaceGetMACString(VIP); - processError(); - return name; + return processError(libvirt.virInterfaceGetMACString(VIP)); } /** @@ -117,9 +111,7 @@ public class Interface { * @throws LibvirtException */ public String getName() throws LibvirtException { - String name = libvirt.virInterfaceGetName(VIP); - processError(); - return name; + return processError(libvirt.virInterfaceGetName(VIP)); } /** @@ -128,8 +120,7 @@ public class Interface { * @throws LibvirtException */ public String getXMLDescription(int flags) throws LibvirtException { - Pointer xml = libvirt.virInterfaceGetXMLDesc(VIP, flags); - processError(); + Pointer xml = processError(libvirt.virInterfaceGetXMLDesc(VIP, flags)); try { return Library.getString(xml); } finally { @@ -143,21 +134,11 @@ public class Interface { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virInterfaceIsActive">Libvirt * Documentation</a> - * @return 1 if running, 0 if inactive, -1 on error + * @return 1 if running, 0 if inactive * @throws LibvirtException */ public int isActive() throws LibvirtException { - int returnValue = libvirt.virInterfaceIsActive(VIP); - processError(); - return returnValue; - } - - /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virInterfaceIsActive(VIP)); } /** @@ -167,8 +148,6 @@ public class Interface { * @throws LibvirtException */ public int undefine() throws LibvirtException { - int returnValue = libvirt.virInterfaceUndefine(VIP); - processError(); - return returnValue; + return processError(libvirt.virInterfaceUndefine(VIP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/Network.java | 61 ++++++++++------------------------ 1 file changed, 17 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/libvirt/Network.java b/src/main/java/org/libvirt/Network.java index 2244c5d..2a77028 100644 --- a/src/main/java/org/libvirt/Network.java +++ b/src/main/java/org/libvirt/Network.java @@ -3,6 +3,7 @@ package org.libvirt; import org.libvirt.jna.Libvirt; import org.libvirt.jna.NetworkPointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Native; import com.sun.jna.Pointer; @@ -43,8 +44,7 @@ public class Network { * @throws LibvirtException */ public void create() throws LibvirtException { - libvirt.virNetworkCreate(VNP); - processError(); + processError(libvirt.virNetworkCreate(VNP)); } /** @@ -56,8 +56,7 @@ public class Network { * @throws LibvirtException */ public void destroy() throws LibvirtException { - libvirt.virNetworkDestroy(VNP); - processError(); + processError(libvirt.virNetworkDestroy(VNP)); } @Override @@ -71,13 +70,12 @@ public class Network { * return an error. * * @throws LibvirtException - * @return number of references left (>= 0) for success, -1 for failure. + * @return number of references left (>= 0) */ public int free() throws LibvirtException { int success = 0; if (VNP != null) { - success = libvirt.virNetworkFree(VNP); - processError(); + success = processError(libvirt.virNetworkFree(VNP)); VNP = null; } @@ -93,8 +91,7 @@ public class Network { */ public boolean getAutostart() throws LibvirtException { IntByReference autoStart = new IntByReference(); - libvirt.virNetworkGetAutostart(VNP, autoStart); - processError(); + processError(libvirt.virNetworkGetAutostart(VNP, autoStart)); return (autoStart.getValue() != 0) ? true : false; } @@ -106,8 +103,7 @@ public class Network { * @throws LibvirtException */ public String getBridgeName() throws LibvirtException { - final Pointer ptr = libvirt.virNetworkGetBridgeName(VNP); - processError(); + final Pointer ptr = processError(libvirt.virNetworkGetBridgeName(VNP)); try { return Library.getString(ptr); } finally { @@ -131,9 +127,7 @@ public class Network { * @throws LibvirtException */ public String getName() throws LibvirtException { - String returnValue = libvirt.virNetworkGetName(VNP); - processError(); - return returnValue; + return processError(libvirt.virNetworkGetName(VNP)); } /** @@ -145,13 +139,8 @@ 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 = Connect.convertUUIDBytes(bytes); - } - return returnValue; + processError(libvirt.virNetworkGetUUID(VNP, bytes)); + return Connect.convertUUIDBytes(bytes); } /** @@ -163,13 +152,8 @@ 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 = Native.toString(bytes); - } - return returnValue; + processError(libvirt.virNetworkGetUUIDString(VNP, bytes)); + return Native.toString(bytes); } /** @@ -183,8 +167,7 @@ public class Network { * @throws LibvirtException */ public String getXMLDesc(int flags) throws LibvirtException { - Pointer ptr = libvirt.virNetworkGetXMLDesc(VNP, flags); - processError(); + Pointer ptr = processError(libvirt.virNetworkGetXMLDesc(VNP, flags)); try { return Library.getString(ptr); } finally { @@ -202,9 +185,7 @@ public class Network { * @throws LibvirtException */ public int isActive() throws LibvirtException { - int returnValue = libvirt.virNetworkIsActive(VNP); - processError(); - return returnValue; + return processError(libvirt.virNetworkIsActive(VNP)); } /** @@ -218,13 +199,7 @@ public class Network { * @throws LibvirtException */ public int isPersistent() throws LibvirtException { - int returnValue = libvirt.virNetworkIsPersistent(VNP); - processError(); - return returnValue; - } - - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virNetworkIsPersistent(VNP)); } /** @@ -237,8 +212,7 @@ public class Network { */ public void setAutostart(boolean autostart) throws LibvirtException { int autoValue = autostart ? 1 : 0; - libvirt.virNetworkSetAutostart(VNP, autoValue); - processError(); + processError(libvirt.virNetworkSetAutostart(VNP, autoValue)); } /** @@ -247,8 +221,7 @@ public class Network { * @throws LibvirtException */ public void undefine() throws LibvirtException { - libvirt.virNetworkUndefine(VNP); - processError(); + processError(libvirt.virNetworkUndefine(VNP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/NetworkFilter.java | 43 +++++++--------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/libvirt/NetworkFilter.java b/src/main/java/org/libvirt/NetworkFilter.java index ba4d2ea..4f4bc6c 100644 --- a/src/main/java/org/libvirt/NetworkFilter.java +++ b/src/main/java/org/libvirt/NetworkFilter.java @@ -3,6 +3,7 @@ package org.libvirt; import org.libvirt.jna.Libvirt; import org.libvirt.jna.NetworkFilterPointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Native; @@ -32,13 +33,12 @@ public class NetworkFilter { * exist. * * @throws LibvirtException - * @return 0 on success, or -1 on error. + * @return <em>ignore</em> (always 0) */ public int free() throws LibvirtException { int success = 0; if (NFP != null) { - success = libvirt.virNWFilterFree(NFP); - processError(); + success = processError(libvirt.virNWFilterFree(NFP)); NFP = null; } @@ -52,9 +52,7 @@ public class NetworkFilter { * @throws LibvirtException */ public String getName() throws LibvirtException { - String returnValue = libvirt.virNWFilterGetName(NFP); - processError(); - return returnValue; + return processError(libvirt.virNWFilterGetName(NFP)); } /** @@ -66,13 +64,8 @@ public class NetworkFilter { */ public int[] getUUID() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN]; - int success = libvirt.virNWFilterGetUUID(NFP, bytes); - processError(); - int[] returnValue = new int[0]; - if (success == 0) { - returnValue = Connect.convertUUIDBytes(bytes); - } - return returnValue; + processError(libvirt.virNWFilterGetUUID(NFP, bytes)); + return Connect.convertUUIDBytes(bytes); } /** @@ -84,13 +77,8 @@ public class NetworkFilter { */ public String getUUIDString() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN]; - int success = libvirt.virNWFilterGetUUIDString(NFP, bytes); - processError(); - String returnValue = null; - if (success == 0) { - returnValue = Native.toString(bytes); - } - return returnValue; + processError(libvirt.virNWFilterGetUUIDString(NFP, bytes)); + return Native.toString(bytes); } /** @@ -102,17 +90,7 @@ public class NetworkFilter { * @return the XML document */ public String getXMLDesc() throws LibvirtException { - String returnValue = libvirt.virNWFilterGetXMLDesc(NFP, 0); - processError(); - return returnValue; - } - - /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virNWFilterGetXMLDesc(NFP, 0)); } /** @@ -121,7 +99,6 @@ public class NetworkFilter { * @throws LibvirtException */ public void undefine() throws LibvirtException { - libvirt.virNWFilterUndefine(NFP); - processError(); + processError(libvirt.virNWFilterUndefine(NFP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/Secret.java | 61 +++++++++-------------------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/libvirt/Secret.java b/src/main/java/org/libvirt/Secret.java index 5332e02..63b1571 100644 --- a/src/main/java/org/libvirt/Secret.java +++ b/src/main/java/org/libvirt/Secret.java @@ -3,6 +3,7 @@ package org.libvirt; import org.libvirt.jna.Libvirt; import org.libvirt.jna.SecretPointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Native; import com.sun.jna.NativeLong; @@ -39,13 +40,12 @@ public class Secret { * Release the secret handle. The underlying secret continues to exist. * * @throws LibvirtException - * @return 0 on success, or -1 on error. + * @return <em>ignore</em> (always 0) */ public int free() throws LibvirtException { int success = 0; if (VSP != null) { - success = libvirt.virSecretFree(VSP); - processError(); + success = processError(libvirt.virSecretFree(VSP)); VSP = null; } @@ -61,9 +61,7 @@ public class Secret { * @throws LibvirtException */ public String getUsageID() throws LibvirtException { - String returnValue = libvirt.virSecretGetUsageID(VSP); - processError(); - return returnValue; + return processError(libvirt.virSecretGetUsageID(VSP)); } /** @@ -75,13 +73,8 @@ public class Secret { */ public int[] getUUID() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN]; - int success = libvirt.virSecretGetUUID(VSP, bytes); - processError(); - int[] returnValue = new int[0]; - if (success == 0) { - returnValue = Connect.convertUUIDBytes(bytes); - } - return returnValue; + processError(libvirt.virSecretGetUUID(VSP, bytes)); + return Connect.convertUUIDBytes(bytes); } /** @@ -93,13 +86,8 @@ public class Secret { */ public String getUUIDString() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN]; - int success = libvirt.virSecretGetUUIDString(VSP, bytes); - processError(); - String returnValue = null; - if (success == 0) { - returnValue = Native.toString(bytes); - } - return returnValue; + processError(libvirt.virSecretGetUUIDString(VSP, bytes)); + return Native.toString(bytes); } /** @@ -121,8 +109,7 @@ public class Secret { */ public byte[] getByteValue() throws LibvirtException { LongByReference value_size = new LongByReference(); - Pointer value = libvirt.virSecretGetValue(VSP, value_size, 0); - processError(); + Pointer value = processError(libvirt.virSecretGetValue(VSP, value_size, 0)); ByteBuffer bb = value.getByteBuffer(0, value_size.getValue()); byte[] returnValue = new byte[bb.remaining()]; bb.get(returnValue); @@ -135,49 +122,33 @@ public class Secret { * @return the XML document */ public String getXMLDesc() throws LibvirtException { - String returnValue = libvirt.virSecretGetXMLDesc(VSP, 0); - processError(); - return returnValue; - } - - /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virSecretGetXMLDesc(VSP, 0)); } /** * Sets the value of the secret * - * @return 0 on success, -1 on failure. + * @return <em>ignore</em> (always 0) */ public int setValue(String value) throws LibvirtException { - int returnValue = libvirt.virSecretSetValue(VSP, value, new NativeLong(value.length()), 0); - processError(); - return returnValue; + return processError(libvirt.virSecretSetValue(VSP, value, new NativeLong(value.length()), 0)); } /** * Sets the value of the secret * - * @return 0 on success, -1 on failure. + * @return <em>ignore</em> (always 0) */ public int setValue(byte[] value) throws LibvirtException { - int returnValue = libvirt.virSecretSetValue(VSP, value, new NativeLong(value.length), 0); - processError(); - return returnValue; + return processError(libvirt.virSecretSetValue(VSP, value, new NativeLong(value.length), 0)); } /** * Undefines, but does not free, the Secret. * - * @return 0 on success, -1 on failure. + * @return <em>ignore</em> (always 0) */ public int undefine() throws LibvirtException { - int returnValue = libvirt.virSecretUndefine(VSP); - processError(); - return returnValue; + return processError(libvirt.virSecretUndefine(VSP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Adjust the doc comment for storageVolLookupByName to indicate that it might return null. Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/StoragePool.java | 95 ++++++++++-------------------- 1 file changed, 30 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/libvirt/StoragePool.java b/src/main/java/org/libvirt/StoragePool.java index 2d59f68..bb608e3 100644 --- a/src/main/java/org/libvirt/StoragePool.java +++ b/src/main/java/org/libvirt/StoragePool.java @@ -5,6 +5,7 @@ import org.libvirt.jna.StoragePoolPointer; import org.libvirt.jna.StorageVolPointer; import org.libvirt.jna.virStoragePoolInfo; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Native; import com.sun.jna.ptr.IntByReference; @@ -72,8 +73,7 @@ public class StoragePool { * future flags, use 0 for now */ public void build(int flags) throws LibvirtException { - libvirt.virStoragePoolBuild(VSPP, flags); - processError(); + processError(libvirt.virStoragePoolBuild(VSPP, flags)); } /** @@ -83,8 +83,7 @@ public class StoragePool { * future flags, use 0 for now */ public void create(int flags) throws LibvirtException { - libvirt.virStoragePoolCreate(VSPP, flags); - processError(); + processError(libvirt.virStoragePoolCreate(VSPP, flags)); } /** @@ -95,8 +94,7 @@ public class StoragePool { * flags for obliteration process */ public void delete(int flags) throws LibvirtException { - libvirt.virStoragePoolDelete(VSPP, flags); - processError(); + processError(libvirt.virStoragePoolDelete(VSPP, flags)); } /** @@ -106,8 +104,7 @@ public class StoragePool { * This does not free the associated virStoragePoolPtr object. */ public void destroy() throws LibvirtException { - libvirt.virStoragePoolDestroy(VSPP); - processError(); + processError(libvirt.virStoragePoolDestroy(VSPP)); } @Override @@ -120,13 +117,12 @@ public class StoragePool { * not change the state of the pool on the host. * * @throws LibvirtException - * @return number of references left (>= 0) for success, -1 for failure. + * @return number of references left (>= 0) */ public int free() throws LibvirtException { int success = 0; if (VSPP != null) { - success = libvirt.virStoragePoolFree(VSPP); - processError(); + success = processError(libvirt.virStoragePoolFree(VSPP)); VSPP = null; } return success; @@ -141,8 +137,7 @@ public class StoragePool { */ public boolean getAutostart() throws LibvirtException { IntByReference autoStart = new IntByReference(); - libvirt.virStoragePoolGetAutostart(VSPP, autoStart); - processError(); + processError(libvirt.virStoragePoolGetAutostart(VSPP, autoStart)); return autoStart.getValue() != 0 ? true : false; } @@ -164,8 +159,7 @@ public class StoragePool { */ public StoragePoolInfo getInfo() throws LibvirtException { virStoragePoolInfo vInfo = new virStoragePoolInfo(); - libvirt.virStoragePoolGetInfo(VSPP, vInfo); - processError(); + processError(libvirt.virStoragePoolGetInfo(VSPP, vInfo)); return new StoragePoolInfo(vInfo); } @@ -176,9 +170,7 @@ public class StoragePool { * @throws LibvirtException */ public String getName() throws LibvirtException { - String returnValue = libvirt.virStoragePoolGetName(VSPP); - processError(); - return returnValue; + return processError(libvirt.virStoragePoolGetName(VSPP)); } /** @@ -189,13 +181,8 @@ public class StoragePool { */ public int[] getUUID() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN]; - int success = libvirt.virStoragePoolGetUUID(VSPP, bytes); - processError(); - int[] returnValue = new int[0]; - if (success == 0) { - returnValue = Connect.convertUUIDBytes(bytes); - } - return returnValue; + processError(libvirt.virStoragePoolGetUUID(VSPP, bytes)); + return Connect.convertUUIDBytes(bytes); } /** @@ -206,13 +193,8 @@ public class StoragePool { */ public String getUUIDString() throws LibvirtException { byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN]; - int success = libvirt.virStoragePoolGetUUIDString(VSPP, bytes); - processError(); - String returnValue = null; - if (success == 0) { - returnValue = Native.toString(bytes); - } - return returnValue; + processError(libvirt.virStoragePoolGetUUIDString(VSPP, bytes)); + return Native.toString(bytes); } /** @@ -224,9 +206,7 @@ public class StoragePool { * @return a XML document -java @throws LibvirtException */ public String getXMLDesc(int flags) throws LibvirtException { - String returnValue = libvirt.virStoragePoolGetXMLDesc(VSPP, flags); - processError(); - return returnValue; + return processError(libvirt.virStoragePoolGetXMLDesc(VSPP, flags)); } /** @@ -235,13 +215,11 @@ public class StoragePool { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virStoragePoolIsActive">Libvirt * Documentation</a> - * @return 1 if running, 0 if inactive, -1 on error + * @return 1 if running, 0 if inactive * @throws LibvirtException */ public int isActive() throws LibvirtException { - int returnValue = libvirt.virStoragePoolIsActive(VSPP); - processError(); - return returnValue; + return processError(libvirt.virStoragePoolIsActive(VSPP)); } /** @@ -251,13 +229,11 @@ public class StoragePool { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virStoragePoolIsPersistent">Libvirt * Documentation</a> - * @return 1 if persistent, 0 if transient, -1 on error + * @return 1 if persistent, 0 if transient * @throws LibvirtException */ public int isPersistent() throws LibvirtException { - int returnValue = libvirt.virStoragePoolIsPersistent(VSPP); - processError(); - return returnValue; + return processError(libvirt.virStoragePoolIsPersistent(VSPP)); } /** @@ -270,8 +246,7 @@ public class StoragePool { public String[] listVolumes() throws LibvirtException { int num = numOfVolumes(); String[] returnValue = new String[num]; - libvirt.virStoragePoolListVolumes(VSPP, returnValue, num); - processError(); + processError(libvirt.virStoragePoolListVolumes(VSPP, returnValue, num)); return returnValue; } @@ -282,13 +257,7 @@ public class StoragePool { * @throws LibvirtException */ public int numOfVolumes() throws LibvirtException { - int returnValue = libvirt.virStoragePoolNumOfVolumes(VSPP); - processError(); - return returnValue; - } - - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virStoragePoolNumOfVolumes(VSPP)); } /** @@ -301,8 +270,7 @@ public class StoragePool { * @throws LibvirtException */ public void refresh(int flags) throws LibvirtException { - libvirt.virStoragePoolRefresh(VSPP, flags); - processError(); + processError(libvirt.virStoragePoolRefresh(VSPP, flags)); } /** @@ -328,8 +296,7 @@ public class StoragePool { * @throws LibvirtException */ public StorageVol storageVolCreateXML(String xmlDesc, int flags) throws LibvirtException { - StorageVolPointer sPtr = libvirt.virStorageVolCreateXML(VSPP, xmlDesc, flags); - processError(); + StorageVolPointer sPtr = processError(libvirt.virStorageVolCreateXML(VSPP, xmlDesc, flags)); return new StorageVol(virConnect, sPtr); } @@ -338,13 +305,12 @@ public class StoragePool { * as input. Information for the new volume (name, perms) are passed via a * typical volume XML description. * - * @return The storage volume, or {@code null} on error. + * @return The storage volume * @throws LibvirtException */ public StorageVol storageVolCreateXMLFrom(String xmlDesc, StorageVol cloneVolume, int flags) throws LibvirtException { - StorageVolPointer sPtr = libvirt.virStorageVolCreateXMLFrom(VSPP, xmlDesc, cloneVolume.VSVP, flags); - processError(); + StorageVolPointer sPtr = processError(libvirt.virStorageVolCreateXMLFrom(VSPP, xmlDesc, cloneVolume.VSVP, flags)); return new StorageVol(virConnect, sPtr); } @@ -354,13 +320,13 @@ public class StoragePool { * * @param name * name of storage volume - * @return The StorageVol object found + * @return a StorageVol object, or {@code null} if not found. * @throws LibvirtException */ public StorageVol storageVolLookupByName(String name) throws LibvirtException { - StorageVolPointer sPtr = libvirt.virStorageVolLookupByName(VSPP, name); - processError(); - return new StorageVol(virConnect, sPtr); + StorageVolPointer sPtr = processError(libvirt.virStorageVolLookupByName(VSPP, name)); + + return (sPtr == null) ? null : new StorageVol(virConnect, sPtr); } /** @@ -369,8 +335,7 @@ public class StoragePool { * @throws LibvirtException */ public void undefine() throws LibvirtException { - libvirt.virStoragePoolUndefine(VSPP); - processError(); + processError(libvirt.virStoragePoolUndefine(VSPP)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/StorageVol.java | 56 +++++++++---------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/libvirt/StorageVol.java b/src/main/java/org/libvirt/StorageVol.java index 561cb4d..1573070 100644 --- a/src/main/java/org/libvirt/StorageVol.java +++ b/src/main/java/org/libvirt/StorageVol.java @@ -4,6 +4,7 @@ import org.libvirt.jna.StoragePoolPointer; import org.libvirt.jna.StorageVolPointer; import org.libvirt.jna.virStorageVolInfo; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; /** * An acutal storage bucket. @@ -82,8 +83,7 @@ public class StorageVol { * @throws LibvirtException */ public void delete(int flags) throws LibvirtException { - libvirt.virStorageVolDelete(VSVP, flags); - processError(); + processError(libvirt.virStorageVolDelete(VSVP, flags)); } @Override @@ -96,13 +96,12 @@ public class StorageVol { * to exist * * @throws LibvirtException - * @return number of references left (>= 0) for success, -1 for failure. + * @return number of references left (>= 0) */ public int free() throws LibvirtException { int success = 0; if (VSVP != null) { - libvirt.virStorageVolFree(VSVP); - processError(); + success = processError(libvirt.virStorageVolFree(VSVP)); VSVP = null; } return success; @@ -127,8 +126,7 @@ public class StorageVol { */ public StorageVolInfo getInfo() throws LibvirtException { virStorageVolInfo vInfo = new virStorageVolInfo(); - libvirt.virStorageVolGetInfo(VSVP, vInfo); - processError(); + processError(libvirt.virStorageVolGetInfo(VSVP, vInfo)); return new StorageVolInfo(vInfo); } @@ -140,9 +138,7 @@ public class StorageVol { * @throws LibvirtException */ public String getKey() throws LibvirtException { - String returnValue = libvirt.virStorageVolGetKey(VSVP); - processError(); - return returnValue; + return processError(libvirt.virStorageVolGetKey(VSVP)); } /** @@ -152,9 +148,7 @@ public class StorageVol { * @throws LibvirtException */ public String getName() throws LibvirtException { - String returnValue = libvirt.virStorageVolGetName(VSVP); - processError(); - return returnValue; + return processError(libvirt.virStorageVolGetName(VSVP)); } /** @@ -167,9 +161,7 @@ public class StorageVol { * @throws LibvirtException */ public String getPath() throws LibvirtException { - String returnValue = libvirt.virStorageVolGetPath(VSVP); - processError(); - return returnValue; + return processError(libvirt.virStorageVolGetPath(VSVP)); } /** @@ -181,43 +173,29 @@ public class StorageVol { * @throws LibvirtException */ public String getXMLDesc(int flags) throws LibvirtException { - String returnValue = libvirt.virStorageVolGetXMLDesc(VSVP, flags); - processError(); - return returnValue; - } - - /** - * Error handling logic which should be called after every libvirt call - * - * @throws LibvirtException - */ - protected void processError() throws LibvirtException { - virConnect.processError(); + return processError(libvirt.virStorageVolGetXMLDesc(VSVP, flags)); } /** * Fetch a storage pool which contains this volume * - * @return StoragePool object, + * @return StoragePool object, or {@code null} if not found. * @throws LibvirtException */ public StoragePool storagePoolLookupByVolume() throws LibvirtException { - StoragePoolPointer ptr = libvirt.virStoragePoolLookupByVolume(VSVP); - processError(); - return new StoragePool(virConnect, ptr); + StoragePoolPointer ptr = processError(libvirt.virStoragePoolLookupByVolume(VSVP)); + return (ptr == null) ? null : new StoragePool(virConnect, ptr); } /** * Ensure data previously on a volume is not accessible to future reads * * @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virStorageVolWipe">Libvirt Documentation</a> - * @return 0 on success, or -1 on error + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int wipe() throws LibvirtException { - int returnValue = libvirt.virStorageVolWipe(VSVP, 0); - processError(); - return returnValue; + return processError(libvirt.virStorageVolWipe(VSVP, 0)); } /** @@ -228,12 +206,10 @@ public class StorageVol { * new capacity for volume * @param flags * flags for resizing, see libvirt API for exact flags - * @return 0 on success, or -1 on error + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int resize(long capacity, int flags) throws LibvirtException { - int returnValue = libvirt.virStorageVolResize(VSVP, capacity, flags); - processError(); - return returnValue; + return processError(libvirt.virStorageVolResize(VSVP, capacity, flags)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..). Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/Stream.java | 64 +++++++++++------------------------ 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/libvirt/Stream.java b/src/main/java/org/libvirt/Stream.java index bd8e87f..fae9729 100644 --- a/src/main/java/org/libvirt/Stream.java +++ b/src/main/java/org/libvirt/Stream.java @@ -3,6 +3,7 @@ package org.libvirt; import org.libvirt.jna.Libvirt; import org.libvirt.jna.StreamPointer; import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.NativeLong; @@ -28,11 +29,11 @@ public class Stream { /** * Request that the in progress data transfer be cancelled abnormally before * the end of the stream has been reached + * + * @return <em>ignore</em> (always 0) */ public int abort() throws LibvirtException { - int returnValue = libvirt.virStreamAbort(VSP); - processError(); - return returnValue; + return processError(libvirt.virStreamAbort(VSP)); } /** @@ -46,13 +47,11 @@ public class Stream { * the events to monitor * @param cb * the callback method - * @return 0 for success, -1 for failure + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int addCallback(int events, Libvirt.VirStreamEventCallback cb) throws LibvirtException { - int returnValue = libvirt.virStreamEventAddCallback(VSP, events, cb, null, null); - processError(); - return returnValue; + return processError(libvirt.virStreamEventAddCallback(VSP, events, cb, null, null)); } @Override @@ -64,27 +63,24 @@ public class Stream { * Indicate that there is no further data is to be transmitted on the * stream. * - * @return 0 if success, -1 if failure + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int finish() throws LibvirtException { - int returnValue = libvirt.virStreamFinish(VSP); - processError(); - return returnValue; + return processError(libvirt.virStreamFinish(VSP)); } /** * Decrement the reference count on a stream, releasing the stream object if * the reference count has hit zero. * + * @return <em>ignore</em> (always 0) * @throws LibvirtException - * @return 0 on success, or -1 on error. */ public int free() throws LibvirtException { int success = 0; if (VSP != null) { - success = libvirt.virStreamFree(VSP); - processError(); + processError(libvirt.virStreamFree(VSP)); VSP = null; } @@ -92,14 +88,6 @@ public class Stream { } /** - * Error handling logic to throw errors. Must be called after every libvirt - * call. - */ - protected void processError() throws LibvirtException { - virConnect.processError(); - } - - /** * Receives data from the stream into the buffer provided. * * @param data @@ -108,9 +96,7 @@ public class Stream { * @throws LibvirtException */ public int receive(byte[] data) throws LibvirtException { - int returnValue = libvirt.virStreamRecv(VSP, data, new NativeLong(data.length)); - processError(); - return returnValue; + return processError(libvirt.virStreamRecv(VSP, data, new NativeLong(data.length))); } /** @@ -119,26 +105,22 @@ public class Stream { * @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecvAll">virStreamRecvAll</a> * @param handler * the callback handler - * @return 0 if successfule, -1 otherwise + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int receiveAll(Libvirt.VirStreamSinkFunc handler) throws LibvirtException { - int returnValue = libvirt.virStreamRecvAll(VSP, handler, null); - processError(); - return returnValue; + return processError(libvirt.virStreamRecvAll(VSP, handler, null)); } /** * Remove an event callback from the stream * * @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventRemoveCallback">Libvirt Docs</a> - * @return 0 for success, -1 for failure + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int removeCallback() throws LibvirtException { - int returnValue = libvirt.virStreamEventRemoveCallback(VSP); - processError(); - return returnValue; + return processError(libvirt.virStreamEventRemoveCallback(VSP)); } /** @@ -151,9 +133,7 @@ public class Stream { * @throws LibvirtException */ public int send(String data) throws LibvirtException { - int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length())); - processError(); - return returnValue; + return processError(libvirt.virStreamSend(VSP, data, new NativeLong(data.length()))); } /** @@ -164,13 +144,11 @@ public class Stream { * Documentation</a> * @param handler * the callback handler - * @return 0 if successfule, -1 otherwise + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int sendAll(Libvirt.VirStreamSourceFunc handler) throws LibvirtException { - int returnValue = libvirt.virStreamSendAll(VSP, handler, null); - processError(); - return returnValue; + return processError(libvirt.virStreamSendAll(VSP, handler, null)); } /** @@ -179,12 +157,10 @@ public class Stream { * @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventUpdateCallback">Libvirt Docs</a> * @param events * the events to monitor - * @return 0 for success, -1 for failure + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int updateCallback(int events) throws LibvirtException { - int returnValue = libvirt.virStreamEventUpdateCallback(VSP, events); - processError(); - return returnValue; + return processError(libvirt.virStreamEventUpdateCallback(VSP, events)); } } -- 1.8.5.2.msysgit.0

Wrap any fallible libvirt function in a call to ErrorHandler.processError(..) and remove calls to the deprecated ErrorHandler.processError(Libvirt) method. Also correct wrong javadoc comments stating that methods would return a value in case an error occurs. --- src/main/java/org/libvirt/Connect.java | 69 ++++++---------------------------- 1 file changed, 11 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java index b3e29be..fedc60e 100644 --- a/src/main/java/org/libvirt/Connect.java +++ b/src/main/java/org/libvirt/Connect.java @@ -15,7 +15,10 @@ import org.libvirt.jna.StorageVolPointer; import org.libvirt.jna.StreamPointer; import org.libvirt.jna.virConnectAuth; import org.libvirt.jna.virNodeInfo; + import static org.libvirt.Library.libvirt; +import static org.libvirt.ErrorHandler.processError; +import static org.libvirt.ErrorHandler.processErrorIfZero; import com.sun.jna.Memory; import com.sun.jna.NativeLong; @@ -79,7 +82,6 @@ public class Connect { */ public static void setErrorCallback(Libvirt.VirErrorCallback callback) throws LibvirtException { Libvirt.INSTANCE.virSetErrorFunc(null, callback); - ErrorHandler.processError(Libvirt.INSTANCE); } /** @@ -119,7 +121,6 @@ public class Connect { VCP = libvirt.virConnectOpen(uri); // Check for an error processError(VCP); - ErrorHandler.processError(Libvirt.INSTANCE); } /** @@ -140,7 +141,6 @@ public class Connect { } // Check for an error processError(VCP); - ErrorHandler.processError(Libvirt.INSTANCE); } /** @@ -173,7 +173,6 @@ public class Connect { VCP = libvirt.virConnectOpenAuth(uri, vAuth, flags); // Check for an error processError(VCP); - ErrorHandler.processError(Libvirt.INSTANCE); } /** @@ -220,8 +219,7 @@ public class Connect { * @throws LibvirtException */ public CPUCompareResult compareCPU(String xmlDesc) throws LibvirtException { - int rawResult = libvirt.virConnectCompareCPU(VCP, xmlDesc, 0); - processError(); + int rawResult = processError(libvirt.virConnectCompareCPU(VCP, xmlDesc, 0)); return CPUCompareResult.get(rawResult); } @@ -308,13 +306,11 @@ public class Connect { * Documentation</a> * @param callbackID * the callback to deregister - * @return 0 on success, -1 on failure + * @return <em>ignore</em> (always 0) * @throws LibvirtException */ public int domainEventDeregisterAny(int callbackID) throws LibvirtException { - int returnValue = libvirt.virConnectDomainEventDeregisterAny(VCP, callbackID); - processError(); - return returnValue; + return processError(libvirt.virConnectDomainEventDeregisterAny(VCP, callbackID)); } /** @@ -496,10 +492,7 @@ public class Connect { * Returns the free memory for the connection */ public long getFreeMemory() throws LibvirtException { - long returnValue = 0; - returnValue = libvirt.virNodeGetFreeMemory(VCP); - if (returnValue == 0) processError(); - return returnValue; + return processErrorIfZero(libvirt.virNodeGetFreeMemory(VCP)); } /** @@ -639,13 +632,11 @@ public class Connect { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectIsEncrypted">Libvirt * Documentation</a> - * @return 1 if encrypted, 0 if not encrypted, -1 on error + * @return 1 if encrypted, 0 if not encrypted * @throws LibvirtException */ public int isEncrypted() throws LibvirtException { - int returnValue = libvirt.virConnectIsEncrypted(VCP); - processError(); - return returnValue; + return processError(libvirt.virConnectIsEncrypted(VCP)); } /** @@ -654,13 +645,11 @@ public class Connect { * @see <a * href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectIsSecure">Libvirt * Documentation</a> - * @return 1 if secure, 0 if not secure, -1 on error + * @return 1 if secure, 0 if not secure * @throws LibvirtException */ public int isSecure() throws LibvirtException { - int returnValue = libvirt.virConnectIsSecure(VCP); - processError(); - return returnValue; + return processError(libvirt.virConnectIsSecure(VCP)); } /** @@ -1141,41 +1130,6 @@ public class Connect { } /** - * call the error handling logic. Should be called after every libvirt call - * - * @throws LibvirtException - */ - protected void processError() throws LibvirtException { - ErrorHandler.processError(libvirt); - } - - /** - * Calls {@link #processError()} when the given libvirt return code - * indicates an error. - * - * @param ret libvirt return code, indicating error if negative. - * @return {@code ret} - * @throws LibvirtException - */ - protected final int processError(int ret) throws LibvirtException { - if (ret < 0) processError(); - return ret; - } - - /** - * Calls {@link #processError()} if {@code arg} is null. - * - * @param arg An arbitrary object returned by libvirt. - * @return {@code arg} - * @throws LibvirtException - */ - protected final <T> T processError(T arg) throws LibvirtException { - if (arg == null) processError(); - return arg; - } - - - /** * Restores a domain saved to disk by Domain.save(). * * @param from @@ -1246,7 +1200,6 @@ public class Connect { public void setConnectionErrorCallback(Libvirt.VirErrorCallback callback) throws LibvirtException { libvirt.virConnSetErrorFunc(VCP, null, callback); - processError(); } /** -- 1.8.5.2.msysgit.0

--- src/main/java/org/libvirt/Library.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/libvirt/Library.java b/src/main/java/org/libvirt/Library.java index 33d3042..95c13cb 100644 --- a/src/main/java/org/libvirt/Library.java +++ b/src/main/java/org/libvirt/Library.java @@ -1,6 +1,7 @@ package org.libvirt; import org.libvirt.jna.Libvirt; +import static org.libvirt.ErrorHandler.processError; import com.sun.jna.Native; import com.sun.jna.Pointer; @@ -24,10 +25,9 @@ final class Library { // Load the native part static { - Libvirt.INSTANCE.virInitialize(); libvirt = Libvirt.INSTANCE; try { - ErrorHandler.processError(Libvirt.INSTANCE); + processError(libvirt.virInitialize()); } catch (Exception e) { e.printStackTrace(); } -- 1.8.5.2.msysgit.0

It was deprecated and is no longer used. --- src/main/java/org/libvirt/ErrorHandler.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/main/java/org/libvirt/ErrorHandler.java b/src/main/java/org/libvirt/ErrorHandler.java index 01e25d6..c2e7337 100644 --- a/src/main/java/org/libvirt/ErrorHandler.java +++ b/src/main/java/org/libvirt/ErrorHandler.java @@ -16,18 +16,6 @@ import com.sun.jna.PointerType; */ public class ErrorHandler { - /** - * Look for the latest error from libvirt not tied to a connection - * - * @param libvirt - * the active connection - * @throws LibvirtException - */ - @Deprecated - static void processError(Libvirt libvirt) throws LibvirtException { - processError(); - } - private static final void processError() throws LibvirtException { virError vError = libvirt.virGetLastError(); if (vError != null) { -- 1.8.5.2.msysgit.0

There's no compelling reason to keep the various processError overloads which were added just to ease refactoring. Consider this squashed in prior of pushing this series. --- src/main/java/org/libvirt/ErrorHandler.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/org/libvirt/ErrorHandler.java b/src/main/java/org/libvirt/ErrorHandler.java index c2e7337..86b8307 100644 --- a/src/main/java/org/libvirt/ErrorHandler.java +++ b/src/main/java/org/libvirt/ErrorHandler.java @@ -49,21 +49,11 @@ public class ErrorHandler { * @return {@code arg} * @throws LibvirtException */ - static final <T extends PointerType> T processError(T arg) throws LibvirtException { + static final <T> T processError(T arg) throws LibvirtException { if (arg == null) processError(); return arg; } - static final Pointer processError(Pointer arg) throws LibvirtException { - if (arg == null) processError(); - return arg; - } - - static final String processError(String str) throws LibvirtException { - if (str == null) processError(); - return str; - } - static final long processErrorIfZero(long ret) throws LibvirtException { if (ret == 0) processError(); return ret; -- 1.8.5.2.msysgit.0
participants (1)
-
Claudio Bley