Java Reference
In-Depth Information
m_logger.log(4, "Started");
}
public void stop(BundleContext context) {
m_logger.close();
}
}
When the bundle is started, you create an instance of your proxy logger that's used
throughout the bundle for logging. Although not shown here, the bundle passes a ref-
erence or somehow provides access to the logger instance to any internal code need-
ing a logger at execution time. When the bundle is stopped, you invoke close() on
the proxy logger, which stops its internal service tracker, if necessary. The manifest for
your logging bundle is
Bundle-ManifestVersion: 2
Bundle-SymbolicName: example.logger
Bundle-Activator: example.logger.Activator
Import-Package: org.osgi.framework, org.osgi.util.tracker,
org.osgi.service.log; resolution:=optional
How would this example change if you wanted to treat the logging package as a
dynamic import? The impact to the Logger class is as follows.
Listing 5.3 Simple proxy logger using dynamic import
public class Logger {
private final BundleContext m_context;
private ServiceTracker m_tracker;
public LoggerImpl(BundleContext context) {
m_context = context;
}
private ServiceTracker init(BundleContext context) {
ServiceTracker tracker = null;
try {
tracker = new ServiceTracker(
context, org.osgi.service.log.LogService.class.getName(), null);
tracker.open();
} catch (NoClassDefFoundError error) { }
return tracker;
}
public synchronized void close() {
if (m_tracker != null) {
m_tracker.close();
}
}
public synchronized void log(int level, String msg) {
boolean logged = false;
if (m_tracker == null) {
m_tracker = init(m_context);
}
if (m_tracker != null) {
LogService logger = (LogService) m_tracker.getService();
if (logger != null) {
B
Synchronizes
entry
methods
Tries to create
ServiceTracker
C
 
Search WWH ::




Custom Search