如何启动JavaFx OSGi包

时间:2017-02-01 12:05:38

标签: java maven intellij-idea javafx osgi

我试图运行JavaFx OSGi模块,但我一直收到错误:

org.osgi.framework.BundleException: Unable to resolve OSGiDmHelloWorldProvider [7](R 7.0): missing requirement [OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application) Unresolved requirements: [[OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application)]
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4112)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2118)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)

这是我加载和启动模块的方式:

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

import java.io.File;
import java.util.*;


public class Launcher {

    private static String[] libs = null;

    private BundleContext context;

    private Launcher() {

        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();

        Map<String, String> config = new HashMap<String, String>();
        config.put("osgi.console", "");
        config.put("osgi.clean", "true");
        config.put("osgi.noShutdown", "true");
        config.put("eclipse.ignoreApp", "true");
        config.put("osgi.bundles.defaultStartLevel", "4");
        config.put("osgi.configuration.area", "./configuration");

        // automated bundles deployment
        config.put("felix.fileinstall.dir", "./dropins");
        config.put("felix.fileinstall.noInitialDelay", "true");
        config.put("felix.fileinstall.start.level", "4");

        config.put(Constants.FRAMEWORK_BOOTDELEGATION, "javafx.*,com.sun.javafx.*");
        config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_APP);

        Framework framework = frameworkFactory.newFramework(config);

        try {
            framework.init();
            framework.start();
        } catch (BundleException e) {
            e.printStackTrace();
        }

        context = framework.getBundleContext();

        Bundle OSGiDmHelloWorldProvider = install("OSGiDmHelloWorldProvider");
        try {

            OSGiDmHelloWorldProvider.start();

        } catch (BundleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Launcher();
    }

    private String[] getLibs() {
        if (libs == null) {
            List<String> jarsList = new ArrayList<String>();
            File pluginsDir = new File("libs");

            System.out.println("PATHS : " + pluginsDir.getAbsolutePath());

            for (String jar : pluginsDir.list()) {
                jarsList.add(jar);
            }
            libs = jarsList.toArray(new String[jarsList.size()]);
        }
        return libs;
    }

    protected Bundle install(String name) {
        String found = null;

        for (String jar : getLibs()) {
            if (jar.startsWith(name)) {
                found = String.format("file:libs/%s", jar);
                System.out.println(found);
                break;
            }
        }
        if (found == null) {
            throw new RuntimeException(String.format("JAR for %s not found", name));
        }
        try {
            return context.installBundle(found);
        } catch (BundleException e) {
            e.printStackTrace();
        }
        return null;
    }
}

OSGiDmHelloWorldProvider Activator类:

import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProviderActivator implements BundleActivator {

    Logger logger = LoggerFactory.getLogger(ProviderActivator.class);

    Stage stage;

    private ServiceRegistration registration;
    BundleContext bundleContext;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        registration = bundleContext.registerService(
                HelloWorldService.class.getName(),
                new HelloWorldServiceImpl(),
                null);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                stage = new Stage();
                BorderPane pane = new BorderPane();
                Scene scene = new Scene(pane, 400, 200);
                pane.setCenter(new Label("This is a JavaFX Scene in a Stage"));
                stage.setScene(scene);
                stage.show();
            }
        });

        System.out.println("STAGE CALLED !!!");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        registration.unregister();

        logger.info("Set4Jfx Bundle: stop()");
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                stage.close();
            }
        });
    }
}

如何在Maven OSGi应用程序中加载使用JavaFx的模块?

提前谢谢大家。

2 个答案:

答案 0 :(得分:1)

错误消息表示您的包导入包javafx.application,并且没有包导出该包。因此导入无法解决。

我注意到在您的启动器代码中,您尝试将bootdelegation设置为javafx.*可能允许从引导类路径加载类,如果你的bundle一直运行,但由于未解析的导入,它无法达到那么远。

如果您打算从引导类加载器加载JavaFX类,那么您应该从自己的bundle中删除包导入。您还必须安排引导类加载器实际提供JavaFX类,因为AFAIK通常只能从扩展类加载器中看到。

然而,更好的解决方案是单独保留导入,并安排从包中导出javafx.application包。可以使用Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA从系统包中完成。

<强>更新

您应该从@Puce查看Drombler FX工具/框架,因为听起来他已经完成了很多这些设置任务。但是我觉得他的回答没有直接解决你的代码失败原因的问题。

答案 1 :(得分:0)

我发布了Drombler FX的第一个版本 - 基于OSGi和Maven(POM优先)的JavaFX模块化应用程序框架。

负责初始化JavaFX和OSGi。

也许你觉得它很有用。应用程序框架是开源的,代码可以在GitHub上找到。

还有tutorial Getting Started个跟踪。