Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Properties in OSGi Declarative Services and ServiceTracker

DZone's Guide to

Properties in OSGi Declarative Services and ServiceTracker

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

As proposed in my last blog "Dependencies and Services in OSGI Enterprise Applications" it seems that a new blog series was started ;-)

ServiceTracker vs Declarative Services

There was only a short statement last time - now some tips follow how to use properties in ServiceTracker vs Declarative Services (DS).

A ServiceTracker from my OSGI Server project was opened with this Filter:

final String filterPool = "("
+Constants.OBJECTCLASS
+ "="
+ JDBCPoolComponent.class.getName()
+")";


So the ServiceTracker will get Services of Type JDBCPoolComponent - we are interested into the attribut JndiName:

private void processDataSources(JDBCPoolComponent service) {
  if (service.getJndiName().equals("foo_entity_data_source_hsql")) {
  // do something
  }
}


How could this be done using Declarative Services (equinox.ds): 

We're defining a reference to get exactly one specific service of type JDBCPoolComponent to know that a DataSource is ready. 

A first idea to declare this reference could be:



Launching the OSGI application, but the reference wasn't resolved. 
How could we find the reason ? At first we should test if the Filter is correct and there's no typo in the Interface.
Tip: Use of Equinox OSGI Console to test the Filter !
Try this command:
services (&(objectclass=org.ow2.easybeans.component.jdbcpool.JDBCPoolComponent)
(jndiName=foo_entity_data_source_hsql))
 

But no Service was found: No registered Services.As next we try only to look for Services of Interface JDBCPoolComponent. Now our command looks like: 

services (objectclass=org.ow2.easybeans.component.jdbcpool.JDBCPoolComponent)

Some Services were listed - we also see the one we're looking for

{org.ow2.easybeans.component.jdbcpool.JDBCPoolComponent}={classname=org.ow2.easybeans.component.jdbcpo......, xmlconfig=
  Registered by bundle: .../easybeans-component-jdbcpool_1.1.0-M3-SNAPSHOT.jar/

Analyzing the printed text we found: jndiName="foo_entity_data_source_hsql"But there's a small difference: its the Property xmlconfig containing the informations. Thats the solution:

services (&(objectclass=org.ow2.easybeans.component.jdbcpool.JDBCPoolComponent)
(xmlconfig=*foo_entity_data_source_hsql*))

The Service we want to reference will be found and we know how to declare the reference inside the Service Component:




Why can the ServiceTracker use  getJndiName(), but the target Filter can't ?getJndiName() is a Methode of JDBCPoolComponent, but JndiName isn't registered as Service Property ! 

Target Filter cannot access methods of implementation - only Service Properties. 

You maybe run into this using 3rdParty bundles where you have to live with their services as-is. If you're in luck its Open Source, you write a Bugzilla or JIRA and get it fixed in hours :-)

Remark:

You can take a look at my blog series about "OSGI Enterprise Applications" - the index is in the column right beside this blog.

There are also some submissions for EclipseCon 2009 - if you want to hear more about: comments are welcome ;-)

From http://ekkes-corner.blogspot.com/

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}