OSGI捆绑无法启动

时间:2014-03-14 13:44:36

标签: java eclipse osgi

我有一个OSGI包,它进入RESOLVED状态,但从未到达ACTIVE状态。当我运行我的应用程序时,这是我获得的堆栈跟踪:http://tny.cz/fa949f16

之后,当我尝试通过OSGI控制台显式启动软件包时,出现此错误:

start 53
gogo: BundleException: The activator rsy.home.mac.sm.schedule.service.win.WinServiceActivator for bundle rsy.home.mac.sm.schedule.service.win is invalid

请注意,在stacktrace输出结束时,会出现以下消息:

  

!ENTRY org.eclipse.osgi 4 0 2014-03-14 10:52:50.984!MESSAGE Bundle   rsy.home.mac.sm.schedule.service.win_1.0.0 [53]未激活。

这里是来自WinServiceActivator的代码:

package rsy.home.mac.sm.schedule.service.win;

import java.util.HashMap;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;


public class WinServiceActivator implements BundleActivator {

    private static BundleContext context;

    @Override
    public void start(BundleContext context) throws Exception {

        ServiceActivator.context = context;

        ScheduleService schedServ = new ScheduleService();

        schedServ.setTdMapping(new HashMap<String, String>());
        schedServ.setHtMapping(new HashMap<String, String>());
        schedServ.setDoMapping(new HashMap<String, String>());

        context.registerService(ScheduleService.class.getName(), 
                schedServ, null);
    }

    @Override
    public void stop(BundleContext context) throws Exception {

        context.ungetService(context.getServiceReference(ScheduleService.class.getName()));
        ServiceActivator.context = null;
    }
}

这是我的MANIFEST文件:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SM Schedule Query Service
Bundle-SymbolicName: rsy.home.mac.sm.schedule.service.sm;singleton:=true
Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Service-Component: OSGI-INF/sm_schedule_service.xml
Require-Bundle: rsy.home.mac.sm.jaxrs.lib;bundle-version="1.0.0",
 rsy.home.mac.sm.config;bundle-version="0.1.0",
 rsy.home.mac.log,
 rsy.home.mac.sm.model;bundle-version="1.0.0",
 rsy.home.mac.sm.schedule.service;bundle-version="1.0.0",
 rsy.home.mac.sm.sm.scheduletable;bundle-version="1.0.0",
 resources;bundle-version="1.0.0",
 rsy.home.mac.portal.utilities,
 rsy.home.mac.portal.logging;bundle-version="1.0.0",
 org.eclipse.xsd;bundle-version="2.7.0",
 org.eclipse.osgi
Import-Package: org.osgi.service.http;version="1.2.1"
Bundle-ActivationPolicy: lazy
Bundle-Activator: rsy.home.mac.sm.schedule.service.sm.SmServiceActivator
Export-Package: rsy.home.mac.sm.schedule.service.sm

在谷歌搜索错误&#34;激活器启动错误时,ClassCastException:xxxx无法转换为org.osgi.framework.BundleActivator&#34;,我发现了这个:

That error is telling you that you have two copies of the BundleActivator class loaded into your VM somehow. The framework is using one and your bundle is using another.

Do you have any other bundles exporting org.osgi.framework? Where is your bundle getting this package from? If you issue the following command in the Felix shell you can see the wiring:

inspect package requirement <bundle-id>

or shortened to:

inspect p r <bundle-id>

Where <bundle-id> is the ID of your bundle, then you should see from where it is getting org.osgi.framework. If it is not the system bundle (org.apache.felix.framework), then you have an issue.

当我执行给定的命令时,我获得了这个输出:

org.osgi.framework; version="1.7.0" -> org.eclipse.osgi_3.9.0.v20130410-1557 [0]

你认为这就是捆绑没有开始的原因吗?如果是这样,我该如何解决这个问题呢?根据我在互联网上发现的评论和下面的人的评论和答案,我试图在MANIFEST文件中替换这一行:

Import-Package: org.osgi.service.http;version="1.2.1"

这一个:

Import-Package: org.osgi.framework

我在代码中遇到了一大堆错误,Eclipse的原因是:

  

此行有多个标记      - 访问限制:由于对所需库的限制,无法访问BundleActivator类型   rsy.home.mac.sm.jaxrs.lib / LIB / org.osgi.core-4.2.0.jar      - 访问限制:由于对所需库的限制,无法访问BundleListener类型   rsy.home.mac.sm.jaxrs.lib / LIB / org.osgi.core-4.2.0.jar

我真的很感激这方面的帮助......谢谢!

1 个答案:

答案 0 :(得分:2)

前一段时间我尝试让CXF在eclipse rcp应用程序中工作时遇到了类似的异常。原因是我确实需要在org.eclipse.osgi上进行捆绑。你应该避免这种情况。

Require bundle意味着您导入该捆绑包的所有包。框架包(org.eclipse.osgi)导出OSGi API。您的require bundle列表中列出的另一个bundle也可能导出BundleActivator包。所以你从两个来源获得这个类,并且可能使用与框架本身不同的类。

这些问题是为什么你应该避免需要捆绑的问题。特别是尽量避免在org.eclipse.osgi上执行require bundle。该捆绑包还会导出系统导出中定义的所有包。因此它会导出大量的软件包,并且遇到问题的可能性很高。在我使用cxf的情况下,我遇到了与org.eclipse.osgi导出的jaxb api类似的问题,以及cxf带来的其中一个包。

本文可能会为您提供更多详细信息,因为您观察到的问题通常表现为使用约束违规。 http://njbartlett.name/2011/02/09/uses-constraints.html

一般情况下,如果你有机会使用maven bundle plugin或bndtools来生成Manifest。两者都具有良好的默认值,可以避免大多数这些问题。