Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Best practices question : Howto obtain services & howto start them
Best practices question : Howto obtain services & howto start them [message #54497] Mon, 12 December 2005 14:04 Go to next message
Erik Vanherck is currently offline Erik VanherckFriend
Messages: 23
Registered: July 2009
Junior Member
Hi All,

In OSGi, obtaining services needs to be done using the BundleContext. The
only way to obtain a BundleContext is using a BundleActivator. Suppose I
have created a simple LogService and registered it with the framework, but
I have lots of 'library plugins' that only contain code and at some point
want to log something, how do they obtain the service, our what is the
best practice in this case ?

I rather not have all of them implement Activators that do virtually the
same thing, certainly because the Eclipse-AutoStart is an extension and
probably doesn't work in the DefaultAdaptor (I'm using both OSGi Adaptors
depending on the environment I run in). Eclipse has a global Platform
class with static functions to obtain the Extension registry and
preferences for instance. Should I make my own global class and have that
provide the Logging capabilities to the library bundles or is there a
better way ?

Second part of the question is then, except using Eclipse-AutoStart (which
I probably can't use due to the DefaultAdaptor), or placing a bundle in
the start level, is there a proper way to start the services ? I'm
wondering how I get my logService started when I need it but not sooner,
as not to unneccessarly have the users machine loading bundles it may not
need at all.

Thanks in advance for any insights anyone has in this matter.

Erik Vanherck
Re: Best practices question : Howto obtain services & howto start them [message #58403 is a reply to message #54497] Thu, 26 January 2006 14:48 Go to previous message
Eclipse UserFriend
Originally posted by: jeff_nospam_mcaffer.ca.ibm.com

"Erik Vanherck" <Erik.Vanherck@inventivedesigners.com> wrote in message
news:e108918647569bb43514b968b15ff3b1$1@www.eclipse.org...
> Hi All,
>
> In OSGi, obtaining services needs to be done using the BundleContext. The
> only way to obtain a BundleContext is using a BundleActivator. Suppose I
> have created a simple LogService and registered it with the framework, but
> I have lots of 'library plugins' that only contain code and at some point
> want to log something, how do they obtain the service, our what is the
> best practice in this case ?

For the bundle context there has been some discussion about having an
accessor that would allow one to get the context from inside a bundle. See
https://bugs.eclipse.org/bugs/show_bug.cgi?id=108214

> I rather not have all of them implement Activators that do virtually the
> same thing, certainly because the Eclipse-AutoStart is an extension and
> probably doesn't work in the DefaultAdaptor (I'm using both OSGi Adaptors
> depending on the environment I run in). Eclipse has a global Platform
> class with static functions to obtain the Extension registry and
> preferences for instance. Should I make my own global class and have that
> provide the Logging capabilities to the library bundles or is there a
> better way ?

This is a challenge we face question as well. The Activator implementation
is a bit of a pain but the code is dead simple and you just copy it in to
every bundle in some internal package (one that is not exported). Then
everyone in the bundle just does Activator.getContext() or some such.

Further to this, coding OSGi service lookup etc is painful. As you have
seen we have largely gone the static helper method route. The Extension
registry does this and more historically, the Runtime Platform class is
almost entirely this approach. The downside here is that either you end up
with one big honkin' class (e.g., Platform) that references all manner of
things and so drags in the world, or you have lots of little static helpers
laying around and they end up being implementation detail and tie you to a
particular implementation.

Declarative services may offer you some relief here but even they do not fit
all the usecases.

> Second part of the question is then, except using Eclipse-AutoStart (which
> I probably can't use due to the DefaultAdaptor), or placing a bundle in
> the start level, is there a proper way to start the services ? I'm
> wondering how I get my logService started when I need it but not sooner,
> as not to unneccessarly have the users machine loading bundles it may not
> need at all.

Declarative services are on the track you are looking.

Jeff
Previous Topic:Oscar - Knopflerfish - Bundles
Next Topic:Is there a reference for all the console commands? <eom>
Goto Forum:
  


Current Time: Thu Apr 25 00:22:31 GMT 2024

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

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

Back to the top