Search in sources :

Example 31 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class ProxyWeavingHook method weave.

public final void weave(WovenClass wovenClass) {
    BundleWiring bw = wovenClass.getBundleWiring();
    if (bw != null) {
        Bundle b = bw.getBundle();
        if (b.getBundleId() == 0 || b.getSymbolicName().startsWith("org.apache.aries.proxy") || b.getSymbolicName().startsWith("org.apache.aries.util")) {
            return;
        }
    }
    if (!isEnabled(wovenClass.getClassName()) || isDisabled(wovenClass.getClassName())) {
        return;
    }
    if (shouldWeave(wovenClass)) {
        byte[] bytes = null;
        try {
            bytes = WovenProxyGenerator.getWovenProxy(wovenClass.getBytes(), wovenClass.getBundleWiring().getClassLoader());
        } catch (Exception e) {
            if (e instanceof RuntimeException && e.getCause() instanceof UnableToProxyException) {
                //This is a weaving failure that should be logged, but the class
                //can still be loaded
                LOGGER.trace(String.format("The class %s cannot be woven, it may not be possible for the runtime to proxy this class.", wovenClass.getClassName()), e);
            } else {
                throw weavingException(wovenClass, e);
            }
        }
        if (bytes != null && bytes.length != 0) {
            wovenClass.setBytes(bytes);
            List<String> imports = wovenClass.getDynamicImports();
            imports.add(IMPORT_A);
            imports.add(IMPORT_B);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) WeavingException(org.osgi.framework.hooks.weaving.WeavingException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 32 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class InterfaceProxyingTest method testNoStaleProxiesForRefreshedBundle.

@Test
public void testNoStaleProxiesForRefreshedBundle() throws Exception {
    Bundle bundle = mock(Bundle.class);
    TestClassLoader loader = new TestClassLoader();
    when(bundle.getLastModified()).thenReturn(10l);
    BundleWiring wiring = AbstractProxyTest.getWiring(loader);
    when(bundle.adapt(BundleWiring.class)).thenReturn(wiring);
    Class<?> clazz = loader.loadClass("org.apache.aries.blueprint.proxy.TestInterface");
    Object proxy = InterfaceProxyGenerator.getProxyInstance(bundle, null, Arrays.<Class<?>>asList(clazz), constantly(null), null);
    assertTrue(clazz.isInstance(proxy));
    ClassLoader parent1 = proxy.getClass().getClassLoader().getParent();
    /* Now again but with a changed classloader as if the bundle had refreshed */
    TestClassLoader loaderToo = new TestClassLoader();
    when(bundle.getLastModified()).thenReturn(20l);
    // let's change the returned revision
    BundleWiring wiring2 = AbstractProxyTest.getWiring(loaderToo);
    when(bundle.adapt(BundleWiring.class)).thenReturn(wiring2);
    Class<?> clazzToo = loaderToo.loadClass("org.apache.aries.blueprint.proxy.TestInterface");
    Object proxyToo = InterfaceProxyGenerator.getProxyInstance(bundle, null, Arrays.<Class<?>>asList(clazzToo), constantly(null), null);
    assertTrue(clazzToo.isInstance(proxyToo));
    ClassLoader parent2 = proxyToo.getClass().getClassLoader().getParent();
    // 
    assertTrue("parents should be different, as the are the classloaders of different bundle revisions", parent1 != parent2);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Test(org.junit.Test)

Example 33 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class WovenClassListener method modified.

@Override
public void modified(WovenClass wovenClass) {
    if (wovenClass.getState() != WovenClass.TRANSFORMED) {
        // the defined state is reached.
        return;
    }
    List<String> dynamicImports = wovenClass.getDynamicImports();
    if (dynamicImports.isEmpty()) {
        // Nothing to do if there are no dynamic imports.
        return;
    }
    BundleWiring wiring = wovenClass.getBundleWiring();
    Bundle bundle = wiring.getBundle();
    BundleRevision revision = bundle.adapt(BundleRevision.class);
    BundleConstituent constituent = new BundleConstituent(null, revision);
    Collection<BasicSubsystem> basicSubsystems = subsystems.getSubsystemsByConstituent(constituent);
    BasicSubsystem subsystem = basicSubsystems.iterator().next();
    // Find the scoped subsystem in the region.
    subsystem = scopedSubsystem(subsystem);
    if (subsystem.getSubsystemId() == 0) {
        // The root subsystem needs no sharing policy.
        return;
    }
    if (EnumSet.of(Subsystem.State.INSTALLING, Subsystem.State.INSTALLED).contains(subsystem.getState())) {
        // The scoped subsystem must be resolved before adding dynamic 
        // package imports to the sharing policy in order to minimize 
        // unpredictable wirings. Resolving the scoped subsystem will also
        // resolve all of the unscoped subsystems in the region.
        AccessController.doPrivileged(new StartAction(subsystem, subsystem, subsystem, Restriction.RESOLVE_ONLY));
    }
    Bundle systemBundle = context.getBundle(org.osgi.framework.Constants.SYSTEM_BUNDLE_LOCATION);
    FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
    // The following map tracks all of the necessary updates as each dynamic
    // import is processed. The key is the tail region of the connection 
    // whose filter needs updating.
    Map<Region, RegionUpdaterInfo> updates = new HashMap<Region, RegionUpdaterInfo>();
    for (String dynamicImport : dynamicImports) {
        // For each dynamic import, collect the necessary update information.
        DynamicImportPackageHeader header = new DynamicImportPackageHeader(dynamicImport);
        List<DynamicImportPackageRequirement> requirements = header.toRequirements(revision);
        for (DynamicImportPackageRequirement requirement : requirements) {
            Collection<BundleCapability> providers = frameworkWiring.findProviders(requirement);
            if (providers.isEmpty()) {
                // import, no updates are made.
                continue;
            }
            addSharingPolicyUpdates(requirement, subsystem, providers, updates);
        }
    }
    // Now update each sharing policy only once.
    for (RegionUpdaterInfo update : updates.values()) {
        RegionUpdater updater = new RegionUpdater(update.tail(), update.head());
        try {
            updater.addRequirements(update.requirements());
        } catch (IllegalStateException e) {
        // Something outside of the subsystems implementation has
        // deleted the edge between the parent and child subsystems.
        // Assume the dynamic import sharing policy is being handled
        // elsewhere. See ARIES-1429.
        } catch (Exception e) {
            throw new SubsystemException(e);
        }
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) DynamicImportPackageHeader(org.apache.aries.subsystem.core.archive.DynamicImportPackageHeader) BundleConstituent(org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent) DynamicImportPackageRequirement(org.apache.aries.subsystem.core.archive.DynamicImportPackageRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) FilteredRegion(org.eclipse.equinox.region.RegionDigraph.FilteredRegion) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 34 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project sling by apache.

the class OsgiInstallerTestBase method isPackageExported.

protected boolean isPackageExported(Bundle b, String packageName) {
    final BundleWiring wiring = b.adapt(BundleWiring.class);
    assertNotNull("Expecting non-null BundleWiring for bundle " + b, wiring);
    for (BundleCapability c : wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
        if (packageName.equals(c.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
            return true;
        }
    }
    return false;
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 35 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project bndtools by bndtools.

the class Auxiliary method weave.

@Override
public void weave(WovenClass wovenClass) {
    if (delta == null || delta.isEmpty())
        return;
    BundleWiring wiring = wovenClass.getBundleWiring();
    if (wiring == null)
        return;
    if (wiring.getBundle() != bndlib)
        return;
    List<String> extra;
    synchronized (this) {
        extra = delta;
        delta = null;
    }
    if (extra != null)
        wovenClass.getDynamicImports().addAll(extra);
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)82 Bundle (org.osgi.framework.Bundle)48 BundleWire (org.osgi.framework.wiring.BundleWire)27 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)14 BundleCapability (org.osgi.framework.wiring.BundleCapability)13 BundleRevision (org.osgi.framework.wiring.BundleRevision)13 Hashtable (java.util.Hashtable)12 Dictionary (java.util.Dictionary)10 List (java.util.List)10 HashMap (java.util.HashMap)9 URL (java.net.URL)8 BundleContext (org.osgi.framework.BundleContext)8 LinkedHashMap (java.util.LinkedHashMap)6 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6 ServiceRegistration (org.osgi.framework.ServiceRegistration)6 Version (org.osgi.framework.Version)6 File (java.io.File)5