cxf - 如何分享cxf :bus over multiple osgi bundles on karaf?

标签 cxf osgi-bundle karaf blueprint-osgi

在我的应用程序中,我有 2 个 bundle 。两者都使用 cxf 创建 Restful 服务器。 在这些包中,我通过蓝图加载了 cxf。我在那些具有相同 id 的包上定义了 cxf:bus 以期望两个包将共享相同的总线实例,然后我可以在一个包上配置身份验证拦截器,它也将应用于另一个包。 它们如下所示。

bundle 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>

bundle 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 的建议? 先感谢您。

最佳答案

由于我无法用蓝图解决这个问题,所以我换了一种方法来修复它。 我使用 Activator 启动了 CXF Bus Rest Server,而不是蓝图。

Bundle 1 激活器: 在 bundle 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();
}

在 bundle 2 中, Bundle 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 上注册的身份验证拦截器。 !!!

关于cxf - 如何分享cxf :bus over multiple osgi bundles on karaf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41031872/

相关文章:

java - CXF 超时不起作用

java - 如何将 OSGI 包添加到 Eclipse RCP 项目

java - Karaf - TransactionRequiredException

java - 需要帮助将脚本引擎加载到 Karaf 中

java - 执行 org.apache.maven.plugins :maven-shade-plugin:1. 3.1 :shade: java. lang.AbstractMethodError: null 时遇到 API 不兼容

java - 如何禁用 SOAP 1.1 与 Apache CXF 的绑定(bind)

java - 使用 Apache CXF wsdl2java 生成具有完整构造函数的类

java - osgi 容器中的 Spring 组件扫描损坏

osgi - 如何添加自定义 osgi 包作为新的依赖项?

java - 如何在 apache karaf 上启用单点登录和 session 登录?