Java Reference
In-Depth Information
cardinality="0..1"
bind="bindLog"
unbind="unbindLog"/>
</scr:component>
The
WindowListener
component has a static, singular, and mandatory dependency on
a
Window
service
B
. You specify a target
LDAP
filter to locate the specific
Window
service
of interest
C
; recall that the filter references the property you associated with the
PaintFrame
component's
Window
service in its component description in listing 11.3.
Additionally, the
WindowListener
component has a dynamic, singular, and optional
dependency on a log service.
Target reference properties
You saw earlier that component properties can be used to define the service proper-
ties associated with a component's provided service. Component properties can also
be used to configure service-dependency target filters at execution time. To do this,
the property name must be equal to the name associated with the service reference
appended with
.target
. In this case, you could override the window target using a
property of this form:
<property name="window.target" value="(name=other)" />
This binds the window listener to windows attributed with the
name=other
identifier.
Doing this directly in the static component description is of relatively low value. But
if you remember the discussion on component properties, these values can also be
set at execution time via the Configuration Admin Service or using component facto-
ries, which opens up a set of interesting use cases.
Filtering services based on attributes is relatively easy and, at first glance, dealing with
optional services appears equally easy. But there are some subtle mechanics of which
you need to be aware when using the dynamic dependency policy. Here are the rele-
vant lines from the
WindowListener
component.
Listing 11.5
WindowListener
with optional
LogService
dependency
public class WindowListener extends WindowAdapter {
...
private AtomicReference<LogService> logRef =
new AtomicReference<LogService>();
protected void bindLog(LogService log) {
logRef.compareAndSet(null, log);
}
protected void unbindLog(LogService log) {
logRef.compareAndSet(log, null);
}
...
private void log(int level, String msg) {
LogService log = logRef.get();