如何在Karaf上共享CXF:在多个OSGI捆绑上的公共汽车



在我的应用程序中,我有2个捆绑包。两者都使用CXF创建RESTFUL服务器。在这些捆绑中,我通过蓝图加载CXF。我定义了CXF:这些捆绑包上的BUS 具有相同的ID 期望两个捆绑包将共享相同的总线实例,然后我可以在一个捆绑包上配置一个身份验证式iTectionce interceptor,并且它将适用于另一个捆绑包。它们看起来像下面。

捆绑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/ret1/"都将属于Authinterceptor,而所有休息请求都不是" CXF/REST2"。

谁能给我一些有关如何共享相同CXF的建议:两个捆绑包上的公共汽车?先感谢您。

由于我无法用蓝图解决此问题,所以我改变了修复它的方法。我使用激活器启动了CXF总线REST服务器,而不是蓝图。

捆绑1激活器:在Bundle 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 Activator 我得到默认的总线,注册并为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();
}

==>结果:现在两个捆绑包都使用相同的总线,捆绑包2可以遇到身份验证拦截器,我在捆绑包1上也注册了。!!!

最新更新