How to add OSGI service references in contexts property of httpserver (Spring remoting)?

705 Views Asked by At

I use Spring remoting and OSGI.

   <bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
        <property name="contexts">
        <util:map>
            <entry key="/remoting/Service1" value-ref="serviceBean1"/>
        </util:map>
        </property>
        <property name="port" value="8080" />
   </bean>   

I declare an dynamic list as below :

<osgi:list id="serviceList" interface="com.xyz.IRemoteService" member-type="service-object">

Now these services can be registered dynamically. At one point of time serviceList will hold all the service references implementing com.xyz.IRemoteService. How can I set this service list to the contexts property?

Update 1 :

The com.xyz.IRemoteService has two methods one which returns the key i.e. the url and the other returns the SimpleHttpInvokerServiceExporter object.So I changed teh configurations to as

<bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
    <property name="contexts">
        <util:map>
        <entry key="/remoting/ArchiveEDSImpl" value-ref="archiveEDS" />
        <entry key="#{serviceList[0].url}" value="#{serviceList[0].httpHandler}" />
        <entry key="#{serviceList[1].url}" value="#{serviceList[1].httpHandler}" />
        </util:map>
    </property>
    <property name="port" value="8081" />
</bean>

<osgi:list id="serviceList" interface="com.xyz.IRemoteServiceProvider" member-type="service-object">
</osgi:list>

This is great but works only if there are at least 2 elements in the serviceReferenceList when bean httpserver is initialized. How do I configure this dynamically based on the size in the list ??

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks for the reply Sheena. It turns out that SimpleHttpServerFactoryBean holds a copy of the Map and not the reference. So contexts property will be the set when the bean is being initialized.I could not find any other way to update it.

As a workaround I had to refresh the bundle in which the SimpleHttpServerFactoryBean bean is initialized.

Please let me know if there is a better solution.

0
On

You can, instead of using util:map, define a factory or a bean that wires in IRemoteServiceProvider and outputs a Map.

class MyMapFactory {
  List<IRemoteServiceProvider> providers;
  public Map buildMap() {
    //build Map of urls to http handlers here.
    // if the map wraps the list directory it can possibly even be dynamic! Depending on
    // whether SimpleHttpServerFactoryBean clones the map or uses it directly.
  }
}

<bean class="MyMapFactory" id="myMapFactory"/>

<bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
  <property name="contexts">
    <bean factory-bean="myMapFactory" factory-method="buildMap" class="java.util.Map"/> 
  </property>
  <property name="port" value="8081" />
</bean>

<osgi:list id="serviceList" interface="com.xyz.IRemoteServiceProvider" member-type="service-   object">
</osgi:list>