[libvirt] Re: [PATCH 7/6] host ("node") device enumeration - Java bindings

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

 



This patch (for libvirt-java) implements Java bindings for the node
device enumeration functions.

Dave

diff --git a/src/Makefile.am b/src/Makefile.am
index 5200c1d..466a0eb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,6 +14,7 @@ java_libvirt_source_files = \
   org/libvirt/Error.java \
   org/libvirt/Network.java \
   org/libvirt/NodeInfo.java \
+  org/libvirt/NodeDevice.java \
   org/libvirt/SchedBooleanParameter.java \
   org/libvirt/SchedDoubleParameter.java \
   org/libvirt/SchedIntParameter.java \
diff --git a/src/jni/Makefile.am b/src/jni/Makefile.am
index 6e19384..8feed24 100644
--- a/src/jni/Makefile.am
+++ b/src/jni/Makefile.am
@@ -10,7 +10,8 @@ GENERATED = \
   org_libvirt_StoragePool.h \
   org_libvirt_StorageVol_Type.h \
   org_libvirt_StorageVol_DeleteFlags.h \
-  org_libvirt_StorageVol.h
+  org_libvirt_StorageVol.h \
+  org_libvirt_NodeDevice.h
 
 BUILT_SOURCES = $(GENERATED)
 
@@ -31,6 +32,9 @@ org_libvirt_StoragePool.h org_libvirt_StoragePool_BuildFlags.h org_libvirt_Stora
 org_libvirt_StorageVol.h org_libvirt_StorageVol_Type.h org_libvirt_StorageVol_DeleteFlags.h : $(JAVA_CLASS_ROOT)/org/libvirt/StorageVol.class
 	$(JAVAH) -classpath $(JAVA_CLASS_ROOT) org.libvirt.StorageVol
 
+org_libvirt_NodeDevice.h : $(JAVA_CLASS_ROOT)/org/libvirt/NodeDevice.class
+	$(JAVAH) -classpath $(JAVA_CLASS_ROOT) org.libvirt.NodeDevice
+
 lib_LTLIBRARIES = libvirt_jni.la
 libvirt_jni_la_SOURCES = \
   org_libvirt_Network.c \
@@ -38,6 +42,7 @@ libvirt_jni_la_SOURCES = \
   org_libvirt_Domain.c \
   org_libvirt_StoragePool.c \
   org_libvirt_StorageVol.c \
+  org_libvirt_NodeDevice.c \
   generic.h \
   ErrorHandler.c \
   ErrorHandler.h \
diff --git a/src/jni/generic.h b/src/jni/generic.h
index 347c076..e1da179 100644
--- a/src/jni/generic.h
+++ b/src/jni/generic.h
@@ -70,6 +70,16 @@
 	return retval;
 
 /*
+ * Generic macro with a VIROBJ and a String and an int arguments
+ * returning an int
+ */
+#define GENERIC_VIROBJ_STRING_INT__INT(ENV, OBJ, VIROBJ, J_STR, ARG1, VIRFUNC1) \
+	const char *str=(*ENV)->GetStringUTFChars(ENV, J_STR, NULL); \
+	jint retval = (jlong)VIRFUNC1(VIROBJ, str, ARG1);	     \
+	(*ENV)->ReleaseStringUTFChars(ENV, J_STR, str);		     \
+	return retval;
+
+/*
  * Generic macro with a VIROBJ and an String arguments returning a virObject
  * (for functions like *CreateXML* that take no flags)
  */
@@ -81,15 +91,14 @@
 
 /*
  * Generic macro with a VIROBJ and String and int arguments returning a
- * virObject (for functions like *CreateXML* that take a flags)
+ * virObject
  */
-#define GENERIC_VIROBJ_STRING_INT__VIROBJ(ENV, OBJ, VIROBJ, J_XMLDESC, FLAGS, VIRFUNC1) \
-	const char *xmlDesc=(*ENV)->GetStringUTFChars(ENV, J_XMLDESC, NULL); \
-	jlong retval = (jlong)VIRFUNC1(VIROBJ, xmlDesc, FLAGS);		\
-	(*ENV)->ReleaseStringUTFChars(ENV, J_XMLDESC, xmlDesc);		\
+#define GENERIC_VIROBJ_STRING_INT__VIROBJ(ENV, OBJ, VIROBJ, J_STR, ARG1, VIRFUNC1) \
+	const char *str=(*ENV)->GetStringUTFChars(ENV, J_STR, NULL);	\
+	jlong retval = (jlong)VIRFUNC1(VIROBJ, str, ARG1);		\
+	(*ENV)->ReleaseStringUTFChars(ENV, J_STR, str);			\
 	return retval;
 
-
 /*
  * Generic macro for the *getAutoStart functions
  */
@@ -150,6 +159,61 @@
 	return j_names;
 
 /*
+ * Generic macro for the *List* functions that return an array of strings
+ * and take a single int arg.
+ * VIRFUNC1 is the *List* function
+ * VIRFUNC2 is the corresponding *NumOf* function
+ */
+#define GENERIC_INT__LIST_STRINGARRAY(ENV, OBJ, VIROBJ, JINT, VF1, VF2) \
+	int maxnames;							\
+	char **names;							\
+	int c;								\
+	jobjectArray j_names=NULL;					\
+	if((maxnames = VF2(VIROBJ, JINT))<0)				\
+		return NULL;						\
+	names= (char**)calloc(maxnames, sizeof(char*));			\
+	if(VF1(VIROBJ, names, maxnames, JINT)>=0){			\
+		j_names= (jobjectArray)(*ENV)->NewObjectArray(ENV, maxnames, \
+			(*ENV)->FindClass(ENV,"java/lang/String"),	\
+			(*ENV)->NewStringUTF(ENV,""));			\
+		for(c=0; c<maxnames; c++){				\
+			(*ENV)->SetObjectArrayElement(ENV, j_names, c,	\
+			(*ENV)->NewStringUTF(ENV, names[c]));		\
+		}							\
+	}								\
+	free(names);							\
+	return j_names;
+
+/*
+ * Generic macro for the *List* functions that return an array of strings
+ * and take a single string arg and an int arg.
+ * VIRFUNC1 is the *List* function
+ * VIRFUNC2 is the corresponding *NumOf* function
+ */
+#define GENERIC_STRING_INT__LIST_STRINGARRAY(ENV, OBJ, VIROBJ, J_STR, JINT, VF1, VF2) \
+	int maxnames;							\
+	char **names;							\
+	int c;								\
+	jobjectArray j_names=NULL;					\
+	const char *str = (*ENV)->GetStringUTFChars(ENV, J_STR, NULL);	\
+	if((maxnames = VF2(VIROBJ, str, JINT))<0)			\
+		goto cleanup;						\
+	names= (char**)calloc(maxnames, sizeof(char*));			\
+	if(VF1(VIROBJ, str, names, maxnames, JINT)>=0){			\
+		j_names= (jobjectArray)(*ENV)->NewObjectArray(ENV, maxnames, \
+			(*ENV)->FindClass(ENV,"java/lang/String"),	\
+			(*ENV)->NewStringUTF(ENV,""));			\
+		for(c=0; c<maxnames; c++){				\
+			(*ENV)->SetObjectArrayElement(ENV, j_names, c,	\
+			(*ENV)->NewStringUTF(ENV, names[c]));		\
+		}							\
+	}								\
+	free(names);							\
+  cleanup:								\
+         (*ENV)->ReleaseStringUTFChars(ENV, J_STR, str);		\
+	return j_names;
+
+/*
  * Generic macro for the *LookupBy* functions that take a string and return a VirObject
  */
 #define GENERIC_LOOKUPBY_STRING(ENV, OBJ, VIROBJ, J_STRINGID, VIRFUNC1)	\
diff --git a/src/jni/org_libvirt_Connect.c b/src/jni/org_libvirt_Connect.c
index cbf437c..e9e69f0 100644
--- a/src/jni/org_libvirt_Connect.c
+++ b/src/jni/org_libvirt_Connect.c
@@ -361,3 +361,33 @@ JNIEXPORT jlong JNICALL Java_org_libvirt_Connect__1virStoragePoolLookupByName
 	GENERIC_LOOKUPBY_STRING(env, obj, (virConnectPtr)VCP, j_name, virStoragePoolLookupByName)
 }
 
+JNIEXPORT jobjectArray JNICALL Java_org_libvirt_Connect__1nodeListDevices
+(JNIEnv *env, jobject obj, jlong VCP, jint flags){
+        GENERIC_INT__LIST_STRINGARRAY(env, obj, (virConnectPtr)VCP, flags, virNodeListDevices, virNodeNumOfDevices)
+}
+
+JNIEXPORT jobjectArray JNICALL Java_org_libvirt_Connect__1nodeListDevicesByCap
+(JNIEnv *env, jobject obj, jlong VCP, jstring j_cap, jint flags){
+        GENERIC_STRING_INT__LIST_STRINGARRAY(env, obj, (virConnectPtr)VCP, j_cap, flags, virNodeListDevicesByCap, virNodeNumOfDevicesByCap)
+}
+
+JNIEXPORT jint JNICALL Java_org_libvirt_Connect__1nodeNumOfDevices
+(JNIEnv *env, jobject obj, jlong VCP, jint flags){
+	GENERIC__VIROBJ_INT__INT(env, obj, (virConnectPtr)VCP, flags, virNodeNumOfDevices);
+}
+
+JNIEXPORT jint JNICALL Java_org_libvirt_Connect__1nodeNumOfDevicesByCap
+(JNIEnv *env, jobject obj, jlong VCP, jstring j_cap, jint flags){
+	GENERIC__VIROBJ_STRING_INT__INT(env, obj, (virConnectPtr)VCP, j_cap, flags, virNodeNumOfDevicesByCap);
+}
+
+JNIEXPORT jlong JNICALL Java_org_libvirt_Connect__1nodeDeviceLookupByName
+(JNIEnv *env, jobject obj, jlong VCP, jstring j_name){
+	GENERIC_LOOKUPBY_STRING(env, obj, (virConnectPtr)VCP, j_name, virNodeDeviceLookupByName)
+}
+
+JNIEXPORT jlong JNICALL Java_org_libvirt_Connect__1nodeDeviceCreate
+(JNIEnv *env, jobject obj, jlong VCP, jstring j_xml, jint flags){
+        GENERIC_VIROBJ_STRING_INT__VIROBJ(env, obj, (virConnectPtr)VCP, j_xml, flags, virNodeDeviceCreate)
+}
+
diff --git a/src/jni/org_libvirt_NodeDevice.c b/src/jni/org_libvirt_NodeDevice.c
new file mode 100644
index 0000000..1d8d4d0
--- /dev/null
+++ b/src/jni/org_libvirt_NodeDevice.c
@@ -0,0 +1,35 @@
+#include <libvirt/libvirt.h>
+#include "org_libvirt_NodeDevice.h"
+#include "generic.h"
+
+
+JNIEXPORT jstring JNICALL Java_org_libvirt_NodeDevice__1getName
+(JNIEnv *env, jobject obj, jlong VNDP){
+        GENERIC__VIROBJ__CONSTSTRING(env, obj, (virNodeDevicePtr)VNDP, virNodeDeviceGetName)
+                }
+
+JNIEXPORT jstring JNICALL Java_org_libvirt_NodeDevice__1getParent
+(JNIEnv *env, jobject obj, jlong VNDP){
+        GENERIC__VIROBJ__CONSTSTRING(env, obj, (virNodeDevicePtr)VNDP, virNodeDeviceGetParent)
+                }
+
+JNIEXPORT jobjectArray JNICALL Java_org_libvirt_NodeDevice__1listCapabilities
+(JNIEnv *env, jobject obj, jlong VNDP){
+        GENERIC_LIST_STRINGARRAY(env, obj, (virNodeDevicePtr)VNDP, virNodeDeviceListCaps, virNodeDeviceNumOfCaps)
+                }
+
+JNIEXPORT jint JNICALL Java_org_libvirt_NodeDevice__1numOfCapabilities
+(JNIEnv *env, jobject obj, jlong VNDP){
+        return (jint)virNodeDeviceNumOfCaps((virNodeDevicePtr)VNDP);
+                }
+
+JNIEXPORT jstring JNICALL Java_org_libvirt_NodeDevice__1getXMLDesc
+(JNIEnv *env, jobject obj, jlong VNDP, jint flags){
+        GENERIC_VIROBJ_INT__STRING(env, obj, (virNodeDevicePtr)VNDP, flags, virNodeDeviceGetXMLDesc)
+                }
+
+JNIEXPORT void JNICALL Java_org_libvirt_NodeDevice__1destroy
+(JNIEnv *env, jobject obj, jlong VNDP, jint flags){
+        virNodeDeviceDestroy((virNodeDevicePtr)VNDP, flags);
+                }
+
diff --git a/src/org/libvirt/Connect.java b/src/org/libvirt/Connect.java
index 4271937..7f0a7b3 100644
--- a/src/org/libvirt/Connect.java
+++ b/src/org/libvirt/Connect.java
@@ -698,4 +698,89 @@ public class Connect {
 	private native long _virStorageVolLookupByPath(long VCP, String path);
 
 
+	/**
+	 * Lists the names of currently-known node devices
+	 *
+	 * @param flags flags future flags, use 0 for now
+	 * @return an Array of Strings containing the names of all known node devices
+	 * @throws LibvirtException
+	 */
+	public String[] nodeListDevices(int flags) throws LibvirtException {
+	    return _nodeListDevices(VCP, flags);
+	}
+
+	private native String[] _nodeListDevices(long VCP, int flags)
+		throws LibvirtException;
+
+	/**
+	 * Lists the names of currently-known node devices with the given capability
+	 *
+	 * @param cap the capability name
+	 * @param flags future flags, use 0 for now
+	 * @return an Array of Strings containing the names of devices with that cap
+	 * @throws LibvirtException
+	 */
+	public String[] nodeListDevicesByCap(String cap, int flags) throws LibvirtException {
+		return _nodeListDevicesByCap(VCP, cap, flags);
+	}
+	private native String[] _nodeListDevicesByCap(long VCP, String cap, int flags)
+		throws LibvirtException;
+
+	/**
+	 * Gets the number of currently-known node devices
+	 *
+	 * @param flags future flags, use 0 for now
+	 * @return the count of all known node devices
+	 * @throws LibvirtException
+	 */
+	public int nodeNumOfDevices(int flags) throws LibvirtException {
+		return _nodeNumOfDevices(VCP, flags);
+	}
+
+	private native int _nodeNumOfDevices(long VCP, int flags)
+		throws LibvirtException;
+
+	/**
+	 * Gets the number of currently-known node devices with the given capability
+	 *
+	 * @param cap the capability name
+	 * @param flags future flags, use 0 for now
+	 * @return the count of devices with that cap
+	 * @throws LibvirtException
+	 */
+	public int nodeNumOfDevicesByCap(String cap, int flags) throws LibvirtException {
+		return _nodeNumOfDevicesByCap(VCP, cap, flags);
+	}
+	private native int _nodeNumOfDevicesByCap(long VCP, String cap, int flags)
+		throws LibvirtException;
+
+	/**
+	 * Looks up a NodeDevice from its name
+	 *
+	 * @param name the node device name
+	 * @return the NodeDevice with that name, or NULL if not found
+	 * @throws LibvirtException
+	 */
+	public NodeDevice nodeDeviceLookupByName(String name) throws LibvirtException {
+		return new NodeDevice(this, _nodeDeviceLookupByName(VCP, name));
+	}
+
+	private native long _nodeDeviceLookupByName(long VCP, String name)
+		throws LibvirtException;
+
+	/**
+	 * Creates a new node device from an XML description; works only
+	 * for certain kinds of devices.
+	 *
+	 * @param xml XML description of the new device
+	 * @param flags future flags, use 0 for now
+	 * @return the new node device
+	 * @throws LibvirtException
+	 */
+	public NodeDevice nodeDeviceCreate(String xml, int flags) throws LibvirtException {
+		return new NodeDevice(this, _nodeDeviceCreate(VCP, xml, flags));
+	}
+
+	private native long _nodeDeviceCreate(long VCP, String xml, int flags)
+		throws LibvirtException;
 }
diff --git a/src/org/libvirt/NodeDevice.java b/src/org/libvirt/NodeDevice.java
new file mode 100644
index 0000000..861ab4c
--- /dev/null
+++ b/src/org/libvirt/NodeDevice.java
@@ -0,0 +1,115 @@
+package org.libvirt;
+
+
+/**
+ * The NodeDevice object represents a device on the host machine (node).
+ *
+ * @author dlively
+ *
+ */
+public class NodeDevice {
+    
+	/**
+	 * the native virNodeDevicePtr.
+	 */
+	private long  VNDP;
+
+	/**
+	 * The VirConnect Object
+	 */
+	private Connect virConnect;
+
+
+	/**
+	 * Constructs a VirNodeDevice object from a known native virNodeDevicePtr, and
+	 * a VirConnect object.  For use when native libvirt returns a virNodeDevicePtr,
+	 * i.e. error handling.
+	 *
+	 * @param virConnect the Domain's hypervisor
+	 * @param VNDP the native virNodeDevicePtr
+	 */
+	NodeDevice(Connect virConnect, long VNDP){
+		this.virConnect = virConnect;
+		this.VNDP = VNDP;
+	}
+
+
+	/**
+	 * Get device name
+	 *
+	 * @return the node device name
+	 * @throws LibvirtException
+	 */
+	public String getName() throws LibvirtException {
+		return _getName(VNDP);
+	}
+
+	private native String _getName(long VNDP) throws LibvirtException;
+
+
+	/**
+	 * Get the name of device parent (if any)
+	 *
+	 * @return the parent's name, or NULL if no parent
+	 * @throws LibvirtException
+	 */
+	public String getParent() throws LibvirtException {
+		return _getParent(VNDP);
+	}
+
+	private native String _getParent(long VNDP) throws LibvirtException;
+
+
+	/**
+	 * Lists capabilities supported by device
+	 *
+	 * @return an array of the capability names
+	 * @throws LibvirtException
+	 */
+	public String[] listCapabilities() throws LibvirtException {
+		return _listCapabilities(VNDP);
+	}
+
+	private native String[] _listCapabilities(long VNDP) throws LibvirtException;
+
+
+	/**
+	 * Get number of capabilities supported by device
+	 *
+	 * @return the number of capabilities the device supports
+	 * @throws LibvirtException
+	 */
+	public int numOfCapabilities() throws LibvirtException {
+		return _numOfCapabilities(VNDP);
+	}
+
+	private native int _numOfCapabilities(long VNDP) throws LibvirtException;
+
+
+	/**
+	 * Get XML describing device details
+	 *
+	 * @param flags future flags, use 0 for now
+	 * @return an XML string describing the device details
+	 * @throws LibvirtException
+	 */
+	public String getXMLDesc(int flags) throws LibvirtException {
+		return _getXMLDesc(VNDP, flags);
+	}
+
+	private native String _getXMLDesc(long VNDP, int flags)
+		throws LibvirtException;
+
+
+	/**
+	 * Tear down device created with Connect.nodeDeviceCreate()
+	 *
+	 * @param flags future flags, use 0 for now
+	 * @throws LibvirtException
+	 */
+	public void destroy(int flags) throws LibvirtException {
+		_destroy(VNDP, flags);
+	}
+
+	private native void _destroy(long VNDP, int flags) throws LibvirtException;
+}
--
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]