## File: dbus-java.tex

package info (click to toggle)
dbus-java 2.8-9
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 \documentclass[a4paper,12pt]{article} % % D-Bus Java Implementation % Copyright (c) 2005-2006 Matthew Johnson % % This program is free software; you can redistribute it and/or modify it % under the terms of either the GNU Lesser General Public License Version 2 or the % Academic Free Licence Version 2.1. % % Full licence texts are included in the COPYING file with this program. % \usepackage{fullpage} \usepackage{ifthen} \ifx\pdfoutput\undefined \usepackage{hyperref} \else \usepackage[ bookmarks, bookmarksopen, bookmarksnumbered=false, colorlinks, pdfpagemode=None, urlcolor=black, citecolor=black, linkcolor=black, pagecolor=black, plainpages=false, pdfpagelabels, ]{hyperref} \fi \author{Matthew Johnson\\dbus@matthew.ath.cx} \title{D-Bus programming in Java 1.5} \begin{document} \def\javadocroot/{http://dbus.freedesktop.org/doc/dbus-java/api/} \maketitle \tableofcontents \listoffigures \listoftables \section{Introduction} This document describes how to use the Java implementation of D-Bus. D-Bus is an IPC mechanism which at a low level uses message passing over Unix Sockets or IP. D-Bus models its messages as either function calls on remote objects, or signals emitted from them. Java is an object-oriented language and this implementation attempts to match the D-Bus IPC model to the Java object model. The implementation also make heavy use of threads and exceptions. The programmer should be careful to take care of synchronisation issues in their code. All method calls by remote programs on exported objects and all signal handlers are run in new threads. Any calls on remote objects may throw {\tt DBusExecutionException}, which is a runtime exception and so the compiler will not remind you to handle it. The Java D-Bus API is also documented in the JavaDoc\footnote{\url{\javadocroot/}}, D-Bus is described in the specification\footnote{http://dbus.freedesktop.org/doc/dbus-specification.html} and the API documentation\footnote{http://dbus.freedesktop.org/doc/api/html/}. \subsection{Protocol Implementation} This library is a native Java implementation of the D-Bus protocol and not a wrapper around the C reference implementation. \subsection{Dependencies} This library requires Java 1.5-compatible VM and compiler (either Sun, or ecj+jamvm with classpath-generics newer than 0.19) and the unix socket, debug and hexdump libraries from \url{http://www.matthew.ath.cx/projects/java/}. \subsection{D-Bus Terminology} D-Bus has several notions which are exposed to the users of the Java implementation. \subsubsection{Bus Names} Programs on the bus are issued a unique identifier by the bus. This is guaranteed to be unique within one run of the bus, but is assigned sequentially to each new connection. There are also so called well-known bus names which a device can request on the bus. These are of the form {\em org.freedesktop.DBus''}, and any program can request them if they are not already owned. \subsubsection{Interfaces} All method calls and signals are specified using an interface, similar to those in Java. When executing a method or sending a signal you specify the interface the method belongs to. These are of the form {\em org.freedesktop.DBus''}. \subsubsection{Object Paths} A program may expose more than one object which implements an interface. Object paths of the form {\em /org/freedesktop/DBus''} are used to distinguish objects. \subsubsection{Member Names} Methods and Signals have names which identify them within an interface. D-Bus does not support method overloading, only one method or signal should exist with each name. \subsubsection{Errors} A reply to any message may be an error condition. In which case you reply with an error message which has a name of the form {\em org.freedesktop.DBus.Error.ServiceUnknown''}. \section{DBusConnection} The {\tt DBusConnection\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusConnection.html}}} class provides methods for connecting to the bus, exporting objects, sending signals and getting references to remote objects. {\tt DBusConnection} is a singleton class, multiple calls to {\tt getConnection} will return the same bus connection. \begin{verbatim} conn = DBusConnection.getConnection(DBusConnection.SESSION); \end{verbatim} This creates a connection to the session bus, or returns the existing connection. \begin{verbatim} conn.addSigHandler(TestSignalInterface.TestSignal.class, new SignalHandler()); \end{verbatim} This sets up a signal handler for the given signal type. SignalHandler.handle will be called in a new thread with an instance of TestSignalInterface.TestSignal when that signal is recieved. \begin{verbatim} conn.sendSignal(new TestSignalInterface.TestSignal( "/foo/bar/com/Wibble", "Bar", new UInt32(42))); \end{verbatim} This sends a signal of type {\tt TestSignalInterface.TestSignal}, from the object {\em /foo/bar/com/Wibble''} with the arguments {\em Bar''} and {\tt UInt32(42)}. \begin{verbatim} conn.exportObject("/Test", new testclass()); \end{verbatim} This exports the {\tt testclass} object on the path {\em /Test''} \begin{verbatim} Introspectable intro = (Introspectable) conn.getRemoteObject( "foo.bar.Test", "/Test", Introspectable.class); \end{verbatim} This gets a reference to the {\em /Test''} object on the process with the name {\em foo.bar.Test''} . The object implements the {\tt Introspectable} interface, and calls may be made to methods in that interface as if it was a local object. \begin{verbatim} String data = intro.Introspect(); \end{verbatim} The Runtime Exception {\tt DBusExecutionException} may be thrown by any remote method if any part of the execution fails. \subsection{Asynchronous Calls} Calling a method on a remote object is synchronous, that is the thread will block until it has a reply. If you do not want to block you can use an asynchronous call. There are two ways of making asynchronous calls. You can either call the {\tt callMethodAsync} function on the connection object, in which case you are returned a {\tt DBusAsyncReply\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusAsyncReply.html}}} object which can be used to check for a reply and get the return value. This is demonstrated in figure \ref{fig:async}. \begin{figure}[htb] \begin{center} \begin{verbatim} DBusAsyncReply stuffreply = conn.callMethodAsync(remoteObject, "methodname", arg1, arg2); ... if (stuffreply.hasReply()) { Boolean b = stuffreply.getReply(); ... } \end{verbatim} \end{center} \caption{Calling an asynchronous method} \label{fig:async} \end{figure} Alternatively, you can register a callback with the connection using the {\tt callWithCallback} function on the connection object. In this case, your callback class (implementing the {\tt CallbackHandler\footnote{\url{\javadocroot/org/freedesktop/dbus/CallbackHandler.html}}} interface will be called when the reply is returned from the bus. \section{DBusInterface} To call methods or expose methods on D-Bus you need to define them with their exact signature in a Java interface. The full name of this interface must be the same as the D-Bus interface they represent. In addition, D-Bus interface names must contain at least one period. This means that DBusInterfaces cannot be declared without being in a package. For example, if I want to expose methods on the interface {\em org.freedesktop.DBus''} I would define a Java interface in the package {\tt org.freedesktop} called {\tt DBus}. This would be in the file {\tt org/freedesktop/DBus.java} as normal. Any object wanting to export these methods would implement {\tt org.freedesktop.DBus}. Any interfaces which can be exported over D-Bus must extend {\tt DBusInterface\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusInterface.html}}}. A class may implement more than one exportable interface, all public methods declared in an interface which extend {\tt DBusInterface} will be exported. A sample interface definition is given in figure~\ref{fig:interface}, and a class which implements it in figure~\ref{fig:class}. More complicated definitions can be seen in the test classes\footnote{\url{\javadocroot/org/freedesktop/dbus/test/TestRemoteInterface2.java} \url{\javadocroot/org/freedesktop/dbus/test/TestRemoteInterface.java}}. All method calls by other programs on objects you export over D-Bus are executed in their own thread. {\tt DBusInterface} itself specifies one method \verb&boolean isRemote()&. If this is executed on a remote object it will always return true. Local objects implementing a remote interface must implement this method to return false. \begin{figure}[htb] \begin{center} \begin{verbatim} package org.freedesktop; import org.freedesktop.dbus.UInt32; import org.freedesktop.dbus.DBusInterface; public interface DBus extends DBusInterface { public boolean NameHasOwner(String name); public UInt32 RequestName(String name, UInt32 flags); } \end{verbatim} \end{center} \caption{An interface which exposes two methods} \label{fig:interface} \end{figure} \begin{figure}[htb] \begin{center} \begin{verbatim} package my.real.implementation; import org.freedesktop.dbus.DBus; import org.freedesktop.dbus.UInt32; public class DBusImpl implements DBus { Vector names; public boolean NameHasOwner(String name) { if (names.contains(name)) return true; else return false; } public UInt32 RequestName(String name, UInt32 flags) { names.add(name); return new UInt32(0); } public boolean isRemote() { return false; } } \end{verbatim} \end{center} \caption{A class providing a real implementation which can be exported} \label{fig:class} \end{figure} \subsection{Interface name overriding} It is highly recommended that the Java interface and package name match the D-Bus interface. However, if, for some reason, this is not possible then the name can be overridden by use of an Annotation. To override the Java interface name you should add an annotation to the interface of {\tt DBusInterfaceName\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusInterfaceName.html}}} with a value of the desired D-Bus interface name. An example of this can be seen in figure \ref{fig:interfacename}. \begin{figure}[htb] \begin{center} \begin{verbatim} package my.package; import org.freedesktop.dbus.DBusInterface; import org.freedesktop.dbus.DBusInterfaceName; @DBusInterfaceName("my.otherpackage.Remote") public interface Remote extends DBusInterface { ... } \end{verbatim} \end{center} \caption{Overloading the name of an interface.} \label{fig:interfacename} \end{figure} If you have signals which are declared in a renamed interface (see below for signals) then when adding a signal handler you {\em must} use an {\tt addSigHandler} method which takes a class object corresponding to that signal. If you do not then receiving the signal will fail. \section{DBusSignal} Signals are also declared as part of an interface. The Java API models these as inner classes within an interface. The containing interface must extend {\tt DBusInterface}, and the inner classes representing the signals must extend {\tt DBusSignal\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusSignal.html}}}. The Signal name is derived from the name of this inner class, and the interface from its containing interface. Signals can take parameters as methods can (although they cannot return anything). For the reflection to work, a Signal declare a single constructor of the correct type. The constructor must take the object path they are being emitted from as their first (String) argument, followed by the other parameters in order. They must also call the superclass constructor with the same parameters. A full definition of a signal can be seen in figure~\ref{fig:signal}. Again, more complicated definitions are available in the test classes\footnote{\url{\javadocroot/org/freedesktop/dbus/test/TestSignalInterface.html}}. \begin{figure}[htb] \begin{center} \begin{verbatim} package org.freedesktop; import org.freedesktop.dbus.DBusInterface; import org.freedesktop.dbus.DBusSignal; import org.freedesktop.dbus.exceptions.DBusException; public interface DBus extends DBusInterface { public class NameAcquired extends DBusSignal { public final String name; public NameAcquired(String path, String name) throws DBusException { super(path, name); this.name = name; } } } \end{verbatim} \end{center} \caption{A Signal with one parameter} \label{fig:signal} \end{figure} \section{DBusExecutionException} If you wish to report an error condition in a method call you can throw an instance of {\tt DBusExecutionException\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusExecutionException.html}}}. This will be sent back to the caller as an error message, and the error name is taken from the class name of the exception. For example, if you wanted to report an unknown method you would define an exception as in figure \ref{fig:exceptiondef} and then throw it in your method as in figure \ref{fig:exceptioncall}. \begin{figure}[htb] \begin{center} \begin{verbatim} package org.freedesktop.DBus.Error; import org.freedesktop.dbus.exceptions.DBusExecutionException; public class UnknownMethod extends DBusExecutionException { public UnknownMethod(String message) { super(message); } } \end{verbatim} \end{center} \caption{An Exception} \label{fig:exceptiondef} \end{figure} \begin{figure}[htb] \begin{center} \begin{verbatim} ... public void throwme() throws org.freedesktop.DBus.Error.UnknownMethod { throw new org.freedesktop.DBus.Error.UnknownMethod("hi"); } ... \end{verbatim} \end{center} \caption{Throwing An Exception} \label{fig:exceptioncall} \end{figure} If you are calling a remote method and you want to handle such an error you can simply catch the exception as in figure \ref{fig:exceptioncatch}. \begin{figure}[htb] \begin{center} \begin{verbatim} ... try { remote.throwme(); } catch (org.freedesktop.DBus.Error.UnknownMethod UM) { ... } ... \end{verbatim} \end{center} \caption{Catching An Exception} \label{fig:exceptioncatch} \end{figure} \section{DBusSigHandler} To handle incoming signals from other programs on the Bus you must register a signal handler. This must implement {\tt DBusSigHandler\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusSigHandler.html}}} and provide an implementation for the handle method. An example Signal Handler is in figure~\ref{fig:handler}. Signal handlers should be parameterised with the signal they are handling. If you want a signal handler to handle multiple signals you can leave out the parameterisation and use {\tt instanceof} to check the type of signal you are handling. Signal handlers will be run in their own thread. \begin{figure}[htb] \begin{center} \begin{verbatim} import org.freedesktop.dbus.DBusSignal; import org.freedesktop.dbus.DBusSigHandler; public class Handler extends DBusSigHandler { public void handle(DBus.NameAcquired sig) { ... } } \end{verbatim} \end{center} \caption{A Signal Handler} \label{fig:handler} \end{figure} \section{D-Bus Types} D-Bus supports a number of types in its messages, some which Java supports natively, and some which it doesn't. This library provides a way of modelling the extra D-Bus Types in Java. The full list of types and what D-Bus type they map to is in table \ref{table:types}. \subsection{Basic Types} All of Java's basic types are supported as parameters and return types to methods, and as parameters to signals. These can be used in either their primitive or wrapper types. \subsubsection{Unsigned Types} D-Bus, like C and similar languages, has a notion of unsigned numeric types. The library supplies {\tt UInt16\footnote{\url{\javadocroot/org/freedesktop/dbus/UInt16.html}}}, {\tt UInt32} and {\tt UInt64} classes to represent these new basic types. \subsection{Strings} D-Bus also supports sending Strings. When mentioned below, Strings count as a basic type. \subsubsection{String Comparisons} There may be some problems with comparing strings received over D-Bus with strings generated locally when using the String.equals method. This is due to how the Strings are generated from a UTF8 encoding. The recommended way to compare strings which have been sent over D-Bus is with the {\tt java.text.Collator} class. Figure \ref{fig:collator} demonstrates its use. \begin{figure}[htb] \begin{center} \begin{verbatim} String rname = remote.getName(); Collator col = Collator.getInstance(); col.setDecomposition(Collator.FULL_DECOMPOSITION); col.setStrength(Collator.PRIMARY); if (0 != col.compare("Name", rname)) fail("getName return value incorrect"); \end{verbatim} \end{center} \caption{Comparing strings with {\tt java.text.Collator}.} \label{fig:collator} \end{figure} \subsection{Arrays} You can send arrays of any valid D-Bus Type over D-Bus. These can either be declared in Java as arrays (e.g. \verb&Integer[]& or \verb&int[]&) or as Lists (e.g. \verb&List&). All lists {\bf must} be parameterised with their type in the source (reflection on this is used by the library to determine their type). Also note that arrays cannot be used as part of more complex type, only Lists (for example \verb&List>&). \subsection{Maps} D-Bus supports a dictionary type analogous to the Java Map type. This has the additional restriction that only basic types can be used as the key (including String). Any valid D-Bus type can be the value. As with lists, maps must be fully parameterised. (e.g. \verb&Map&). \subsection{Variants} D-Bus has support for a Variant type. This is similar to declaring that a method takes a parameter of type {\tt Object}, in that a Variant may contain any other type. Variants can either be declared using the {\tt Variant\footnote{\url{\javadocroot/org/freedesktop/dbus/Variant.html}}} class, or as a Type Variable. In the latter case the value is automatically unwrapped and passed to the function. Variants in compound types (Arrays, Maps, etc) must be declared using the Variant class with the full type passed to the Variant constructor and manually unwrapped. Both these methods use variants: \begin{verbatim} public void display(Variant v); public int hash(T v); \end{verbatim} \subsection{Structs} D-Bus has a struct type, which is a collection of other types. Java does not have an analogue of this other than fields in classes, and due to the limitation of Java reflection this is not sufficient. The library declares a {\tt Struct\footnote{\url{\javadocroot/org/freedesktop/dbus/Struct.html}}} class which can be used to create structs. To define a struct you extend the {\tt Struct} class and define fields for each member of the struct. These fields then need to be annotated in the order which they appear in the struct (class fields do not have a defined order). You must also define a single constructor which takes the contents of he struct in order. This is best demonstrated by an example. Figure~\ref{fig:struct} shows a Struct definition, and figure~\ref{fig:structmethod} shows this being used as a parameter to a method. \begin{figure}[htb] \begin{center} \begin{verbatim} package org.freedesktop.dbus.test; import org.freedesktop.dbus.DBusException; import org.freedesktop.dbus.Position; import org.freedesktop.dbus.Struct; public final class TestStruct extends Struct { @Position(0) public final String a; @Position(1) public final int b; @Position(2) public final String c; public Struct3(String a, int b, String c) { this.a = a; this.b = b; this.c = c; } } \end{verbatim} \end{center} \caption{A Struct with three elements} \label{fig:struct} \end{figure} \begin{figure}[htb] \begin{center} \begin{verbatim} public void do(TestStruct data); \end{verbatim} \end{center} \caption{A struct as a parameter to a method} \label{fig:structmethod} \end{figure} Section~\ref{sec:create} describes how these can be automatically generated from D-Bus introspection data. \subsection{Objects} You can pass references to exportable objects round using their object paths. To do this in Java you declare a type of {\tt DBusInterface}. When the library receive- an object path it will automatically convert it into the object you are exporting with that object path. You can pass remote objects back to their processes in a similar fashion. Using a parameter of type {\tt DBusInterface} can cause the automatic creation of a proxy object using introspection. If the remote app does not support introspection, or the object does not exist at the time you receive the message then this will fail. In that case the parameter can be declared to be of type {\tt Path}. In this case no automatic creation will be performed and you can get the path as a string with either the {\tt getPath} or {\tt toString} methods on the {\tt Path} object. \subsection{Multiple Return Values} D-Bus also allows functions to return multiple values, a concept not supported by Java. This has been solved in a fashion similar to the struct, using a {\tt Tuple\footnote{\url{\javadocroot/org/freedesktop/dbus/Tuple.html}}} class. Tuples are defined as generic tuples which can be parameterised for different types and just need to be defined of the appropriate length. This can be seen in figure~\ref{fig:tuple} and a call in figure~\ref{fig:tuplemethod}. Again, these can be automatically generated from introspection data. \begin{figure}[htb] \begin{center} \begin{verbatim} import org.freedesktop.dbus.Tuple; public final class TestTuple extends Tuple { public final A a; public final B b; public final C c; public TestTuple(A a, B b, C c) { this.a = a; this.b = b; this.c = c; } } \end{verbatim} \end{center} \caption{A 3-tuple} \label{fig:tuple} \end{figure} \begin{figure}[htb] \begin{center} \begin{verbatim} public ThreeTuple status(int item); \end{verbatim} \end{center} \caption{A Tuple being returned from a method} \label{fig:tuplemethod} \end{figure} \subsection{Full list of types} Table \ref{table:types} contains a full list of all the Java types and their corresponding D-Bus types. \begin{table} \begin{center} \begin{tabular}{l|l} \bf Java Type & \bf D-Bus Type \\ \hline Byte & DBUS\_TYPE\_BYTE \\ byte & DBUS\_TYPE\_BYTE \\ Boolean & DBUS\_TYPE\_BOOLEAN \\ boolean & DBUS\_TYPE\_BOOLEAN \\ Short & DBUS\_TYPE\_INT16 \\ short & DBUS\_TYPE\_INT16 \\ UInt16 & DBUS\_TYPE\_UINT16 \\ int & DBUS\_TYPE\_INT32 \\ Integer & DBUS\_TYPE\_INT32 \\ UInt32 & DBUS\_TYPE\_UINT32 \\ long & DBUS\_TYPE\_INT64 \\ Long & DBUS\_TYPE\_INT64 \\ UInt64 & DBUS\_TYPE\_UINT64 \\ double & DBUS\_TYPE\_DOUBLE \\ Double & DBUS\_TYPE\_DOUBLE \\ String & DBUS\_TYPE\_STRING \\ Path & DBUS\_TYPE\_OBJECT\_PATH \\ $<$T$>$ & DBUS\_TYPE\_VARIANT \\ Variant & DBUS\_TYPE\_VARIANT \\ ? extends Struct & DBUS\_TYPE\_STRUCT \\ ?$[$~$]$ & DBUS\_TYPE\_ARRAY \\ ? extends List & DBUS\_TYPE\_ARRAY \\ ? extends Map & DBUS\_TYPE\_DICT \\ ? extends DBusInterface & DBUS\_TYPE\_OBJECT\_PATH \\ Type$[$~$]$ & DBUS\_TYPE\_SIGNATURE \\ \end{tabular} \end{center} \caption{Mapping between Java types and D-Bus types} \label{table:types} \end{table} \subsubsection{float} Currently the D-Bus reference implementation does not support a native single-precision floating point type. Along with the C\# implementation of the protocol, the Java implementation supports this extension to the protocol. By default, however, the library operates in compatibility mode and converts all floats to the double type. To disable compatibility mode export the environment variable {\tt DBUS\_JAVA\_FLOATS=true}. \section{Annotations} You can annotate your D-Bus methods as in figure \ref{fig:annotation} to provide hints to other users of your API. Common annotations are listed in table \ref{tab:annotations}. \begin{figure}[htb] \begin{center} \begin{verbatim} package org.freedesktop; import org.freedesktop.dbus.UInt32; import org.freedesktop.dbus.DBusInterface; @org.freedesktop.DBus.Description("Some Methods"); public interface DBus extends DBusInterface { @org.freedesktop.DBus.Description("Check if the name has an owner") public boolean NameHasOwner(String name); @org.freedesktop.DBus.Description("Request a name") @org.freedesktop.DBus.Deprecated() public UInt32 RequestName(String name, UInt32 flags); } \end{verbatim} \end{center} \caption{An annotated method} \label{fig:annotation} \end{figure} \begin{table}[htb] \begin{tabular}{l|l} {\bf Name} & {\bf Meaning} \\ \hline org.freedesktop.DBus.Description & Provide a short 1-line description \\ & of the method or interface. \\ org.freedesktop.DBus.Deprecated & This method or interface is Deprecated. \\ org.freedesktop.DBus.Method.NoReply & This method may be called and returned \\ & without waiting for a reply. \\ org.freedesktop.DBus.Method.Error & This method may throw the listed Exception\\ & in addition to the standard ones. \\ \end{tabular} \caption{Common Annotations} \label{tab:annotations} \end{table} \section{DBusSerializable} Some people may want to be able to pass their own objects over D-Bus. Obviously only raw D-Bus types can be sent on the message bus itself, but the Java implementation allows the creation of serializable objects which can be passed to D-Bus functions and will be converted to/from D-Bus types by the library. To create such a class you must implement the {\tt DBusSerializable\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusSerializable.html}}} class and provide two methods and a zero-argument constructor. The first method has the signature {\tt public Object serialize() throws DBusException} and the second must be called {\tt deserialize}, return {\tt null} and take as it's arguments exactly all the dbus types that are being serialized to {\em in order} and {\em with parameterization}. The serialize method should return the class properties you wish to serialize, correctly formatted for the wire ({\tt DBusConnection.convertParameters()} can help with this), in order in an Object array. An example of a serializable class can be seen in figure~\ref{fig:serializable}. \begin{figure}[htb] \begin{center} \begin{verbatim} import java.lang.reflect.Type; import java.util.List; import java.util.Vector; import org.freedesktop.dbus.DBusConnection; import org.freedesktop.dbus.DBusSerializable; public class TestSerializable implements DBusSerializable { private int a; private String b; private Vector c; public TestSerializable(int a, String b, Vector c) { this.a = a; this.b = b.toString(); this.c = c; } public TestSerializable() {} public void deserialize(int a, String b, List c) { this.a = a; this.b = b; this.c = new Vector(c); } public Object[] serialize() { return new Object[] { a, b, c }; } public int getInt() { return a; } public String getString() { return b; } public Vector getVector() { return c; } public String toString() { return "TestSerializable{"+a+","+b+","+c+"}"; } } \end{verbatim} \end{center} \caption{A serializable class} \label{fig:serializable} \end{figure} \section{CreateInterface} \label{sec:create} D-Bus provides a method to get introspection data on a remote object, which describes the interfaces, methods and signals it provides. This introspection data is in XML format\footnote{http://dbus.freedesktop.org/doc/dbus-specification.html\#introspection-format}. The library automatically provides XML introspection data on all objects which are exported by it. Introspection data can be used to create Java interface definitions automatically. The {\tt CreateInterface\footnote{\url{\javadocroot/org/freedesktop/dbus/CreateInterface.html}}} class will automatically create Java source files from an XML file containing the introspection data, or by querying the remote object over D-Bus. CreateInterface can be called from Java code, or can be run as a stand alone program. The syntax for the CreateInterface program is \begin{verbatim} CreateInterface [--system] [--session] [--create-files]