Java Reference
In-Depth Information
logger.log(level, msg);
logged = true;
}
}
if (!logged) {
System.out.println("[" + level + "] " + msg);
}
}
}
You can no longer make your ServiceTracker member variable final , because you
don't know when it will be created. To make your proxy logger thread safe and avoid
creating more than one ServiceTracker instance, you need to synchronize your entry
methods B . Because the logging package can appear at any time during execution,
you try to create the ServiceTracker instance each time you log a message C until
successful. As before, if all else fails, you log to standard output. The manifest meta-
data is pretty much the same as before, except you use DynamicImport-Package to
import the logging package:
Bundle-ManifestVersion: 2
Bundle-SymbolicName: example.logger
Bundle-Activator: example.logger.Activator
Import-Package: org.osgi.framework, org.osgi.util.tracker
DynamicImport-Package: org.osgi.service.log
These two examples illustrate the differences between these two mechanisms. As you
can see, if you plan to take advantage of the full, dynamic nature of dynamically
imported packages, there's added complexity with respect to threading and concur-
rency. There's also potential overhead associated with dynamic imports, not only
because of the synchronization, but also because it can be costly for the framework to
try to find a matching package at execution time. For logging, which happens fre-
quently, this cost can be great.
Optional imports are optional
We should point out that you can use dynamic imports in a fashion similar to optional
imports. In this sense, the use of the optional import package mechanism is itself
optional. For example, you can modify the metadata of the optional logger example
to be a dynamic import instead, but keep the code exactly the same. If you did this,
the two solutions would behave equivalently.
If this is the case, then why choose one over the other? There's no real reason or
recommendation for doing so. These two concepts overlap for historical reasons.
Dynamic imports have existed since the R2 release of the OSGi specification,
whereas optional imports have only existed since the R4 release. Even though op-
tional imports overlapped dynamic imports, they were added for consistency with
bundle dependencies, which were also added in R4 and can also be declared
as optional.
 
Search WWH ::




Custom Search