[libvirt] [libvirt-java] [PATCH 62/65] Connect: add constructors using java.net.URI params

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


Additionally, add an OpenFlags enum to the Connect classes which
implements the BitFlags interface.

This should be used when creating a read-only connection or
to prevent using URI aliases.

Signed-off-by: Claudio Bley <cbley at av-test.de>
---
 src/main/java/org/libvirt/Connect.java |  104 +++++++++++++++++++++++++-------
 1 file changed, 82 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index 8659076..4ab61b3 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -1,5 +1,6 @@
 package org.libvirt;
 
+import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
@@ -24,6 +25,7 @@ import static org.libvirt.Library.libvirt;
 import static org.libvirt.Library.getConstant;
 import static org.libvirt.ErrorHandler.processError;
 import static org.libvirt.ErrorHandler.processErrorIfZero;
+import static org.libvirt.BitFlagsHelper.OR;
 
 import com.sun.jna.Memory;
 import com.sun.jna.NativeLong;
@@ -82,6 +84,24 @@ public class Connect {
         static final int LAST = 13;
     }
 
+    public enum OpenFlags implements BitFlags {
+        /** Open a connection in read-only mode */
+        READONLY(1),
+
+        /** Don't try to resolve URI aliases */
+        NO_ALIASES(2);
+
+        OpenFlags(int v) {
+            this.value = v;
+        }
+
+        @Override
+        public int getBit() {
+            return value;
+        }
+        private final int value;
+    }
+
     /**
      * Get the version of a connection.
      *
@@ -202,9 +222,20 @@ public class Connect {
      * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
      */
     public Connect(String uri) throws LibvirtException {
-        VCP = libvirt.virConnectOpen(uri);
-        // Check for an error
-        processError(VCP);
+        this(uri, null, 0);
+    }
+
+
+    /**
+     * Constructs a read-write Connect object from the supplied URI.
+     *
+     * @param uri
+     *            The connection URI
+     * @throws LibvirtException
+     * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
+     */
+    public Connect(URI uri, OpenFlags... flags) throws LibvirtException {
+        this(uri, null, flags);
     }
 
     /**
@@ -218,13 +249,7 @@ public class Connect {
      * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
      */
     public Connect(String uri, boolean readOnly) throws LibvirtException {
-        if (readOnly) {
-            VCP = libvirt.virConnectOpenReadOnly(uri);
-        } else {
-            VCP = libvirt.virConnectOpen(uri);
-        }
-        // Check for an error
-        processError(VCP);
+        this(uri, null, readOnly ? OpenFlags.READONLY.getBit() : 0);
     }
 
     /**
@@ -240,26 +265,61 @@ public class Connect {
      * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
      */
     public Connect(String uri, ConnectAuth auth, int flags) throws LibvirtException {
-        virConnectAuth vAuth = new virConnectAuth();
-        vAuth.cb = auth;
-        vAuth.cbdata = null;
-        vAuth.ncredtype = auth.credType.length;
-        int[] authInts = new int[vAuth.ncredtype];
-
-        for (int x = 0; x < vAuth.ncredtype; x++) {
-            authInts[x] = auth.credType[x].mapToInt();
+        virConnectAuth vAuth = null;
+
+        if (auth != null) {
+            vAuth = new virConnectAuth();
+            vAuth.cb = auth;
+            vAuth.cbdata = null;
+            vAuth.ncredtype = auth.credType.length;
+            int[] authInts = new int[vAuth.ncredtype];
+
+            for (int x = 0; x < vAuth.ncredtype; x++) {
+                authInts[x] = auth.credType[x].mapToInt();
+            }
+
+            Memory mem = new Memory(4 * vAuth.ncredtype);
+            mem.write(0, authInts, 0, vAuth.ncredtype);
+            vAuth.credtype = mem.share(0);
         }
 
-        Memory mem = new Memory(4 * vAuth.ncredtype);
-        mem.write(0, authInts, 0, vAuth.ncredtype);
-        vAuth.credtype = mem.share(0);
-
         VCP = libvirt.virConnectOpenAuth(uri, vAuth, flags);
         // Check for an error
         processError(VCP);
     }
 
     /**
+     * Constructs a Connect object from the supplied URI, using the supplied
+     * authentication callback
+     *
+     * @param uri
+     *            The connection URI
+     * @param auth
+     *            a ConnectAuth object
+     * @param flags
+     * @throws LibvirtException
+     * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
+     */
+    public Connect(URI uri, ConnectAuth auth, OpenFlags... flags) throws LibvirtException {
+        this(uri.toString(), auth, OR(flags));
+    }
+
+    /**
+     * Constructs a Connect object from the supplied URI, using the supplied
+     * authentication callback
+     *
+     * @param uri
+     *            The connection URI
+     * @param auth
+     *            a ConnectAuth object
+     * @throws LibvirtException
+     * @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
+     */
+    public Connect(URI uri, ConnectAuth auth) throws LibvirtException {
+        this(uri.toString(), auth, 0);
+    }
+
+    /**
      * Computes the most feature-rich CPU which is compatible with all given
      * host CPUs.
      *
-- 
1.7.9.5




More information about the libvir-list mailing list