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

发布于 2024-08-15 06:59:09 字数 190 浏览 10 评论 0原文

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

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

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

请帮忙。

I can't find information about starting and using Apache Felix from code.

I want do things which I am able (or unable ;) ) to do with Apache Felix's Shell.

For example, how do I start a module?

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-08-22 06:59:09

因此,经过大量尝试后,这里有一个可能的解决方案

我们将有两个项目:

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

项目 OsgiModuleA

文件 module/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);
  }

}

文件 manifest.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...

So, after trying around a lot of time, here's a possible solution.

We will have two projects:

  • OsgiModuleA, which contains a module-to-be-started
  • OsgiUsage, which will use the "Module A"

Project OsgiModuleA

File module/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);
  }

}

File manifest.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

Project OsgiUsage

File usage/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);
  }

}

Output

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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文