java - 如何以编程方式启动 OSGi

标签 java maven osgi

我尝试按如下方式在 Java 中启动 OSGi:

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
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");

        Framework framework = frameworkFactory.newFramework(config);

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

        context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();

        Bundle OSGiDmHelloWorldProvider = install("OSGiDmHelloWorldProvider");
        Bundle OSGiDmHelloWorldConsumer = install("OSGiDmHelloWorldConsumer");
        try {
            OSGiDmHelloWorldProvider.start();
            OSGiDmHelloWorldConsumer.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");
            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 + "_") || jar.startsWith(name + "-")) {
                found = String.format("file:libs/%s", jar);
                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;
    }
}

这是 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.sarxos</groupId>
    <artifactId>equinox-launcher</artifactId>
    <version>0.1-SNAPSHOT</version>

    <name>equinox-launcher</name>
    <description>Launcher for Equinox OSGi runtime</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>copy-libs</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>libs</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
        </dependency>

    </dependencies>
</project>

我收到此错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:365)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at com.github.sarxos.equinox.Launcher.<init>(Launcher.java:21)
    at com.github.sarxos.equinox.Launcher.main(Launcher.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我错了什么?我需要调整什么才能使其正常工作?

最佳答案

该错误告诉您找不到 FrameworkFactory,因为您不依赖任何 OSGi 框架。在您的 pom 中添加对 equinox 或 felix 的依赖,它应该可以工作。

关于java - 如何以编程方式启动 OSGi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41721988/

相关文章:

java - BufferedWriter 导致中断

maven - 是否有 Maven 相当于 "gradle dependencyInsight"?

java - 如何使用新的 Google App Engine Maven 插件推广已部署的版本?

java - OSGi - 我应该为每个包创建服务跟踪器吗?

java - 如何在android中设备方向改变后保持listview项目的高亮

java.lang.NoSuchMethodException : setHomeActionContentDescription [int]? 异常

java - 使用Javadocs生成Swagger文档

spring - 我在哪里可以找到 Spring 4 OSGi 包

java - 在线程运行时使用 OSGi 控制台

java - 如何在模型- View - Controller 中为模型设计接口(interface)?