无法以编程方式启动捆绑包

时间:2017-06-04 08:23:20

标签: osgi osgi-bundle embedded-osgi

注意:就我而言,我正在使用Apache Felix实施,如果这很重要。

我已经编写了捆绑包,我正在使用它作为测试。这是非常简单的“Hello World”捆绑包,除了在启动/停止时将消息打印到stdout之外,它只会执行此操作:

public class Activator implements BundleActivator {

    @Override
    public void start(BundleContext context) throws Exception {
        System.out.println("Hello, World.");
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye, World.");
    }

}

还有MANIFEST文件,因为我从标准发行版(which can be downloaded here)开始通过Apache Felix控制台部署上面的捆绑包时发布并打印出消息,所以发布这个文件毫无意义。

我要做的下一步是使用编程方法部署完全相同的bundle。不幸的是,这对我不起作用。我的代码如下:

public static void main(String[] args) throws Exception {
    FrameworkFactory frameworkFactory = getFrameworkFactory();
    Framework framework = frameworkFactory.newFramework(null);

    System.out.println("BundleID = " + framework.getBundleId());
    System.out.println("State = " + getState(framework.getState()));

    framework.init();

    System.out.println("BundleID = " + framework.getBundleId());
    System.out.println("State = " + getState(framework.getState()));

    BundleContext bundleContext = framework.getBundleContext();
    bundleContext.addBundleListener((event) -> {
        System.out.println("Bundle Changed Event");
    });
    bundleContext.addFrameworkListener((event) -> {
        System.out.println("Framework Event");
    });
    bundleContext.addServiceListener((event) -> {
        System.out.println("Service Changed Event");
    });

    Bundle bundle = bundleContext.installBundle("file://<absolute-path-to-bundle-jar-same-as-above");

    System.out.println("BundleID = " + bundle.getBundleId());
    System.out.println("State = " + getState(bundle.getState()));

    bundle.start();

    System.out.println("BundleID = " + bundle.getBundleId());
    System.out.println("State = " + getState(bundle.getState()));
}

private static FrameworkFactory getFrameworkFactory() throws IllegalStateException {
    ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);

    FrameworkFactory factory = null;
    for (FrameworkFactory iterator : loader) {
        if (factory != null) {
            throw new IllegalStateException("Ambiguous SPI implementations.");
        }

        factory = iterator;
    }

    return factory;
}

private static String getState(int state) {
    switch (state) {
    case Bundle.UNINSTALLED:
        return "UNINSTALLED";
    case Bundle.INSTALLED:
        return "INSTALLED";
    case Bundle.RESOLVED:
        return "RESOLVED";
    case Bundle.STARTING:
        return "STARTING";
    case Bundle.STOPPING:
        return "STOPPING";
    case Bundle.ACTIVE:
        return "ACTIVE";
    default:
        throw new IllegalStateException("Unknown state");
    }
}

输出如下:

BundleID = 0
State = INSTALLED
BundleID = 0
State = STARTING
Bundle Changed Event
BundleID = 1
State = INSTALLED
BundleID = 1
State = INSTALLED

据我所知,bundle已安装但最后4行表明bundle.start()因某些原因被忽略。

你能指出我做错的工作吗?

1 个答案:

答案 0 :(得分:0)

经过一小时的调试并更仔细地阅读javadoc,这种情况正在发生,因为框架只是初始化而不是被启动。要进行示例工作,您只需在framework.start()之后添加framework.init()(或者只要调用framwork.start()调用framework.init(),如果有必要的话)。

我要留下这些信息,因为很少有令人困惑的事情:

  1. Apache Felix的官方文档包含有关将框架嵌入主机应用程序的信息。不幸的是,只有使用Apache Felix自定义机制的示例使其无法移植到其他实现。令人困惑的是warning note,如果您想要创建便携式解决方案,则应使用init()getBundleContext()。引用的全文如下:
  2.   

    警告 felix.systembundle.activators配置属性特定于Felix框架实现。如果您希望代码与其他框架实现一起使用,则应在框架实例上调用init()并直接使用getBundleContext()。否则,方法将非常相似。

    1. 无参数版init()方法的JavaDoc没有提及初始化与启动框架不同,尽管init(FrameworkListener...)有这样的信息。
    2.   

      在调用start之前,实际上不会启动此框架。

相关问题