Making static class members unique per bundle [message #51967] |
Mon, 31 October 2005 10:56  |
Eclipse User |
|
|
|
I am using several components which utilize Apache's Jakarta Commons
Logging API to log messages. I would like to log messages on a per-bundle
basis. This is easy in OSGi land, using a ServiceFactory. However, a
standard use of the JCL API is:
protected final Log logger = LogFactory.getLog(getClass());
The problem here is that LogFactory.getLog(), a static method, has no way
of knowing which bundle is requesting a Log object. One way around this
(I believe) is to have each bundle include its own copy of the LogFactory
class instead of sharing the class from one library. This however can
become a maintenance nightmare, and negates one of OSGi's strong points.
It would be nice if a bundle could have its own unique "instance" of the
static LogFactory class. Therefore, any class requesting the static
LogFactory.getLog() method would get the instance associated with the
ClassLoader's corresponding bundle.
Is this at all possible (possibly with a few modifications/extensions to
the Framework)?
My current solution to the issue is to have Bundles register their
ClassLoader with a modified LogFactory class upon bundle activation.
LogFactory.getLog() then analyzes the call stack (provided by
SecurityManager.getClassContext()) to see if one of the Class objects in
the call stack belongs to a registered ClassLoader. This, of course, is
not the optimal solution to the problem, but it works for now.
Thanks,
Jeremy Volkman
|
|
|
|
|
Re: Making static class members unique per bundle [message #52180 is a reply to message #52021] |
Wed, 02 November 2005 01:48  |
Eclipse User |
|
|
|
Thanks for the tip! I've changed my implementation to use
PackageAdmin.getBundle(Class clazz). I still walk the call stack to find
calling classes, but I no longer have to register bundles at activation.
The reason I still walk the call stack is because I have a
logging-priority per bundle. For example, if a critical service bundle
executes a method in a library bundle, and that method logs a message, I'd
like that message to be logged under the critical service.
|
|
|
Powered by
FUDForum. Page generated in 0.04050 seconds