有没有一种方法可以让一个捆绑包从 ServiceReference 获取另一个捆绑包的 Bundle 实例?

发布于 2024-10-11 12:57:31 字数 555 浏览 1 评论 0原文

我正在尝试创建一个包来监视服务注册,并根据服务接口的 API 包中嵌入的某些元数据执行一些附加任务。元数据主要由一个或多个属性文件组成,因此我的想法是使用 Bundle.findEntries() 但由于元数据嵌入在 API 包中,我不能只做类似 >ServiceReference.getBundle().findEntries() 因为这会尝试在服务实现包中查找属性,而不是在 API 包中。

我考虑过从 ServiceReference ObjectClass 属性获取服务 API 类名,然后使用 Package Admin 服务或 FrameworkUtil.getBundle(),但这两者都需要 < code>Class——但是如何获取服务接口的Class呢?执行此工作的包可能尚未导入该类的包,因此 Class.forName() 将不起作用。

我的另一个选择是监视捆绑包和服务事件:第一个创建包含元数据的捆绑包注册表,第二个选择在注册服务时使用第一个。在走这条路之前,我想看看是否有更简单的方法。

I'm trying to create a bundle that watches service registrations and, depending on certain metadata embedded in the API bundle for the service interface, performs some additional tasks. The metadata consists primarily of one or more properties files, so my thought was to use Bundle.findEntries() but since the metadata is embedded in the API bundle, I can't just do something like ServiceReference.getBundle().findEntries() as this would try to find the properties in the service implementation bundle, not in the API bundle.

I thought about getting the service API class name from the ServiceReference ObjectClass property and then using either the Package Admin service or FrameworkUtil.getBundle(), but both of these require a Class--but how do I get the Class of the service interface? The bundle that's doing this work probably hasn't imported the Class's package, so Class.forName() won't work.

My other option is to watch for both bundle and service events: the first creates a registry of bundles that contain the metadata, the second using the first when a service is registered. Before going down that path I'm looking to see if there's an easier way.

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

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

发布评论

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

评论(1

☆獨立☆ 2024-10-18 12:57:31

免责声明:我还没有尝试过这个,但我有理由确定它应该可以完成这项工作。

您可以从 ServiceReferenceObjectClass 获取包名称,所以现在我们有了它,我们可以在框架中找到该包。给定一个 PackageAdmin packageAdmin ,您可以执行类似的操作

public Bundle getExporterOf(String package, ServiceReference ref) {

  ExportedPackage[] packages = packageAdmin.getExportedPackages(packageName);
  if (packages == null) {
    return null;
  }
  for (ExportedPackage package : packages) {
    Bundle[] importers = package.getImportingBundles()) {
    if (importers == null) {
      continue;
    }
    for (Bundle bundle : importers) {
      if (bundle.getBundleId() == ref.getBundle().getBundleId()) {
        return package.getExportingBundle
      }
    }
  }
}

,我们在这里所做的就是查找具有给定包名称的所有包(可能有多个),找到注册服务的包导入,并获取导出该包的包。你或许可以让这个方法变得更好一点。

Disclaimer: I haven't tried this, but I'm reasonably sure it should do the job.

You can get the packagename from the ServiceReference's ObjectClass, so now we have that, we can find the package in the framework. Given a PackageAdmin packageAdmin, you can do something like

public Bundle getExporterOf(String package, ServiceReference ref) {

  ExportedPackage[] packages = packageAdmin.getExportedPackages(packageName);
  if (packages == null) {
    return null;
  }
  for (ExportedPackage package : packages) {
    Bundle[] importers = package.getImportingBundles()) {
    if (importers == null) {
      continue;
    }
    for (Bundle bundle : importers) {
      if (bundle.getBundleId() == ref.getBundle().getBundleId()) {
        return package.getExportingBundle
      }
    }
  }
}

What we're doing here, is find all packages with the given package name (there might be multiple), find the one that the bundle that registered the service imports, and get the bundle that exported that package. You can probably make the method a little nicer.

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