[PATCH 12/15] Remove the libvirt instance attribute from all classes.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Every class contained a member called "libvirt". This member
was initialized during construction to refer to the Connect.libvirt
member variable, which in turn referred to the static
jna.Libvirt.INSTANCE field.

Add a Library class which contains a static "libvirt" field and
import this field statically for convenient access.
---
 src/main/java/org/libvirt/Connect.java        |   16 +--------------
 src/main/java/org/libvirt/Device.java         |    7 +------
 src/main/java/org/libvirt/Domain.java         |    7 +------
 src/main/java/org/libvirt/DomainSnapshot.java |    7 +------
 src/main/java/org/libvirt/Interface.java      |    7 +------
 src/main/java/org/libvirt/Library.java        |   26 +++++++++++++++++++++++++
 src/main/java/org/libvirt/Network.java        |    7 +------
 src/main/java/org/libvirt/NetworkFilter.java  |    7 +------
 src/main/java/org/libvirt/Secret.java         |    7 +------
 src/main/java/org/libvirt/StoragePool.java    |    7 +------
 src/main/java/org/libvirt/StorageVol.java     |    7 +------
 src/main/java/org/libvirt/Stream.java         |    7 +------
 12 files changed, 37 insertions(+), 75 deletions(-)

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index e02c601..ae306d6 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -15,6 +15,7 @@ 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 com.sun.jna.Memory;
 import com.sun.jna.NativeLong;
@@ -28,16 +29,6 @@ import com.sun.jna.ptr.LongByReference;
  */
 public class Connect {
 
-    // Load the native part
-    static {
-        Libvirt.INSTANCE.virInitialize();
-        try {
-            ErrorHandler.processError(Libvirt.INSTANCE);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
     /**
      * Creates a new connection object from the domain. If all you want is the
      * existing domain's connection, use the getConnection method directly. Thie
@@ -135,11 +126,6 @@ public class Connect {
     protected ConnectionPointer VCP;
 
     /**
-     * The libvirt library
-     */
-    Libvirt libvirt = Libvirt.INSTANCE;
-
-    /**
      * Protected constructor to return a Connection with ConnectionPointer
      */
     Connect(ConnectionPointer ptr) {
diff --git a/src/main/java/org/libvirt/Device.java b/src/main/java/org/libvirt/Device.java
index bc25258..416e06a 100644
--- a/src/main/java/org/libvirt/Device.java
+++ b/src/main/java/org/libvirt/Device.java
@@ -2,6 +2,7 @@ package org.libvirt;
 
 import org.libvirt.jna.DevicePointer;
 import org.libvirt.jna.Libvirt;
+import static org.libvirt.Library.libvirt;
 
 /**
  * A device which is attached to a node
@@ -19,11 +20,6 @@ public class Device {
     private Connect virConnect;
 
     /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs a Device object from a DevicePointer, and a Connect object.
      *
      * @param virConnect
@@ -34,7 +30,6 @@ public class Device {
     Device(Connect virConnect, DevicePointer VDP) {
         this.virConnect = virConnect;
         this.VDP = VDP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index f4f5225..45fba26 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -11,6 +11,7 @@ import org.libvirt.jna.virDomainJobInfo;
 import org.libvirt.jna.virDomainMemoryStats;
 import org.libvirt.jna.virSchedParameter;
 import org.libvirt.jna.virVcpuInfo;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.Native;
 import com.sun.jna.NativeLong;
@@ -86,11 +87,6 @@ public class Domain {
     private Connect virConnect;
 
     /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs a Domain object from a known native DomainPointer, and a
      * Connect object.
      *
@@ -102,7 +98,6 @@ public class Domain {
     Domain(Connect virConnect, DomainPointer VDP) {
         this.virConnect = virConnect;
         this.VDP = VDP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/DomainSnapshot.java b/src/main/java/org/libvirt/DomainSnapshot.java
index 3083ca0..355d9d0 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 org.libvirt.jna.Libvirt;
+import static org.libvirt.Library.libvirt;
 
 public class DomainSnapshot {
 
@@ -15,15 +16,9 @@ public class DomainSnapshot {
      */
     private Connect virConnect;
 
-    /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
     public DomainSnapshot(Connect virConnect, DomainSnapshotPointer VDSP) {
         this.VDSP = VDSP;
         this.virConnect = virConnect;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Interface.java b/src/main/java/org/libvirt/Interface.java
index 17c189d..684adca 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 org.libvirt.jna.Libvirt;
+import static org.libvirt.Library.libvirt;
 
 /**
  * A device which is attached to a node
@@ -24,11 +25,6 @@ public class Interface {
     private Connect virConnect;
 
     /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs an Interface object from an InterfacePointer, and a Connect
      * object.
      *
@@ -40,7 +36,6 @@ public class Interface {
     Interface(Connect virConnect, InterfacePointer VIP) {
         this.virConnect = virConnect;
         this.VIP = VIP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Library.java b/src/main/java/org/libvirt/Library.java
new file mode 100644
index 0000000..035ed06
--- /dev/null
+++ b/src/main/java/org/libvirt/Library.java
@@ -0,0 +1,26 @@
+package org.libvirt;
+
+import org.libvirt.jna.Libvirt;
+
+/**
+ * This class represents an instance of the JNA mapped libvirt
+ * library.
+ *
+ * The library will get loaded when first accessing this class.
+ */
+final class Library {
+    final static Libvirt libvirt;
+
+    // Load the native part
+    static {
+        Libvirt.INSTANCE.virInitialize();
+        libvirt = Libvirt.INSTANCE;
+        try {
+            ErrorHandler.processError(Libvirt.INSTANCE);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private Library() {}
+}
diff --git a/src/main/java/org/libvirt/Network.java b/src/main/java/org/libvirt/Network.java
index acaef0e..bdb0d78 100644
--- a/src/main/java/org/libvirt/Network.java
+++ b/src/main/java/org/libvirt/Network.java
@@ -2,6 +2,7 @@ package org.libvirt;
 
 import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.NetworkPointer;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.Native;
 import com.sun.jna.ptr.IntByReference;
@@ -22,11 +23,6 @@ public class Network {
     protected Connect virConnect;
 
     /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs a Network object from a known native virNetworkPtr, and a
      * Connect object. For use when native libvirt returns a virConnectPtr, i.e.
      * error handling.
@@ -37,7 +33,6 @@ public class Network {
     Network(Connect virConnect, NetworkPointer VNP) {
         this.virConnect = virConnect;
         this.VNP = VNP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/NetworkFilter.java b/src/main/java/org/libvirt/NetworkFilter.java
index 7bc07a5..ba4d2ea 100644
--- a/src/main/java/org/libvirt/NetworkFilter.java
+++ b/src/main/java/org/libvirt/NetworkFilter.java
@@ -2,6 +2,7 @@ package org.libvirt;
 
 import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.NetworkFilterPointer;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.Native;
 
@@ -16,15 +17,9 @@ public class NetworkFilter {
      */
     private Connect virConnect;
 
-    /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
     public NetworkFilter(Connect virConnect, NetworkFilterPointer NFP) {
         this.NFP = NFP;
         this.virConnect = virConnect;
-        libvirt = virConnect.libvirt;
     }
 
     @Override
diff --git a/src/main/java/org/libvirt/Secret.java b/src/main/java/org/libvirt/Secret.java
index 888cdf3..5332e02 100644
--- a/src/main/java/org/libvirt/Secret.java
+++ b/src/main/java/org/libvirt/Secret.java
@@ -2,6 +2,7 @@ package org.libvirt;
 
 import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.SecretPointer;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.Native;
 import com.sun.jna.NativeLong;
@@ -24,15 +25,9 @@ public class Secret {
      */
     private Connect virConnect;
 
-    /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
     Secret(Connect virConnect, SecretPointer VSP) {
         this.virConnect = virConnect;
         this.VSP = VSP;
-        libvirt = virConnect.libvirt;
     }
 
     @Override
diff --git a/src/main/java/org/libvirt/StoragePool.java b/src/main/java/org/libvirt/StoragePool.java
index 29a18ed..2d59f68 100644
--- a/src/main/java/org/libvirt/StoragePool.java
+++ b/src/main/java/org/libvirt/StoragePool.java
@@ -4,6 +4,7 @@ import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.StoragePoolPointer;
 import org.libvirt.jna.StorageVolPointer;
 import org.libvirt.jna.virStoragePoolInfo;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.Native;
 import com.sun.jna.ptr.IntByReference;
@@ -50,11 +51,6 @@ public class StoragePool {
     protected Connect virConnect;
 
     /**
-     * the libvirt instance
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs a VirStoragePool object from a known native virStoragePoolPtr,
      * and a VirConnect object. For use when native libvirt returns a
      * virStoragePoolPtr, i.e. error handling.
@@ -67,7 +63,6 @@ public class StoragePool {
     StoragePool(Connect virConnect, StoragePoolPointer VSPP) {
         this.virConnect = virConnect;
         this.VSPP = VSPP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/StorageVol.java b/src/main/java/org/libvirt/StorageVol.java
index e2bc717..4b9db80 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.Libvirt;
 import org.libvirt.jna.StoragePoolPointer;
 import org.libvirt.jna.StorageVolPointer;
 import org.libvirt.jna.virStorageVolInfo;
+import static org.libvirt.Library.libvirt;
 
 /**
  * An acutal storage bucket.
@@ -43,11 +44,6 @@ public class StorageVol {
     protected Connect virConnect;
 
     /**
-     * the libvirt instance
-     */
-    protected Libvirt libvirt;
-
-    /**
      * Constructs a VirStorageVol object from a known native virStoragePoolPtr,
      * and a VirConnect object. For use when native libvirt returns a
      * virStorageVolPtr, i.e. error handling.
@@ -60,7 +56,6 @@ public class StorageVol {
     StorageVol(Connect virConnect, StorageVolPointer VSVP) {
         this.virConnect = virConnect;
         this.VSVP = VSVP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Stream.java b/src/main/java/org/libvirt/Stream.java
index 06b67cf..84e300c 100644
--- a/src/main/java/org/libvirt/Stream.java
+++ b/src/main/java/org/libvirt/Stream.java
@@ -2,6 +2,7 @@ package org.libvirt;
 
 import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.StreamPointer;
+import static org.libvirt.Library.libvirt;
 
 import com.sun.jna.NativeLong;
 
@@ -19,15 +20,9 @@ public class Stream {
      */
     private Connect virConnect;
 
-    /**
-     * The libvirt connection from the hypervisor
-     */
-    protected Libvirt libvirt;
-
     Stream(Connect virConnect, StreamPointer VSP) {
         this.virConnect = virConnect;
         this.VSP = VSP;
-        libvirt = virConnect.libvirt;
     }
 
     /**
-- 
1.7.9.5

-- 
AV-Test GmbH, Henricistraße 20, 04155 Leipzig, Germany
Phone: +49 341 265 310 19
Web:<http://www.av-test.org>

Eingetragen am / Registered at: Amtsgericht Stendal (HRB 114076)
Geschaeftsfuehrer (CEO): Andreas Marx, Guido Habicht, Maik Morgenstern

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]