private boolean incompatibleExtender(Bundle bundle) { List<BundleWire> requiredWires = bundle.adapt(BundleWiring.class) .getRequiredWires(OSGI_EXTENDER_NS); for(BundleWire bw : requiredWires) { BundleCapability capability = bw.getCapability(); if(EntityManagerFactoryBuilder.JPA_CAPABILITY_NAME.equals( capability.getAttributes().get(OSGI_EXTENDER_NS))) { // If the persistence bundle requires a different revision for the // JPA extender then we are incompatible, otherwise we are return !capability.getRevision().equals(wiring.getRevision()); } } // If there is no requirement then we must assume that it's safe return false; }
/** * Get all the relevant packages that the EclipseLink JPA provider exports or persistence packages it uses itself. These are needed * so that the woven proxy (for runtime enhancement) can be used later on :) * * Note that differently to OpenJPA the relevant classes are actually in more than just one bundle (org.eclipse.persistence.jpa and org.eclipse.persistence.core * at the time of this writing). Hence, we have to take more than just the packages of the JPA provider bundle into account ... * * @param jpaBundle * @return */ private String[] getJPAPackages(Bundle jpaBundle) { Set<String> result = new HashSet<String>(); for (Bundle b : context.getBundles()) { BundleWiring bw = b.adapt(BundleWiring.class); if (bw == null) { continue; } boolean isJpaBundle = b.equals(jpaBundle); List<BundleWire> wires = bw.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE); for (BundleWire w : wires) { String pkgName = (String)w.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE); boolean add = isJpaBundle || pkgName.startsWith("org.eclipse.persistence"); if (add) { result.add(getPkg(b, pkgName)); } } } result.add(getPkg(context.getBundle(), "org.apache.aries.jpa.eclipselink.adapter.platform")); LOG.debug("Found JPA packages {}", result); return result.toArray(new String[0]); }
private Set<BundleWiring> getInUseBundleWirings() { Set<BundleWiring> wirings = new HashSet<>(); Collection<BundleCapability> bundles = fwkWiring.findProviders(ALL_BUNDLES_REQUIREMENT); for (BundleCapability bundleCap : bundles) { // Only pay attention to non JPMS boot modules. // NOTE this means we will not create a real JPMS Module or Layer for this bundle if (bundleCap.getAttributes().get(BOOT_JPMS_MODULE) == null) { BundleRevision revision = bundleCap.getRevision(); BundleWiring wiring = revision.getWiring(); if (wiring != null && wiring.isInUse()) { wirings.add(wiring); } if (revision.getBundle().getBundleId() == 0) { // also store the system.bundle fragments because they may have exports unknown to JPMS List<BundleWire> hostWires = wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE); for (BundleWire hostWire : hostWires) { wirings.add(hostWire.getRequirerWiring()); } } } } return wirings; }
private boolean extenderCapabilityWired(Bundle bundle) { BundleWiring wiring = bundle.adapt(BundleWiring.class); if (wiring == null) { return true; } List<BundleWire> requiredWires = wiring.getRequiredWires(EXTENDER_NAMESPACE); for (BundleWire requiredWire : requiredWires) { if (CAMEL_EXTENDER.equals(requiredWire.getCapability().getAttributes().get(EXTENDER_NAMESPACE))) { if (this.bundleId == requiredWire.getProviderWiring().getBundle().getBundleId()) { LOG.debug("Camel extender requirement of bundle {} correctly wired to this implementation", bundle.getBundleId()); return true; } else { LOG.info("Not processing bundle {} as it requires a camel extender but is not wired to the this implementation", bundle.getBundleId()); return false; } } } return true; }
private void prepareDependencies(final Bundle bundle) { final BundleWiring wiring = bundle.adapt(BundleWiring.class); final List<BundleWire> wires = wiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE); if (wires != null) { for (final BundleWire wire : wires) { try { final Bundle dependency = wire.getProviderWiring().getBundle(); if (visited.add(dependency.getSymbolicName()) && hasComponents(dependency)) { if (!live(dependency)) { dependency.start(); } if (live(dependency)) { // pseudo-event to trigger bundle activation addingBundle(dependency, null /* unused */); } } } catch (final Exception e) { log.warn("MISSING {}", wire, e); } } } }
private Collection<BundleRequirement> resolveWiredRequirements(final Bundle bundle) { BundleWiring bundleWiring = bundle.adapt(BundleWiring.class); List<BundleRequirement> result = new ArrayList<>(); List<BundleWire> extenderWires = bundleWiring.getRequiredWires(ExtenderNamespace.EXTENDER_NAMESPACE); Bundle extenderBundle = context.getBundle(); for (BundleWire bundleWire : extenderWires) { if (extenderBundle.equals(bundleWire.getProviderWiring().getBundle())) { Map<String, Object> capabilityAttributes = bundleWire.getCapability().getAttributes(); if (ECMExtenderConstants.EXTENDER_SYMBOLIC_NAME .equals(capabilityAttributes.get(ExtenderNamespace.EXTENDER_NAMESPACE))) { BundleRequirement requirement = bundleWire.getRequirement(); result.add(requirement); } } } return result; }
@Override public Object addingBundle(Bundle bundle, BundleEvent event) { // Get the wiring of the bundle. BundleWiring wiring = Helper.getWiring(bundle); // See it is wired to us as extender. List<BundleWire> requirements = wiring.getRequiredWires("osgi.extender"); Context context = null; if (requirements != null && requirements.stream().anyMatch((w) -> w.getProviderWiring().getBundle().equals(me))) { // Create the stuff. WeldContainer container = new WeldContainer(bundle); Hashtable<String, Object> dict = new Hashtable<>(); dict.put(Constants.BUNDLE_SYMBOLICNAME, bundle.getSymbolicName()); String cat = bundle.getHeaders().get(Constants.BUNDLE_CATEGORY); if (cat != null) { dict.put(Constants.BUNDLE_CATEGORY, cat); } ServiceRegistration<BeanManager> reg = bundle.getBundleContext().registerService(BeanManager.class, container.getManager(), dict); context = new Context(container, reg); } return context; }
/** * Retrieves the set of bundles from which given bundle depends. * * @param bundle * target bundle * @return set of ancestor bundles */ public static Set<Bundle> getAncestorBundles(Bundle bundle) { BundleWiring wiring = bundle.adapt(BundleWiring.class); // the set of bundles from which the bundle imports packages Set<Bundle> exporters = new HashSet<Bundle>(); if (wiring != null) { List<BundleWire> bundleWires = wiring.getRequiredWires(null); if (bundleWires != null) { for (BundleWire pkg : bundleWires) { Bundle providerBundle = pkg.getProviderWiring().getBundle(); exporters.add(providerBundle); } } } return exporters; }
private void calculateImporters() { importers.clear(); synchronized (importers) { // calculate importers Bundle bundle = m_bundleCapability.getRevision().getBundle(); if (bundle != null) { BundleWiring wiring = bundle.adapt(BundleWiring.class); if (wiring != null) { List<BundleWire> wires = wiring.getProvidedWires(PACKAGE_NAMESPACE); if (wires != null) { for (BundleWire wire : wires) { if (wire.getCapability().equals(m_bundleCapability)) { Bundle requirerBundle = wire.getRequirerWiring().getBundle(); importers.add(requirerBundle); } } } } } } }
@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(); }
private void updateContext(Bundle currentContext, String className) { Bundle contextToSet = (currentContext == null) ? bundle : currentContext; int idx = className.lastIndexOf('.'); String packageName = (idx == -1) ? "" : className.substring(0, idx); BundleWiring wiring = contextToSet.adapt(BundleWiring.class); for (BundleWire wire : wiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE)) { if (wire.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(packageName)) { contextToSet = wire.getProviderWiring().getBundle(); break; } } currentLoadingBundle.get().push(contextToSet); }
public BundleWiringLastModified(BundleWiring hostWiring) { // get the host last modified if (hostWiring.isCurrent()) { // use the current bundle id and last modified lastModifieds.put(hostWiring.getBundle().getBundleId(), hostWiring.getBundle().getLastModified()); } else { // use a unique negative id to indicate not current lastModifieds.put(nextNotCurrentID.getAndDecrement(), hostWiring.getBundle().getLastModified()); } for (BundleWire hostWire : hostWiring.getProvidedWires(HostNamespace.HOST_NAMESPACE)) { // Always use the fragment id and last modified. // It makes no difference if it is current or not because the host wiring indicates that. lastModifieds.put(hostWire.getRequirer().getBundle().getBundleId(), hostWire.getRequirer().getBundle().getLastModified()); } }
/** * Check if bundle can see the given class */ protected boolean canSee(Bundle bundle, Class<?> clazz) { if (bundle.getBundleId() == bundleId) { // Need extra handling of camel core as it does not import the api return true; } BundleCapability packageCap = packageCapabilities.get(clazz.getPackage().getName()); if (packageCap != null) { BundleWiring wiring = bundle.adapt(BundleWiring.class); List<BundleWire> imports = wiring.getRequiredWires(PACKAGE_NAMESPACE); for (BundleWire importWire : imports) { if (packageCap.equals(importWire.getCapability())) { return true; } } } // it may be running outside real OSGi container such as when unit testing with camel-test-blueprint // then we need to use a different canSee algorithm that works outside real OSGi if (bundle.getBundleId() > 0) { Bundle root = bundle.getBundleContext().getBundle(0); if (root != null && "org.apache.felix.connect".equals(root.getSymbolicName())) { return checkCompat(bundle, clazz); } } return false; }
public static String toWiringString(Bundle bundle){ String str = ""; BundleWiring bw = bundle.adapt(BundleWiring.class); if (null != bw) { for (BundleWire wire : bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE)) { String packagee = (String) wire.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE); Bundle b = wire.getProviderWiring().getBundle(); // str = str + "package: " + packagee + " bundle: " + b + "\n"; str = str + "package: " + packagee + " " + BundleDetailFragment.SCHEMA_PLUGIN + "://?id=" + b.getBundleId() + "\n"; } } return str; }
private Collection<Bundle> getDependantBundles(Bundle bundle) { BundleWiring bundleWiring = bundle.adapt(BundleWiring.class); List<BundleWire> bundleWires = bundleWiring.getRequiredWires(null); HashSet<Bundle> bundles = new HashSet<Bundle>(); for (BundleWire bundleWire : bundleWires) { bundles.add(bundleWire.getProvider().getBundle()); } return bundles; }
protected Set<Bundle> getWiredBundles(Bundle bundle) { BundleWiring wiring = bundle.adapt(BundleWiring.class); // the set of bundles from which the bundle imports packages Set<Bundle> exporters = new HashSet<Bundle>(); for (BundleWire pkg : wiring.getRequiredWires(null)) { Bundle providerBundle = pkg.getProviderWiring().getBundle(); exporters.add(providerBundle); } return exporters; }
/** * Retrieves the set of bundles dependent of given bundle. * * @param bundle * target bundle * @return set of child bundles */ public static Set<Bundle> getChildBundles(Bundle bundle) { BundleWiring wiring = bundle.adapt(BundleWiring.class); Set<Bundle> dependencies = new HashSet<Bundle>(); if (wiring != null) { for (BundleWire requiredWire : wiring.getProvidedWires(null)) { dependencies.add(requiredWire.getRequirerWiring().getBundle()); } } return dependencies; }
@Override public <A> A adaptTo(Class<A> clazz) { if (BundleWire.class.equals(clazz)) { return (A) m_wire; } else if (BundleWireResource.class.equals(clazz)) { return (A) this; } else { return null; } }
public List<BundleWire> getWires() { ArrayList<BundleWire> wires = new ArrayList<BundleWire>(); wires.addAll(m_wires); return wires; }
/** * Constructor for bundle capability resource * * @param path * @param bundleCapability */ public BundleCapabilityResource(Path path, BundleWiring hostWiring, BundleCapability bundleCapability) { super(path.addElements(uniqueCapabilityId(bundleCapability))); m_capability = bundleCapability; isPackage = m_capability.getNamespace().equals(OsgiResourceUtils.PackageNamespace.PACKAGE_NAMESPACE); List<Relation> relations = new ArrayList<Relation>(); // calculate wires coming from this capability BundleRevision revision = m_capability.getRevision(); if (revision != null) { String bundleId = Long.toString(revision.getBundle().getBundleId()); //BundleWiring wiring = revision.getWiring(); BundleWiring wiring = hostWiring; if (wiring != null) { List<BundleWire> allWires = wiring.getProvidedWires(m_capability.getNamespace()); for (BundleWire wire : allWires) { if (wire.getCapability().equals(m_capability)) { // and add a relation link m_wires.add(wire); String wireId = uniqueWireId(wire); Path wirePath = BundleResourceManager.getInstance().getPath().addElements(Long.toString(hostWiring.getBundle().getBundleId()), BundleResource.WIRES_PATH, wireId ); relations.add(new DefaultRelation(wirePath, Action.READ, wireId)); } } } if (isPackage) { // add relation to package String packageId = uniqueCapabilityId(m_capability); Path packagePath = PackageResourceManager.getInstance().getPath().addElements(packageId); relations.add(new DefaultRelation(packagePath, Action.READ, PACKAGE_RELATION)); // add relation to bundle export package header Path exportPackagePath = BundleResourceManager.getInstance().getPath() .addElements(bundleId, BundleHeadersResource.HEADERS_PATH, BundleHeadersResource.EXPORT_PACKAGE, packageId); relations.add(new DefaultRelation(exportPackagePath, Action.READ, BundleHeadersResource.EXPORT_PACKAGE)); } setRelations(relations); } }
/** * Generates a unique id for given bundle wire * Uses hashcode of the wire object * * @param wire {@code BundleWire} object * @return unique id in {@code String} */ public static String uniqueWireId(BundleWire wire) { return "wire-" + wire.hashCode(); }