forked from hypfvieh/dbus-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.html
More file actions
394 lines (332 loc) · 16.1 KB
/
Copy pathproperties.html
File metadata and controls
394 lines (332 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from src/site/markdown/properties.md at 2025-12-23
| Rendered using Apache Maven Fluido Skin 2.1.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<title>Properties – dbus-java</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-2.1.0.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-2.1.0.min.js"></script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2025-12-21</li>
<li id="projectVersion" class="pull-right">Version: 5.2.1-SNAPSHOT</li>
<li class="pull-right"><span class="divider">|</span>
<a href="https://search.maven.org/search?q=g:com.github.hypfvieh%20dbus-java">Maven Central</a></li>
<li class="pull-right"><span class="divider">|</span>
<a href="https://www.github.com/hypfvieh/dbus-java/wiki">GitHub Wiki</a></li>
<li class="pull-right"><a href="https://www.github.com/hypfvieh/dbus-java">GitHub Repository</a></li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="index.html">Home</a></li>
<li><a href="quick-start.html">Quickstart</a></li>
<li><a href="code-generation.html">Code Generation</a></li>
<li><a href="exporting-objects.html">Exporting Objects</a></li>
<li><a href="remote-objects.html">Calling Remote Objects</a></li>
<li class="active"><a>Properties</a></li>
<li><a href="variant-handling.html">Variant</a></li>
<li><a href="using-signals.html">Signals</a></li>
<li><a href="howto.html">Howto...</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
<li class="nav-header">Modules</li>
<li><a href="dbus-java-core/index.html">dbus-java-core</a></li>
<li><a href="dbus-java-osgi/index.html">dbus-java-osgi</a></li>
<li><a href="dbus-java-utils/index.html">dbus-java-utils</a></li>
<li><a href="dbus-java-transport-jnr-unixsocket/index.html">dbus-java-transport-jnr-unixsocket</a></li>
<li><a href="dbus-java-transport-junixsocket/index.html">dbus-java-transport-junixsocket</a></li>
<li><a href="dbus-java-transport-native-unixsocket/index.html">dbus-java-transport-native-unixsocket</a></li>
<li><a href="dbus-java-transport-tcp/index.html">dbus-java-transport-tcp</a></li>
<li><a href="dbus-java-bom/index.html">dbus-java-bom</a></li>
<li><a href="dbus-java-tests/index.html">dbus-java-tests</a></li>
<li><a href="dbus-java-examples/index.html">dbus-java-examples</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Properties"></a>
<h1>Properties</h1>
<p><a href="https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties" class="externalLink">DBus Properties</a> can be accessed
and exported using one of two methods.</p>
<ul>
<li><a href="#using-the-exported-object">Directly Bound To Setters And Getters</a>.</li>
<li><a href="#implementing-the-properties-interface">Implementing the Properties Interface</a>.</li>
</ul><section><a id="Directly_Bound_To_Setters_And_Getters"></a>
<h2>Directly Bound To Setters And Getters</h2>
<p>Since version 5 of <code>dbus-java</code>, it has been possible to directly map DBus properties to Java
methods. By convention this is done using a JavaBeans like pattern.</p>
<p>This method is likely preferable in most cases, as it has a number of advantages over the
legacy method described below.</p>
<ul>
<li>Simpler code when exporting services, no need to implement <code>Get()</code>, <code>Set()</code> and <code>GetAll()</code> from <code>org.freedesktop.DBus.Properties</code>.
In fact, you do not need to implement this interface at all.</li>
<li>Simpler code when accessing properties on services, no casting or accessing properties by string names.</li>
<li>More natural. Java programmers are familiar with this pattern.</li>
<li>Makes it much easier to share interfaces between client and server code.</li>
</ul>
<p>This feature is activated by the <code>@DBusBoundProperty</code> annotation.</p><section><a id="The_Interface"></a>
<h3>The Interface</h3>
<p>Consider the following interface.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.annotations.DBusProperty.Access;
import org.freedesktop.dbus.annotations.DBusBoundProperty;
public interface MyInterface extends DBusInterface {
String sayHello();
@DBusBoundProperty(access = Access.READ, name = "MyProperty")
String getMyProperty();
@DBusBoundProperty(access = Access.WRITE, name = "MyProperty")
void setMyProperty(String _property);
@DBusBoundProperty(access = Access.READ, name = "ZZZZZZZ")
long getMyAltProperty();
@DBusBoundProperty(access = Access.WRITE, name = "ZZZZZZZ")
void setMyAltProperty(long _property);
@DBusBoundProperty
boolean isMyOtherProperty();
@DBusBoundProperty
void setMyOtherProperty(boolean _property);
}
</code></pre>
<p>When instances of the interface are exported, just a single method call will be visible.
All other attributes of the interface will be exported as properties instead.</p>
<p>Every DBus property has 3 optional attributes. It's <code>name</code>, it's <code>type</code> and it's <code>access</code> mode.
All 3 of these attributes may optionally be supplied to the annotation, but for most cases it
is sufficient to omit all attributes and for dbus-java to infer these from the method
signature.</p>
<ul>
<li>The name of the property will be derived from the method name, with the leading <code>get</code>,
<code>set</code> or <code>is</code> removed (case insensitively).</li>
<li>The access mode of the property will be determined by whether the method is a <code>set</code> or
and <code>get</code> or <code>is</code>.</li>
<li>The type of the property will be derived from either the return value or the first (and only) parameter.</li>
</ul>
<p><em>All getters should have zero arguments and a return value, and all setters should have
a single argument, and no return value</em></p>
<p>If you are just interested in using a 3rd party service that exports your target object, you then
can just skip the next section and go straight to <a href="#using-the-exported-object">Using The Exported Object</a> below.</p>
<p>However if you wish to export your own service that uses properties, then read on.</p></section><section><a id="The_Implementation"></a>
<h3>The Implementation</h3>
<p>Expanding on the above example, we will now provide the implementation of this interface
and export it for clients to use.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
public class MyObject implements MyInterface {
private String myProperty = "Initial value";
private boolean myOtherProperty;
private long myAltProperty = 123;
@Override
public String getObjectPath() {
return "/com/acme/MyObject";
}
@Override
public String sayHello() {
return "Hello!";
}
@Override
public long getMyAltProperty() {
return myAltProperty;
}
@Override
public void setMyAltProperty(long _myAltProperty) {
this.myAltProperty = _myAltProperty;
}
@Override
public String getMyProperty() {
return myProperty;
}
@Override
public void setMyProperty(String _property) {
myProperty = _property;
}
@Override
public boolean isMyOtherProperty() {
return myOtherProperty;
}
@Override
public void setMyOtherProperty(boolean _property) {
myOtherProperty = _property;
}
@Override
public boolean isRemote() {
/* Whenever you are implementing an object, always return false */
return false;
}
}
</code></pre></section><section><a id="Exporting_The_Object"></a>
<h3>Exporting The Object</h3>
<p>You would export the object the same as normal.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
public class ExportMyObject {
ExportMyObject() throws DBusException {
DBusConnection conn = DBusConnectionBuilder.forSessionBus().build();
conn.requestBusName( "com.acme" );
MyObject obj = new MyObject();
conn.exportObject( obj.getObjectPath(), obj );
}
public static void main(String[] args) throws DBusException {
new ExportMyObject();
}
}
</code></pre></section><section><a id="Using_The_Exported_Object"></a>
<h3>Using The Exported Object</h3>
<p>And finally making use of the exported interface in client code.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
public class ImportMyObject {
ImportMyObject() throws DBusException {
DBusConnection conn = DBusConnectionBuilder.forSessionBus().build();
MyInterface obj = conn.getRemoteObject("com.acme", "/com/acme/MyObject", MyInterface.class);
System.out.println("My property: " + obj.getMyProperty());
System.out.println("My Alt property: " + obj.getMyAltProperty());
System.out.println("My Other property: " + obj.isMyOtherProperty());
}
public static void main(String[] args) throws DBusException {
new ImportMyObject();
}
}
</code></pre></section></section><section><a id="Implementing_the_Properties_Interface"></a>
<h2>Implementing the Properties Interface</h2>
<p>As an alternative to the above, you can use DBus's <code>org.freedesktop.dbus.Properties</code> interface.</p>
<p>If you are exporting your own service, this means that you <code>extends Properties</code> in your interface,
and provide the required implementations of the <code>Get()</code>, <code>GetAll()</code> and <code>Set()</code> methods in your
concrete class.</p>
<p>If you are using an exported interface in client code, then you access your remote object as normal,
and invoke the <code>Get()</code>, <code>GetAll()</code> or <code>Set()</code> methods on that object.</p>
<p>The above example, re-written to use <code>@DBusProperty</code>, would look like this.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.annotations.DBusProperty;
import org.freedesktop.dbus.annotations.DBusProperty.Access;
import org.freedesktop.dbus.interfaces.Properties;
@DBusProperty(access = Access.READ_WRITE, name = "MyProperty", type = String.class)
@DBusProperty(access = Access.READ_WRITE, name = "ZZZZZZZ", type = String.class)
@DBusProperty(access = Access.READ_WRITE, name = "MyOtherProperty", type = Boolean.class)
public interface MyInterface extends DBusInterface, Properties {
String sayHello();
}
</code></pre>
<p>While this interface is smaller than the above, there is a bit more to do in the implementation,
or when you need to access these properties.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
public class MyObject implements MyInterface {
private String myProperty = "Initial value";
private boolean myOtherProperty;
private long myAltProperty = 123;
@Override
public String getObjectPath() {
return "/com/acme/MyObject";
}
@Override
public String sayHello() {
return "Hello!";
}
@SuppressWarnings("unchecked")
@Override
public <A> A Get(String _interfaceName, String _propertyName) {
if ("MyProperty".equals(_propertyName)) {
return (A) new Variant<>(myProperty);
} else if ("ZZZZZZZ".equals(_propertyName)) {
return (A) new Variant<>(myAltProperty);
} else if ("MyOtherProperty".equals(_propertyName)) {
return (A) new Variant<>(myOtherProperty);
} else {
throw new IllegalArgumentException();
}
}
@SuppressWarnings("unchecked")
@Override
public <A> void Set(String _interfaceName, String _propertyName, A _value) {
if ("MyProperty".equals(_propertyName)) {
myProperty = ((Variant<String>) _value).getValue();
} else if ("ZZZZZZZ".equals(_propertyName)) {
myAltProperty = ((Variant<Long>) _value).getValue();
} else if ("MyOtherProperty".equals(_propertyName)) {
myOtherProperty = ((Variant<Boolean>) _value).getValue();
} else {
throw new IllegalArgumentException();
}
}
@Override
public Map<String, Variant<?>> GetAll(String _interfaceName) {
Map<String, Variant<?>> all = new HashMap<>();
all.put("MyProperty", new Variant<>(myProperty));
all.put("ZZZZZZZ", new Variant<>(myAltProperty));
all.put("MyOtherProperty", new Variant<>(myOtherProperty));
return all;
}
@Override
public boolean isRemote() {
/* Whenever you are implementing an object, always return false */
return false;
}
}
</code></pre>
<p>And finally making use of these properties in client code.</p>
<pre class="prettyprint"><code class="language-java">package com.acme;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.types.Variant;
public class ImportMyObject {
ImportMyObject() throws DBusException {
DBusConnection conn = DBusConnectionBuilder.forSessionBus().build();
MyInterface obj = conn.getRemoteObject("com.acme", "/com/acme/MyObject", MyInterface.class);
System.out.println("My property: "
+ (String) obj.Get("com.acme.MyInterface", "MyProperty"));
System.out.println("My Alt property: "
+ (Long) obj.Get("com.acme.MyInterface", "ZZZZZZZ"));
System.out.println("My Other property: "
+ (Boolean) obj.Get("com.acme.MyInterface", "MyOtherProperty"));
}
public static void main(String[] args) throws DBusException {
new ImportMyObject();
}
}
</code></pre></section></section> </main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2025
</p>
</div>
</div>
</footer>
</body>
</html>