gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:00:03 27 4
gpt4 key购买 nike

我尝试按如下方式在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com