[libvirt] [libvirt-java] [PATCH 03/15] Remove processError from Domain class

Claudio Bley cbley at av-test.de
Thu Jan 9 10:07:58 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().
---
 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




More information about the libvir-list mailing list