PDA

View Full Version : Accessing osgi:list attributes from Java...


mburbidg
Jun 9th, 2008, 01:40 PM
I'm importing a list of Spring-DM based osgi services using the following line in my context file:


<list id="views" interface="org.springframework.web.servlet.View"/>


I took the example from the documentation for iterating through a list of services and modified for my purposes. Here is how I'm iterating over the list from above. I injected a reference to the views bean into one of my beans. My bean has a field named views of type List. The code that follows is inside one of the methods of my bean. Somehow it seems like I should be able to get at the implementation name of the bean in the exporting bundle from within the iteration. In the xml you can filter services based on the spring bean name that implements the service. Is there a way to get at that name inside the iterator shown below? If so how would I do it?


for (Iterator iterator = views.iterator(); iterator.hasNext();) {
View view = (View) iterator.next();
view.executeOperation();
}


Thanks,
Michael-

Costin Leau
Jun 20th, 2008, 07:40 AM
Michael,

the attributes of your services are kept separate from the service itself in OSGi. To access this, in Spring-DM you can cast your objects to ImportedOsgiServiceProxy interface that offers you additional service properties including the service reference of your object (and thus its properties).
We were thinking of allowing injection of service reference (rather then service themselves) for collections but the problem is that under jdk 1.4 it's impossible to determine whether you want a ServiceReference or a Service.
For your case this could work since you could potentially have two collections - one with services and one with service references that would be updated automatically and using the index, you could access the properties of the service you're looking for.
However, the problem with that approach would be that they could be potentially updated independently especially due to the dynamics of the OSGI and thus your index can become invalid - however, you could do two iterations a the same time but again, since locking is not cumulative, you might have side-effects.
Basically my advice - use the Spring-DM interface.