org.osgi.framework.Bundle.adapt()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中org.osgi.framework.Bundle.adapt()方法的一些代码示例,展示了Bundle.adapt()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bundle.adapt()方法的具体详情如下:
包路径:org.osgi.framework.Bundle
类名称:Bundle
方法名:adapt

Bundle.adapt介绍

[英]Adapt this bundle to the specified type.

Adapting this bundle to the specified type may require certain checks, including security checks, to succeed. If a check does not succeed, then this bundle cannot be adapted and null is returned.
[中]将此捆绑适应指定的类型。
使此捆绑包适应指定的类型可能需要进行某些检查,包括安全检查才能成功。如果检查未成功,则无法调整此捆绑包,并返回null。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Creates a OsgiArchiveDescriptor
 *
 * @param persistenceBundle The bundle being described as an archive
 */
@SuppressWarnings("RedundantCast")
public OsgiArchiveDescriptor(Bundle persistenceBundle) {
  this.persistenceBundle = persistenceBundle;
  bundleWiring = (BundleWiring) persistenceBundle.adapt( BundleWiring.class );
}

代码示例来源:origin: apache/activemq

/**
 * Caches the package capabilities that are needed for a set of interface classes
 *
 * @param classes interfaces we want to track
 */
private void cachePackageCapabilities(Class<?> ... classes) {
  BundleWiring ourWiring = bundleContext.getBundle().adapt(BundleWiring.class);
  Set<String> packageNames = new HashSet<String>();
  for (Class<?> clazz: classes) {
    packageNames.add(clazz.getPackage().getName());
  }
  List<BundleCapability> ourExports = ourWiring.getCapabilities(PACKAGE_NAMESPACE);
  for (BundleCapability ourExport : ourExports) {
    String ourPkgName = (String) ourExport.getAttributes().get(PACKAGE_NAMESPACE);
    if (packageNames.contains(ourPkgName)) {
      packageCapabilities.add(ourExport);
    }
  }
}

代码示例来源:origin: apache/activemq

/**
 * We consider a bundle to be a candidate for objects if it imports at least
 * one of the packages of our interfaces
 *
 * @param bundle
 * @return true if the bundle is improting.
 */
private boolean isImportingUs(Bundle bundle) {
  BundleWiring wiring = bundle.adapt(BundleWiring.class);
  List<BundleWire> imports = wiring.getRequiredWires(PACKAGE_NAMESPACE);
  for (BundleWire importWire : imports) {
    if (packageCapabilities.contains(importWire.getCapability())) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

private void loadProvider(final Bundle bundle) {
  if (bundle.getState() == Bundle.UNINSTALLED) {
    return;
  }
  try {
    checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
    checkPermission(new AdaptPermission(BundleWiring.class.getName(), bundle, AdaptPermission.ADAPT));
    final BundleContext bundleContext = bundle.getBundleContext();
    if (bundleContext == null) {
      LOGGER.debug("Bundle {} has no context (state={}), skipping loading provider", bundle.getSymbolicName(), toStateString(bundle.getState()));
    } else {
      loadProvider(bundleContext, bundle.adapt(BundleWiring.class));
    }
  } catch (final SecurityException e) {
    LOGGER.debug("Cannot access bundle [{}] contents. Ignoring.", bundle.getSymbolicName(), e);
  } catch (final Exception e) {
    LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(), e);
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Override
public void start(final BundleContext bundleContext) throws Exception {
  ProviderUtil.STARTUP_LOCK.lock();
  lockingProviderUtil = true;
  final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class);
  final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
  for (final BundleWire wire : required) {
    loadProvider(bundleContext, wire.getProviderWiring());
  }
  bundleContext.addBundleListener(this);
  final Bundle[] bundles = bundleContext.getBundles();
  for (final Bundle bundle : bundles) {
    loadProvider(bundle);
  }
  unlockIfReady();
}

代码示例来源:origin: ehcache/ehcache3

public static void testAllServicesAreAvailable() {
 Set<String> osgiAvailableClasses =
  stream(spliterator(OsgiServiceLoader.load(ServiceFactory.class).iterator(), Long.MAX_VALUE, 0), false)
   .map(f -> f.getClass().getName())
   .collect(toSet());
 Set<String> jdkAvailableClasses = of(EhcacheActivator.getCoreBundle().getBundles())
  .map(b -> b.adapt(BundleWiring.class).getClassLoader())
  .flatMap(cl ->
   stream(spliterator(ServiceLoader.load(ServiceFactory.class, cl).iterator(), Long.MAX_VALUE, 0), false)
    .map(f -> f.getClass().getName()))
  .collect(toSet());
 assertThat(osgiAvailableClasses, hasItems(jdkAvailableClasses.toArray(new String[0])));
}

代码示例来源:origin: ehcache/ehcache3

public static void testAllServicesAreAvailable() {
  Set<String> osgiAvailableClasses =
   stream(spliterator(OsgiServiceLoader.load(ServiceFactory.class).iterator(), Long.MAX_VALUE, 0), false)
    .map(f -> f.getClass().getName())
    .collect(toSet());
  Set<String> jdkAvailableClasses = of(EhcacheActivator.getCoreBundle().getBundles())
   .map(b -> b.adapt(BundleWiring.class).getClassLoader())
   .flatMap(cl ->
    stream(spliterator(ServiceLoader.load(ServiceFactory.class, cl).iterator(), Long.MAX_VALUE, 0), false)
     .map(f -> f.getClass().getName()))
   .collect(toSet());
  assertThat(osgiAvailableClasses, hasItems(jdkAvailableClasses.toArray(new String[0])));
 }
}

代码示例来源:origin: ehcache/ehcache3

public static void testAllServicesAreAvailable() {
  Set<String> osgiAvailableClasses =
   stream(spliterator(OsgiServiceLoader.load(ServiceFactory.class).iterator(), Long.MAX_VALUE, 0), false)
    .map(f -> f.getClass().getName())
    .collect(toSet());
  Set<String> jdkAvailableClasses = of(EhcacheActivator.getCoreBundle().getBundles())
   .map(b -> b.adapt(BundleWiring.class).getClassLoader())
   .flatMap(cl ->
    stream(spliterator(ServiceLoader.load(ServiceFactory.class, cl).iterator(), Long.MAX_VALUE, 0), false)
     .map(f -> f.getClass().getName()))
   .collect(toSet());
  assertThat(osgiAvailableClasses, hasItems(jdkAvailableClasses.toArray(new String[0])));
 }
}

代码示例来源:origin: hibernate/hibernate-orm

final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt( BundleWiring.class );
final Collection<String> cfgResources = bundleWiring.listResources(
    "/",

代码示例来源:origin: apache/karaf

protected Object doExecute(List<Bundle> bundles) throws Exception {
  FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
  if (bundles == null || bundles.isEmpty()) {
    bundles = null;
  }
  wiring.refreshBundles(bundles);
  return null;
}

代码示例来源:origin: apache/karaf

private void resolveAll() {
    ServiceRegistration<ResolverHookFactory> registration = context
      .registerService(ResolverHookFactory.class, (triggers) -> resolver, null);
    try {
      context.getBundle().adapt(FrameworkWiring.class)
        .resolveBundles(Arrays.asList(context.getBundles()));
    } finally {
      registration.unregister();
    }
  }
}

代码示例来源:origin: apache/karaf

protected Object doExecute(List<Bundle> bundles) throws Exception {
  FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
  wiring.resolveBundles(bundles == null || bundles.isEmpty() ? null : bundles);
  return null;
}

代码示例来源:origin: apache/karaf

private CharSequence print(Bundle bundle)
{
  // [ ID ] [STATE      ] [ SL ] symname
  int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
  return String.format("%5d|%-11s|%5d|%s (%s)", bundle.getBundleId(),
    getState(bundle), level, bundle.getSymbolicName(), bundle.getVersion());
}

代码示例来源:origin: apache/karaf

public void frameworkEvent(FrameworkEvent event) {
    if (event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) {
      int defaultStartLevel = Integer.parseInt(System.getProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL));
      int startLevel = this.bundleContext.getBundle(0).adapt(FrameworkStartLevel.class).getStartLevel();
      if (startLevel >= defaultStartLevel) {
        started.set(true);
      }
    }
  }
}

代码示例来源:origin: apache/karaf

private CharSequence print(Bundle bundle)
{
  // [ ID ] [STATE      ] [ SL ] symname
  int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
  return String.format("%5d|%-11s|%5d|%s (%s)", bundle.getBundleId(),
    getState(bundle), level, bundle.getSymbolicName(), bundle.getVersion());
}

代码示例来源:origin: apache/karaf

private List<String> getExports(Bundle bundle) throws Exception {
  List<String> exports = new ArrayList<>();
  BundleRevision rev = bundle.adapt(BundleRevision.class);
  List<BundleCapability> caps = rev.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
  for (BundleCapability cap : caps) {
    Map<String, Object> attr = cap.getAttributes();
    String packageName = (String)attr.get(BundleRevision.PACKAGE_NAMESPACE);
    exports.add(packageName);
  }
  return exports;
}

代码示例来源:origin: apache/karaf

@Override
public void refreshPackages(Collection<Bundle> bundles) throws InterruptedException {
  final CountDownLatch latch = new CountDownLatch(1);
  FrameworkWiring fw = systemBundleContext.getBundle().adapt(FrameworkWiring.class);
  fw.refreshBundles(bundles, (FrameworkListener) event -> {
    if (event.getType() == FrameworkEvent.ERROR) {
      LOGGER.error("Framework error", event.getThrowable());
    }
    latch.countDown();
  });
  latch.await();
}

代码示例来源:origin: org.eclipse/osgi

private static void setStartLevel(final int value) throws InterruptedException {
  FrameworkStartLevel fwkStartLevel = context.getBundle().adapt(FrameworkStartLevel.class);
  final Semaphore semaphore = new Semaphore(0);
  StartupEventListener listener = new StartupEventListener(semaphore, FrameworkEvent.STARTLEVEL_CHANGED);
  context.addBundleListener(listener);
  fwkStartLevel.setStartLevel(value, listener);
  updateSplash(semaphore, listener);
}

代码示例来源:origin: apache/karaf

@Override
public List<String> getImports(long bundleId) {
  Bundle bundle = bundleContext.getBundle(bundleId);
  BundleRevision rev = bundle.adapt(BundleRevision.class);
  List<BundleRequirement> reqs = rev.getDeclaredRequirements(BundleRevision.PACKAGE_NAMESPACE);
  List<String> imports = new ArrayList<>();
  for (BundleRequirement req : reqs) {
    PackageRequirement packageReq = create(req, bundle);
    imports.add(packageReq.getPackageName());
  }
  return imports;
}

代码示例来源:origin: apache/karaf

BundleWires(Bundle bundle) {
  this.bundleId = bundle.getBundleId();
  for (BundleWire wire : bundle.adapt(BundleWiring.class).getRequiredWires(null)) {
    String requirementId = getRequirementId(wire.getRequirement());
    String capabilityId = getCapabilityId(wire.getCapability());
    this.wiring.put(requirementId, capabilityId);
  }
}

相关文章