Using D-Bus in Java
I think one of the most fun things I've got to learn during Summer of Code is d-bus. I had to learn it to open instant message conversations in the Pidgin module. And I guess I can present a noobish tutorial to do something simple in Java using d-bus. Before you start, you can refer to this excellent manual here.
Pre-requisites-
1. you need JVM and JDK (the version I used was openJDK 1.6 taken from the standard repository)
2. dbus-java (which requires you to install libmatthew as well).
The task at hand is to make an empty note in Tomboy using a java program.
Step-1 Extend the required interface for the function you're looking to implement (you can use a software called d-feet which can help you analyze various buses, object paths and interface names)
package org.gnome.Tomboy;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
/**
*
* @author sourcemorph
*/
@DBusInterfaceName("org.gnome.Tomboy.RemoteControl")
public interface RemoteControl extends DBusInterface {
public String CreateNote();
}
[The annotation is important, and you need to mention the corresponding object path of the interface you're extending, here I have declared just one function that we needed, but you can choose from the available functions from d-feet].
Step-2 Write a main class to get an object of this interface type and execute the function.
/**
*
* @author sourcemorph
*/
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.gnome.Tomboy.RemoteControl;
public class NewClass {
private static String ObjectPath = "/org/gnome/Tomboy/RemoteControl";
private static String ServiceBusName = "org.gnome.Tomboy";
private static DBusConnection conn;
public NewClass() {
try {
conn = DBusConnection.getConnection(DBusConnection.SESSION);
RemoteControl c = (RemoteControl) conn.getRemoteObject(ServiceBusName, ObjectPath);
c.CreateNote();
} catch(DBusException ex) {
ex.printStackTrace();
}
}
public static void main(String [] args) {
NewClass n = new NewClass();
}
}
Fairly simple. By the way its something really admirable about D-Bus because now my Java code can interact with processes which could have been coded in C sharp, Python etc. I am going to call methods on the Pidgin interface through Java code, and learning D-Bus was totally worth the effort.
PS: thanks to my mentor Stephen Shaw for patiently referring me to the README files when dbus-java wasn't compiling.. :) and also for d-feet, its awesome!
June 28, 2009 at 3:11 AM
You can also let the interface get generated automatically with the program CreateInterface that comes with dbus-java:
Just open a terminal, go to the top level source directory of your project and type
CreateInterface -s -f org.gnome.Tomboy /org/gnome/Tomboy/RemoteControl
and the interface should get generated if Tomboy is running.
June 28, 2009 at 4:03 PM
@Joachim.. yeah, thats a cool thing, takes care of the manual work of creating an interface.
Post a Comment