PDA

View Full Version : Importing HandlerMapping service to war...


mburbidg
Jun 11th, 2008, 04:39 PM
I have a web application written with Spring-MVC/Spring-DM. I'm been able to modularize it in many compelling ways. I've very pleased with the architecture! Spring-DM/OSGi is cool.

Now I'm trying to modularize the web portion of the application. I have created a bundle that implements a spring controller, lets call this my service bundle. It declares a SimpleUrlHandlerMapping bean and registers the controller in this bean. I would like to export that mapping bean to the war bundle.

Here's the SimpleUrlHandlerMapping bean that is declared in my service bundle.


<bean id="sampleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/subtract">subtractController</prop>
</props>
</property>
</bean>

<osgi:service ref="sampleUrlMapping" auto-export="interfaces"/>


I can import that bean into my war by including the following import service in the context file for my war.


<osgi:reference id="sampleUrlMapping">
<osgi:interfaces>
<value>org.springframework.web.servlet.HandlerMapping</value>
<value>org.springframework.core.Ordered</value>
</osgi:interfaces>
</osgi:reference>


This actually works and is pretty cool. The sampleUrlMapping bean from my service bundle is automatically recognized and used to map url to the controller bean in my service bundle.

So then I took the next step. I don't want my war to actually have to be aware of each bundle that may export handler mappings. So I changed the above to the following:


<osgi:list id="handlerMappings">
<osgi:interfaces>
<value>org.springframework.web.servlet.HandlerMapping</value>
<value>org.springframework.core.Ordered</value>
</osgi:interfaces>
</osgi:list>


I can see in the debugger that the service was imported, but it is not recognized by my war/Spring-MVC as a handler mapping. The url mapped in my service bundle is not recognized.

This is so close to being way cool. Does anyone have any ideas how I could get this to work?

Thanks,
Michael-

Costin Leau
Jun 20th, 2008, 07:57 AM
Michael, I'm glad to hear that things are working for you and you find the OSGi architecture (with Spring-DM) cool.
Back to your problem, Spring doesn't see the handlers since they are all part of a collection which is not recognized- simply create a handler (I believe there is already one) that registers the handler mapping with Spring-MVC directly - basically unwrapping the list/collection with Spring-MVC.
Due to the dynamics of OSGi, it might make sense to also consider the appearance/disappearance of services. So basically, you could simply create some sort of handler adapter that will be picked up automatically by spring and will delegate the map to the first handler from the list that accepts it - this way you can automatically have your application updated based on the service availability.
This is just an idea - there might be better ways to do this ...