Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » name of the active bundle(How can you get the name of the active bundle/plugin in an OSGi context?)
name of the active bundle [message #1733633] Mon, 30 May 2016 14:28 Go to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
In our, purely OSGi dynamic services based, services layer we are trying to access the name of the current bundle.
From the presentation layer we used to do bundleContext.getBundle().getSymbolicName() before, with bundleContext injected from the eclipse context:
@Inject
@Optional
private BundleContext bundleContext;
BundleContext lives in the org.osgi.framework package of org.eclipse.osgi, but sadly (for us) isn't a declarative service.
Can we access BundleContext from DS, or is there another way to find the name of the active bundle?
Re: name of the active bundle [message #1733639 is a reply to message #1733633] Mon, 30 May 2016 15:12 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
What do you mean with "active bundle"? Typically all bundles are active.

For sure the BundleContext is not a declarative service. It is the context of the bundle. Not sure what your use case is, but technically you can get it as a parameter in a lifecycle method (@Activate, @Modified, @Deactivate). Similar to the usage in an Activator.

Re: name of the active bundle [message #1733646 is a reply to message #1733633] Mon, 30 May 2016 16:27 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
I'm a bit lost because you use @Inject in your OSGi-Service? If not
mistaken you want to know in your OSGi-Layer who request your
OSGi-Service when some writes:

class MyUIPart {
@Inject
MyService myService;
}

is that assumption correct?

The the answer is you can't get that information by using default
eclipse-DI because the guy requesting the OSGi-Service is never the real
receiver but org.eclipse.core.di.context.

e(fx)clipse has extra code to make the real receiver showing up in OSGi
but before going into that you need to clarify what you really want.

Tom

On 30.05.16 16:28, Erik Vande Velde wrote:
> In our, purely OSGi dynamic services based, services layer we are trying
> to access the name of the current bundle.
> From the presentation layer we used to do
> bundleContext.getBundle().getSymbolicName() before, with bundleContext
> injected from the eclipse context:
> @Inject
> @Optional
> private BundleContext bundleContext;
> BundleContext lives in the org.osgi.framework package of
> org.eclipse.osgi, but sadly (for us) isn't a declarative service.
> Can we access BundleContext from DS, or is there another way to find the
> name of the active bundle?
Re: name of the active bundle [message #1733702 is a reply to message #1733646] Tue, 31 May 2016 07:24 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Maybe this will help:
FrameworkUtil.getBundle(getClass()).getBundleContext();
Re: name of the active bundle [message #1733731 is a reply to message #1733702] Tue, 31 May 2016 09:38 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
1) Let's start with the use case:
- Our e4 client has a JMS connector to a server application
- sometimes the server goes down, and then we land up in the 'onException' method of our connector
- there we want to show the user, in a message box, that the connection is lost

2) Technical challenges
- It seems logical that the jms connector is a service, and my present task is to convert it from a class in the eclipse context to a dynamic service
- The connector uses a 'GeneralErrorHandler' class (which was annotated with Creatable), invoking its handleException method to show the message to the user
- Before my task we used the eclipse context to inject the handler in the connector
- Because I'm using DS I'm also trying to convert the GeneralErrorHandler into a dynamic service
- Stumbling block: GeneralErrorHandler has an injected field of type BundleContext (NOT a dynamic service), with as only purpose to find a plug-in identifier
- That identifier is needed because in the end we use ErrorDialog.openError(activeShell, "Error", message, status)
- status is of type IStatus, which has a required field 'plug-in identifier'

3) current status
- Thanks to Christoph's contribution I succeeded in making GeneralErrorHandler a DS: the bundle id is dynamically obtained when constructing the status
- But we still have a 'code smell' here: the dynamic service GeneralErrorHandler wants to do something in the presentation layer
- So my current question is: how would you propagate a jms connection error message from the OSGi services layer to the presentation layer ?
Re: name of the active bundle [message #1733743 is a reply to message #1733731] Tue, 31 May 2016 10:45 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
If that is the only case, you could also use the activate method in your DS component to get the BundleContext like with an Activator

BundleContext context = null;

@Activate
public void activate(BundleContext context) {
    this.context = context;
}


But with this you get only the BundleContext of the components bundle. If you want to get the BundleContext of another bundle to which you know a consisting class, the solution by Christoph is suitable.

For the question on how to propagate the message from the OSGi layer to the presentation layer, I would use the event bus. This decouples the layers completely. I wrote a blog post about that a while ago:
https://blog.codecentric.de/en/2015/04/osgi-declarative-services-and-the-event-bus-eclipse-rcp-cookbook/

[Updated on: Tue, 31 May 2016 10:46]

Report message to a moderator

Re: name of the active bundle [message #1733752 is a reply to message #1733743] Tue, 31 May 2016 12:14 Go to previous message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
Thanks Dirk! I'll certainly have a closer look at your blog post. We're already using the event bus somewhere in our code for similar stuff, so it's probably only a matter of making the overall error handling a bit more consistent (also handling the message translation in a uniform way) ...
Previous Topic:Splash Screen with Progress Bar and Messages
Next Topic:Does e4 RCP support adding features (plug-ins) by copying them into the plug-in directory?
Goto Forum:
  


Current Time: Sat Apr 20 00:23:54 GMT 2024

Powered by FUDForum. Page generated in 0.03191 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top