/** * 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 ); }
/** * 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); } } }
/** * 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; }
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); } }
@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(); }
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]))); }
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]))); } }
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]))); } }
final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt( BundleWiring.class ); final Collection<String> cfgResources = bundleWiring.listResources( "/",
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; }
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(); } } }
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; }
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()); }
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); } } } }
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()); }
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; }
@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(); }
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); }
@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; }
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); } }