2016-12-08 36 views
2

在我的應用程序中,我有2捆。兩者都使用cxf來創建Restful服務器。 在這些包中,我通過藍圖加載cxf。我定義了這些包上的cxf:bus ,使用相同的ID,期望兩個bundle將共享相同的總線實例,然後我可以在一個bundle上配置一個Authentication攔截器,並且它也將應用於其他bundle。 他們看起來像下面。如何通過karaf上的多個osgi包共享cxf:bus?

的羣1:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" 
     xmlns:cxf="http://cxf.apache.org/blueprint/core" 
     xsi:schemaLocation=" 
     http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd 
     http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd 
     http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd 
     "> 
<bean id="authInterceptor" class="com.dasannetworks.vn.rest.impl.AuthInterceptorImpl"></bean> 
<cxf:bus id="my-app-bus" name="tutorial"> 
    <cxf:inInterceptors> 
     <ref component-id="authInterceptor"/> 
    </cxf:inInterceptors> 
</cxf:bus> 


<bean id="Rest1ServiceImpl"class="com.dasannetworks.vn.rest.impl.Rest1ServiceImpl"></bean> 

<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"> 
    <property name="serializeAsArray" value="true"/> 
    <property name="dropRootElement" value="true"/> 
    <property name="supportUnwrapped" value="true"/> 
</bean> 

<jaxrs:server id="custom1Service" address="/rest1"> 
    <jaxrs:serviceBeans> 
     <ref component-id="rest1ServiceImpl"/> 
    </jaxrs:serviceBeans> 
    <jaxrs:providers> 
     <ref component-id="jsonProvider"/> 
    </jaxrs:providers> 
</jaxrs:server> 

束2:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" 
     xmlns:cxf="http://cxf.apache.org/blueprint/core" 
     xsi:schemaLocation=" 
     http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd 
     http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd 
     http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd 
     "> 

<cxf:bus id="my-app-bus" bus="tutorial"></cxf:bus> 
<bean id="rest2Service" class="com.dasannetworks.vn.rest2.impl.Rest2ServiceImpl" /> 
<jaxrs:server id="custom2Service" address="/rest2"> 
    <jaxrs:serviceBeans> 
     <ref component-id="rest2Service"/> 
    </jaxrs:serviceBeans> 

安裝和運行後: 結果: 所有將請求休息到「/ cxf/rest1 /」將會運行authInterceptor,而所有休息請求都不會是「cxf/rest2」。

誰能給我有關如何共享相同的CXF一些建議:在兩個包巴士? 預先感謝您。

+0

也許你可以在其他包定義總線,將其導出爲一個服務,然後導入總線作爲一個OSGi服務。 –

回答

1

因爲我解決不了這個問題的藍圖,我改變來解決它的辦法。 取而代之的是藍圖,我用Activator啓動了CXF Bus Rest Server。

包1個激活: 在束1,我得到了默認總線,並添加認證攔截器就可以了,然後就可以註冊一個REST服務器端點。

public void start(BundleContext bundleContext) throws Exception { 
    Bus defaultBus = BusFactory.getDefaultBus(); 
    defaultBus.setId("my-app-bus"); 
    BusFactory.clearDefaultBusForAnyThread(defaultBus); 
    System.out.println(this.getClass().getName()); 
    System.out.println(defaultBus.getId()); 
    defaultBus.getInInterceptors().clear(); 
    defaultBus.getInInterceptors().add(new AuthInterceptorImpl()); 

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); 
    sf.setBus(defaultBus); 
    sf.setAddress("/tutorial"); 
    sf.setServiceClass(HelloRestServiceImpl.class); 
    sf.create(); 
} 

捆綁2, 包2激活 我得到了默認總線,註冊和設置總線REST服務器。

@Override 
public void start(BundleContext bundleContext) throws Exception { 
    Bus defaultBus = BusFactory.getDefaultBus(); 
    List<Interceptor<? extends Message>> inInterceptors = defaultBus.getInInterceptors(); 
    for(Interceptor interceptor: inInterceptors) { 
     System.out.println(interceptor.getClass().getName()); 
    } 

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); 
    sf.setBus(defaultBus); 
    sf.setAddress("/Rest2"); 
    sf.setServiceClass(Rest2ServiceImpl.class); 
    sf.create(); 
} 

==>結果:兩個bundle現在使用相同的總線,並且bundle 2可以運行到我在bundle 1上註冊的身份驗證攔截器。 !