Java Reference
In-Depth Information
Using synchronization logic such as synchronized blocks , AtomicReference ,
or CopyOnWriteArraySet
Using timeouts if the service removal is only temporary (during a software
upgrade)
Declaring dependencies as mandatory such that a component is shut down if its
dependencies become unsatisfied during execution time
i POJO offers another option through the use of the @Requires annotation, which we'll
look at next.
FIELD INJECTION
i POJO defines the @Requires field-level annotation to associate a service dependency
with a component class-member field, rather than a pair of binding methods. As we
mentioned previously, i POJO performs byte-code instrumentation on components to
enable field-access interception. For the @Requires annotation, the i POJO framework
intercepts field access at execution time to provide components access to their
required services. At a very high level, this acts as if you've sprinkled your code with a
liberal number of AtomicRefererence s. This ensures that the component always sees
a consistent view of the services as they appear in the OSG i service registry at a given
moment, without all the tedious boilerplate synchronization code in the source files.
The @Requires annotation also works with collections or arrays to aggregate multi-
ple services from the OSG i service registry. In addition, it can create default objects or
null objects if an optional service isn't available, which greatly simplifies your source
code because you don't need to perform null checks throughout.
Let's look at how you can use these features in the paint program. The Window-
Listener component has an optional dependency on the OSG i Log Service. In
Declarative Services and in Blueprint, you use an AtomicReference to ensure that you
have a consistent view of the service in your component. In i POJO , you declare the log
service dependency on a field, like so:
@Requires(optional=true)
private LogService m_log;
To a c c e s s t h e l o g s e r v i c e , y o u u s e t h e f i e l d l i k e t h i s :
@Override
public void windowClosed(WindowEvent evt) {
try {
m_log.log(LogService.LOG_INFO, "Window closed");
m_context.getBundle(0).stop();
} catch (BundleException e) {
} catch (IllegalStateException e) {
}
}
In Declarative Services and Blueprint, you have to use the AtomicReference to hold
the log service and then check for null before using it. In i POJO , you can use the log
service, because optional dependencies automatically receive a null object if no real
Search WWH ::




Custom Search