【问题标题】:How to share cxf:bus over multiple osgi bundles on karaf?如何在 karaf 上通过多个 osgi 包共享 cxf:bus?
【发布时间】:2016-12-08 04:38:00
【问题描述】:

在我的应用程序中,我有 2 个捆绑包。两者都使用 cxf 创建 Restful 服务器。 在这些包中,我通过蓝图加载 cxf。我在那些 具有相同 id 的捆绑包上定义 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:bus 的建议? 先感谢您。

【问题讨论】:

  • 也许您可以在其他包中定义总线,将其导出为服务,然后将总线导入为 OSGi 服务。

标签: cxf osgi-bundle karaf blueprint-osgi


【解决方案1】:

由于我不能用蓝图解决这个问题,我改变了修复它的方法。 我没有使用蓝图,而是使用 Activator 启动了 CXF Bus Rest Server。

捆绑 1 激活器: 在捆绑包 1 中,我获取默认总线并在其上添加身份验证拦截器,然后在其上注册一个 Rest Server 端点。

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 Server 注册并设置了该总线。

@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 上注册的身份验证拦截器。 !!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多