Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » obtaining a reference to EventAdmin too complex
obtaining a reference to EventAdmin too complex [message #513492] Wed, 10 February 2010 12:01 Go to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
Currently, to obtain the EventAdmin, you would have to do something similar to this if you would not cache the reference somewhere:

ServiceReference ref = null;
BundleContext ctx = null;
EventAdmin ea = null;
{
Bundle b = FrameworkUtil.getBundle( this.getClass() );
if ( b != null ) {
ctx = b.getBundleContext();
if ( ctx != null ) {
ref = ctx.getServiceReference( EventAdmin.class.getName() );
if ( ref != null ) {
ea = (EventAdmin) ref;
}
}
}
}

if ( ea != null ) {
Map<String, Object> props = new HashMap<String, Object>();
// add event props...
ea.sendEvent( new Event( "com.example.myEvents/doSomething", props ) );
ctx.ungetService( ref );
}

Are there any simpler ways to get the EventAdmin?
I mean just a one-liner like this:

boolean sent = SomeEventUtil.sendEvent( new Event(...) );

or even this:

boolean sent = SomeEventUtil.sendEvent( topic, props );
Re: obtaining a reference to EventAdmin too complex [message #513606 is a reply to message #513492] Wed, 10 February 2010 16:48 Go to previous message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 437
Registered: July 2009
Senior Member
You should look at using ServiceTracker to track the service or Declarative Services to inject the service.

For ServiceTracker it would be something like this:

ServiceTracker eventAdminTracker =
new ServiceTracker(context, EventAdmin.class.getName(), null);
eventAdminTracker.open();

....

EventAdmin eventAdmin = (EventAdmin) eventAdminTracker.getService();
if (eventAdmin != null) {
// do your stuff here
}

If using a ServiceTracker then you should create and open it when your bundle is activated. Then you can use the tracker throughout your code to obtain the EventAdmin service.

HTH.

Tom.
Previous Topic:Declarative Services (DS) do not work as expected
Next Topic:Filter cyclic service registrations in EventHook
Goto Forum:
  


Current Time: Sat Nov 29 05:24:37 GMT 2014

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

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