gpt4 book ai didi

java - 如何从代码启动和使用 Apache Felix?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:48:12 25 4
gpt4 key购买 nike

我无法从代码中找到有关开始使用Apache Felix的信息。

我想用 Apache Felix 的 Shell 做我能够(或不能 ;))的事情。

例如,我如何启动一个模块?

请帮忙。

最佳答案

因此,在尝试了很多时间之后,这里有一个可能的解决方案

我们将有两个项目:

  • OsgiModuleA,其中包含一个要启动的模块
  • OsgiUsage,它将使用“模块 A”

OsgiModuleA 项目

文件模块/a/Activator.java

package module.a;

import org.osgi.framework.*;

public class Activator
implements BundleActivator, ServiceListener {

@Override
public void start(BundleContext bc)
throws Exception {

System.out.println("*** started");
bc.addServiceListener(this);
}

@Override
public void stop(BundleContext bc)
throws Exception {

bc.removeServiceListener(this);
System.out.println("*** stopped");
}

@Override
public void serviceChanged(ServiceEvent se) {
String s;

switch (se.getType()) {
case ServiceEvent.MODIFIED:
s = "modified";
break;
case ServiceEvent.REGISTERED:
s = "registered";
break;
case ServiceEvent.UNREGISTERING:
s = "unregistered";
break;
default:
s = "unknown";
}

System.out.println("= = = service changed: " + s);
}

}

文件 list .mf

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Bundle-Name: Module A
Bundle-Description: Module A
Bundle-Vendor: me
Bundle-Version: 1.0.0
Bundle-Activator: module.a.Activator
Import-Package: org.osgi.framework

项目OsgiUsage

文件使用/Main.java

package usage;

import java.util.*;
import org.apache.felix.framework.*;
import org.apache.felix.framework.util.*;
import org.osgi.framework.*;

public class Main {

static class HostActivator
implements BundleActivator {

private BundleContext context = null;

@Override
public void start(BundleContext bc) {
context = bc;
}

@Override
public void stop(BundleContext bc) {
context = null;
}

public BundleContext getContext() {
return context;
}

}

public static void main(String[] args)
throws Exception {

Map<String, Object> map = new HashMap<String, Object>();

map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"some.module.i.dont.know; version=1.0.0");

HostActivator activator = new HostActivator();

List<Object> list = new LinkedList<Object>();
list.add(activator);
map.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);

Felix f = new Felix(map);

System.out.println("starting OSGI...");
f.start();

Bundle b = f.getBundleContext().installBundle(
"file:../OsgiModuleA/dist/OsgiModuleA.jar"); // dirty path ;)
String bName = b.getLocation();

System.out.println("starting bundle " + bName);
b.start();

Set<String> inUse = new HashSet<String>();
for (ServiceReference sr : f.getServicesInUse()) {
inUse.add(sr.toString());
}
for (ServiceReference sr : f.getRegisteredServices()) {
String flag = (inUse.contains(sr.toString()) ? "[in use]" : "[ ]");
System.out.println("+ registered service: " + flag + " " + sr.toString());
}

System.out.println("stopping bundle " + bName);
b.stop();

System.out.println("uninstalling bundle " + bName);
b.uninstall();

System.out.println("stopping...");
f.stop();
f.waitForStop(1000);
}

}

输出

starting OSGI...
starting bundle file:../OsgiModuleA/dist/OsgiModuleA.jar
*** started
+ registered service: [in use] [org.osgi.service.startlevel.StartLevel]
+ registered service: [ ] [org.osgi.service.packageadmin.PackageAdmin]
stopping bundle file:../OsgiModuleA/dist/OsgiModuleA.jar
*** stopped
uninstalling bundle file:../OsgiModuleA/dist/OsgiModuleA.jar
stopping...

关于java - 如何从代码启动和使用 Apache Felix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887809/

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