mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
java: Implement the event API.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# libguestfs Java bindings
|
||||
# Copyright (C) 2009-2012 Red Hat Inc.
|
||||
# Copyright (C) 2009-2013 Red Hat Inc.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -33,13 +33,15 @@ include $(srcdir)/Makefile.inc
|
||||
|
||||
java_sources = \
|
||||
$(java_built_sources) \
|
||||
com/redhat/et/libguestfs/EventCallback.java \
|
||||
com/redhat/et/libguestfs/LibGuestFSException.java
|
||||
|
||||
java_tests = \
|
||||
Bindtests.java \
|
||||
t/GuestFS005Load.java \
|
||||
t/GuestFS010Basic.java \
|
||||
t/GuestFS080OptArgs.java
|
||||
t/GuestFS080OptArgs.java \
|
||||
t/GuestFS400Events.java
|
||||
|
||||
EXTRA_DIST = \
|
||||
$(java_sources) \
|
||||
|
||||
33
java/com/redhat/et/libguestfs/EventCallback.java
Normal file
33
java/com/redhat/et/libguestfs/EventCallback.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/* libguestfs Java bindings
|
||||
* Copyright (C) 2009-2013 Red Hat Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package com.redhat.et.libguestfs;
|
||||
|
||||
/**
|
||||
* Event callback interface.
|
||||
* <p>
|
||||
* This is the interface for event callbacks. See the
|
||||
* {@link GuestFS#set_event_callback set_event_callback method}
|
||||
* for details.
|
||||
*
|
||||
* @author rjones
|
||||
* @see GuestFS
|
||||
*/
|
||||
public interface EventCallback {
|
||||
public void event (long event, int eh, String buffer, long[] array);
|
||||
}
|
||||
@@ -40,6 +40,36 @@ is the error message (a C<String>).
|
||||
|
||||
Calling any method on a closed handle raises the same exception.
|
||||
|
||||
=head2 EVENTS
|
||||
|
||||
The L<libguestfs event API|guestfs(3)/EVENTS> is fully supported from
|
||||
Java. Create a class which implements the C<EventCallback> interface,
|
||||
create an instance of this class, and then call the C<GuestFS#set_event_callback>
|
||||
method to register this instance. The C<event> method of the class is
|
||||
called when libguestfs generates an event.
|
||||
|
||||
For example, this will print all trace events:
|
||||
|
||||
GuestFS g = new GuestFS ();
|
||||
g.set_trace (true);
|
||||
g.set_event_callback (
|
||||
new EventCallback () {
|
||||
public void event (long event, int eh,
|
||||
String buffer, long[] array) {
|
||||
System.out.println (GuestFS.eventToString (event) +
|
||||
": " + buffer);
|
||||
}
|
||||
},
|
||||
GuestFS.EVENT_TRACE);
|
||||
g.add_drive_ro ("disk.img");
|
||||
// etc.
|
||||
|
||||
The output looks similar to this:
|
||||
|
||||
EVENT_TRACE: add_drive_ro "disk.img"
|
||||
EVENT_TRACE: add_drive_ro = 0
|
||||
// etc.
|
||||
|
||||
=head1 EXAMPLE 1: CREATE A DISK IMAGE
|
||||
|
||||
@EXAMPLE1@
|
||||
|
||||
95
java/t/GuestFS400Events.java
Normal file
95
java/t/GuestFS400Events.java
Normal file
@@ -0,0 +1,95 @@
|
||||
/* libguestfs Java bindings
|
||||
* Copyright (C) 2013 Red Hat Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import com.redhat.et.libguestfs.*;
|
||||
|
||||
public class GuestFS400Events
|
||||
{
|
||||
static class PrintEvent implements EventCallback
|
||||
{
|
||||
public void event (long event, int eh, String buffer, long[] array)
|
||||
{
|
||||
String msg = "event=" + GuestFS.eventToString (event) + " " +
|
||||
"eh=" + eh + " ";
|
||||
|
||||
if (buffer != null)
|
||||
msg += "buffer='" + buffer + "' ";
|
||||
|
||||
if (array.length > 0) {
|
||||
msg += "array[" + array.length + "]={";
|
||||
for (int i = 0; i < array.length; ++i)
|
||||
msg += " " + array[i];
|
||||
msg += " }";
|
||||
}
|
||||
|
||||
System.out.println ("java event logged: " + msg);
|
||||
}
|
||||
}
|
||||
|
||||
static class CloseInvoked extends PrintEvent
|
||||
{
|
||||
private int close_invoked = 0;
|
||||
|
||||
public void event (long event, int eh, String buffer, long[] array)
|
||||
{
|
||||
super.event (event, eh, buffer, array);
|
||||
close_invoked++;
|
||||
}
|
||||
|
||||
public int getCloseInvoked ()
|
||||
{
|
||||
return close_invoked;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main (String[] argv)
|
||||
{
|
||||
try {
|
||||
GuestFS g = new GuestFS ();
|
||||
|
||||
// Grab all messages into an event handler that just
|
||||
// prints each event.
|
||||
g.set_event_callback (new PrintEvent (),
|
||||
GuestFS.EVENT_APPLIANCE|GuestFS.EVENT_LIBRARY|
|
||||
GuestFS.EVENT_TRACE);
|
||||
|
||||
// Check that the close event is invoked.
|
||||
CloseInvoked ci = new CloseInvoked ();
|
||||
g.set_event_callback (ci, GuestFS.EVENT_CLOSE);
|
||||
|
||||
// Now make sure we see some messages.
|
||||
g.set_trace (true);
|
||||
g.set_verbose (true);
|
||||
|
||||
// Do some stuff.
|
||||
g.add_drive_ro ("/dev/null");
|
||||
g.set_autosync (true);
|
||||
|
||||
// Close the handle.
|
||||
assert ci.getCloseInvoked() == 0;
|
||||
g.close ();
|
||||
assert ci.getCloseInvoked() == 1;
|
||||
}
|
||||
catch (Exception exn) {
|
||||
System.err.println (exn);
|
||||
System.exit (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user