[libvirt] [libvirt-java] [PATCH 17/65] Remove processError from Domain class

Claudio Bley cbley at av-test.de
Thu Feb 13 15:22:25 UTC 2014


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().

Signed-off-by: Claudio Bley <cbley at av-test.de>
---
 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 b50e182..047ac33 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -13,6 +13,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;
@@ -160,13 +162,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));
     }
 
     /**
@@ -180,8 +180,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void attachDevice(String xmlDesc) throws LibvirtException {
-        libvirt.virDomainAttachDevice(VDP, xmlDesc);
-        processError();
+        processError(libvirt.virDomainAttachDevice(VDP, xmlDesc));
     }
 
     /**
@@ -197,8 +196,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));
     }
 
     /**
@@ -207,14 +205,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);
     }
 
     /**
@@ -234,9 +231,8 @@ public class Domain {
      */
     public DomainBlockStats blockStats(String path) throws LibvirtException {
         virDomainBlockStats stats = new virDomainBlockStats();
-        int success = libvirt.virDomainBlockStats(VDP, path, stats, new SizeT(stats.size()));
-        processError();
-        return success == 0 ? new DomainBlockStats(stats) : null;
+        processError(libvirt.virDomainBlockStats(VDP, path, stats, new SizeT(stats.size())));
+        return new DomainBlockStats(stats);
     }
 
     /**
@@ -251,8 +247,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));
     }
 
 
@@ -267,8 +262,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));
     }
 
     /**
@@ -283,12 +277,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));
     }
 
     /**
@@ -296,12 +289,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));
     }
 
     /**
@@ -313,8 +305,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void destroy() throws LibvirtException {
-        libvirt.virDomainDestroy(VDP);
-        processError();
+        processError(libvirt.virDomainDestroy(VDP));
     }
 
     /**
@@ -328,8 +319,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void detachDevice(String xmlDesc) throws LibvirtException {
-        libvirt.virDomainDetachDevice(VDP, xmlDesc);
-        processError();
+        processError(libvirt.virDomainDetachDevice(VDP, xmlDesc));
     }
 
     /**
@@ -343,8 +333,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
@@ -357,13 +346,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;
         }
 
@@ -379,8 +367,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;
     }
 
@@ -400,9 +387,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public int getID() throws LibvirtException {
-        int returnValue = libvirt.virDomainGetID(VDP);
-        processError();
-        return returnValue;
+        return processError(libvirt.virDomainGetID(VDP));
     }
 
     /**
@@ -418,14 +403,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);
     }
 
     /**
@@ -439,14 +419,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);
     }
 
     /**
@@ -456,9 +431,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());
     }
 
     /**
@@ -471,21 +446,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);
     }
 
     /**
@@ -495,8 +466,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 {
@@ -513,18 +483,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;
@@ -542,8 +508,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);
@@ -559,13 +524,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);
     }
 
     /**
@@ -577,13 +537,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);
     }
 
     /**
@@ -603,8 +558,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];
             }
@@ -622,8 +576,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]);
         }
@@ -642,8 +595,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 {
@@ -656,13 +608,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));
     }
 
     /**
@@ -675,9 +625,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));
     }
 
     /**
@@ -695,8 +643,7 @@ public class Domain {
      */
     public DomainInterfaceStats interfaceStats(String path) throws LibvirtException {
         virDomainInterfaceStats stats = new virDomainInterfaceStats();
-        libvirt.virDomainInterfaceStats(VDP, path, stats, new SizeT(stats.size()));
-        processError();
+        processError(libvirt.virDomainInterfaceStats(VDP, path, stats, new SizeT(stats.size())));
         return new DomainInterfaceStats(stats);
     }
 
@@ -706,13 +653,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));
     }
 
     /**
@@ -722,13 +667,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));
     }
 
     /**
@@ -737,13 +680,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));
     }
 
     /**
@@ -752,13 +693,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));
     }
 
     /**
@@ -766,19 +705,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;
     }
@@ -852,8 +788,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);
     }
 
@@ -896,14 +832,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);
     }
 
@@ -916,13 +851,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));
     }
 
     /**
@@ -956,9 +889,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)));
     }
 
     /**
@@ -981,9 +914,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)));
     }
 
     /**
@@ -1005,16 +936,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));
     }
 
     /**
@@ -1027,8 +949,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void reboot(int flags) throws LibvirtException {
-        libvirt.virDomainReboot(VDP, flags);
-        processError();
+        processError(libvirt.virDomainReboot(VDP, flags));
     }
 
     /**
@@ -1039,8 +960,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void resume() throws LibvirtException {
-        libvirt.virDomainResume(VDP);
-        processError();
+        processError(libvirt.virDomainResume(VDP));
     }
 
     /**
@@ -1051,13 +971,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));
     }
 
     /**
@@ -1071,8 +989,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void save(String to) throws LibvirtException {
-        libvirt.virDomainSave(VDP, to);
-        processError();
+        processError(libvirt.virDomainSave(VDP, to));
     }
 
     /**
@@ -1084,8 +1001,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));
     }
 
     /**
@@ -1097,8 +1013,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)));
     }
 
     /**
@@ -1111,8 +1026,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)));
     }
 
     /**
@@ -1127,8 +1041,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));
     }
 
     /**
@@ -1142,8 +1055,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void setVcpus(int nvcpus) throws LibvirtException {
-        libvirt.virDomainSetVcpus(VDP, nvcpus);
-        processError();
+        processError(libvirt.virDomainSetVcpus(VDP, nvcpus));
     }
 
     /**
@@ -1155,8 +1067,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void shutdown() throws LibvirtException {
-        libvirt.virDomainShutdown(VDP);
-        processError();
+        processError(libvirt.virDomainShutdown(VDP));
     }
 
     /**
@@ -1170,17 +1081,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);
     }
 
     /**
@@ -1209,17 +1115,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);
     }
 
     /**
@@ -1237,8 +1138,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;
@@ -1269,17 +1169,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);
     }
 
     /**
@@ -1290,9 +1185,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));
     }
 
     /**
@@ -1304,8 +1197,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void suspend() throws LibvirtException {
-        libvirt.virDomainSuspend(VDP);
-        processError();
+        processError(libvirt.virDomainSuspend(VDP));
     }
 
     /**
@@ -1314,8 +1206,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public void undefine() throws LibvirtException {
-        libvirt.virDomainUndefine(VDP);
-        processError();
+        processError(libvirt.virDomainUndefine(VDP));
     }
 
     /**
@@ -1327,8 +1218,7 @@ public class Domain {
      * @throws LibvirtException
     */
     public void undefine(int flags) throws LibvirtException {
-        libvirt.virDomainUndefineFlags(VDP, flags);
-        processError();
+        processError(libvirt.virDomainUndefineFlags(VDP, flags));
     }
 
     /**
@@ -1339,13 +1229,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.7.9.5




More information about the libvir-list mailing list