开发者

How can I find out which bundle or plugin invoked (shared) code?

I want to create something like 'BundleLocal' variables, just like ThreadLocal variables, but instead of looking them up by Thread.currentThread() I need to look them up by bundle context.

The point is that I have a framework plugin 'F', and other plugins 'A', 'B', etc. using that framework. The framework contains a singleton which I want to refactor such that I have a singleton instance per bundle. MySingleton.getInstance() would then create/return a dedicated instance for each plugin. The problem is that within MySingleton.getInstance() I'd need to find out which of the plugins ('A', 'B', ..) called that method.

I could do it with a hack, obtaining the call stack with

StackTraceElement[] ste=new Throwable().getStackTra开发者_运维技巧ce();

and then querying the bundle context by class for each element:

org.osgi.framework.FrameworkUtil.getBundle(Class c).getBundleContext()

but sincerely hope that there exists a better way to obtain the bundle context or bundle id from within framework 'F'.

Any advice on how to do this more elegantly?


I would recommend to inject the information from where the call comes instead of trying to determine where it comes from. Something like this:

MyMultiton.getInstance(bundleContext)


Because of the modular nature of OSGi (and its isolated class loaders) singletons don't work (as you found out).

If you're refactoring, one option would be to simply add indirection. In ActivatorA:

public static Singleton getSingleton() {
    Framework.getSingleton(BUNDLE_ID);
}

Then plugin A uses ActivatorA.getSingleton() and plugin B uses ActivatorB.getSingleton().

The other potential way is to provide a ComponentFactory and use Declarative Service to inject the appropriate Component (OSGi service) into plugin A and plugin B (everybody gets their own instance of the Component). But this is a more dynamic, more decoupled implementation of my first example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜