Java 如何以编程方式启动OSGi
我尝试在Java中启动OSGi,如下所示:Java 如何以编程方式启动OSGi,java,maven,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
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;
}
}
import org.osgi.framework.Bundle;
导入org.osgi.framework.BundleContext;
导入org.osgi.framework.BundleException;
导入org.osgi.FrameworkUtil;
导入org.osgi.framework.launch.framework;
导入org.osgi.framework.launch.FrameworkFactory;
导入java.io.File;
导入java.util.*;
公共类启动器{
私有静态字符串[]libs=null;
私密文本语境;
专用启动器(){
FrameworkFactory=ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map config=newhashmap();
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”);
//自动捆绑包部署
config.put(“felix.fileinstall.dir”,“/dropins”);
config.put(“felix.fileinstall.noInitialDelay”、“true”);
config.put(“felix.fileinstall.start.level”,“4”);
frameworkFactory.newFramework(配置);
试一试{
framework.start();
}捕获(捆绑例外e){
e、 printStackTrace();
}
context=FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Bundle OSGiDmHelloWorldProvider=install(“OSGiDmHelloWorldProvider”);
Bundle OSGiDmHelloWorldConsumer=install(“OSGiDmHelloWorldConsumer”);
试一试{
OSGiDmHelloWorldProvider.start();
OSGiDmHelloWorldConsumer.start();
}捕获(捆绑例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
新发射器();
}
私有字符串[]getLibs(){
if(libs==null){
List jarsList=new ArrayList();
文件pluginsDir=新文件(“libs”);
for(字符串jar:pluginsDir.list()){
jarsList.add(jar);
}
libs=jarsList.toArray(新字符串[jarsList.size()]);
}
返回LIB;
}
受保护的捆绑包安装(字符串名称){
找到的字符串=null;
for(字符串jar:getLibs()){
if(jar.startsWith(name+“”)| | jar.startsWith(name+“-”){
found=String.format(“文件:libs/%s”,jar);
打破
}
}
if(found==null){
抛出新的RuntimeException(String.format(“未找到%s的JAR”,名称));
}
试一试{
返回context.installBundle(已找到);
}捕获(捆绑例外e){
e、 printStackTrace();
}
返回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>
4.0.0
com.github.sarxos
秋分发射器
0.1-1快照
秋分发射器
Equinox OSGi运行时启动器
org.apache.maven.plugins
maven编译器插件
3.6.0
1.6
1.6
org.apache.maven.plugins
maven依赖插件
3.0.0
复制库
编译
复制依赖项
自由基
org.osgi
org.osgi.core
6.0.0
我得到这个错误:
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)
线程“main”java.util.NoSuchElementException中的异常
位于java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:365)
位于java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
位于java.util.ServiceLoader$1.next(ServiceLoader.java:480)
位于com.github.sarxos.equinox.Launcher.(Launcher.java:21)
位于com.github.sarxos.equinox.Launcher.main(Launcher.java:58)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我怎么了?我需要调整什么才能让它工作?错误告诉您找不到FrameworkFactory,因为您对任何OSGi框架都没有依赖关系。在pom中添加对equinox或felix的依赖项,它应该可以工作。错误告诉您,找不到FrameworkFactory,因为您对任何OSGi框架都没有依赖项。在pom中添加对equinox或felix的依赖项,应该可以使用。您需要在类路径中放置OSGi框架实现。可能的重复需要在类路径中放置OSGi框架实现。可能的重复