Search in sources :

Example 21 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 22 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 23 with BundleWiring

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

the class XAJPAEMFLocator method setupTransactionManager.

private void setupTransactionManager(BundleContext context, Map<String, Object> props, Map<String, Object> providerProps, ThreadLocal<TransactionControl> t, ServiceReference<EntityManagerFactoryBuilder> reference) {
    String provider = (String) reference.getProperty(JPA_UNIT_PROVIDER);
    ServiceReference<PersistenceProvider> providerRef = getPersistenceProvider(provider, context);
    if (providerRef == null) {
        // TODO log a warning and give up
        return;
    }
    Bundle providerBundle = providerRef.getBundle();
    Bundle txControlProviderBundle = context.getBundle();
    try {
        if ("org.hibernate.jpa.HibernatePersistenceProvider".equals(provider)) {
            try {
                providerBundle.loadClass("org.hibernate.resource.transaction.TransactionCoordinatorBuilder");
            } catch (Exception e) {
                BundleWiring wiring = providerBundle.adapt(BundleWiring.class);
                providerBundle = wiring.getRequiredWires("osgi.wiring.package").stream().filter(bw -> "org.hibernate".equals(bw.getCapability().getAttributes().get("osgi.wiring.package"))).map(BundleWire::getProviderWiring).map(BundleWiring::getBundle).findFirst().get();
            }
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.hibernate.impl.HibernateTxControlPlatform");
            Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
            props.put("hibernate.transaction.coordinator_class", plugin);
        } else if ("org.apache.openjpa.persistence.PersistenceProviderImpl".equals(provider)) {
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.openjpa.impl.OpenJPATxControlPlatform");
            Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
            props.put("openjpa.ManagedRuntime", plugin);
        } else if ("org.eclipse.persistence.jpa.PersistenceProvider".equals(provider)) {
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.eclipse.impl.EclipseTxControlPlatform");
            pluginClazz.getMethod("setTransactionControl", ThreadLocal.class).invoke(null, t);
            props.put("eclipselink.target-server", pluginClazz.getName());
            props.put("org.apache.aries.jpa.eclipselink.plugin.types", pluginClazz);
            // transactions without blowing up.
            if (!props.containsKey("eclipselink.jdbc.sequence-connection-pool")) {
                props.put("eclipselink.jdbc.sequence-connection-pool", "true");
            }
        } else {
            // TODO log a warning and give up
            return;
        }
    } catch (Exception e) {
        //TODO log a warning and give up
        e.printStackTrace();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) PersistenceProvider(javax.persistence.spi.PersistenceProvider) BundleWiring(org.osgi.framework.wiring.BundleWiring) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 24 with BundleWiring

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

the class ShowBundleTree method createNodeForImport.

/*
     * Create a child node for a given import (by finding a matching export in the currently installed bundles)
     */
private void createNodeForImport(Node<Bundle> node, Bundle bundle, Clause i) {
    VersionRange range = VersionRange.parseVersionRange(i.getAttribute(Constants.VERSION_ATTRIBUTE));
    boolean foundMatch = false;
    for (Bundle b : bundleContext.getBundles()) {
        BundleWiring wiring = b.adapt(BundleWiring.class);
        if (wiring != null) {
            List<BundleCapability> caps = wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            if (caps != null) {
                for (BundleCapability cap : caps) {
                    String n = getAttribute(cap, BundleRevision.PACKAGE_NAMESPACE);
                    String v = getAttribute(cap, Constants.VERSION_ATTRIBUTE);
                    if (i.getName().equals(n) && range.contains(VersionTable.getVersion(v))) {
                        boolean existing = tree.flatten().contains(b);
                        System.out.printf("- import %s: resolved using %s%n", i, b);
                        foundMatch = true;
                        if (!node.hasChild(b)) {
                            Node<Bundle> child = node.addChild(b);
                            if (!existing) {
                                createNode(child);
                            }
                        }
                    }
                }
            }
        }
    }
    if (!foundMatch) {
        System.out.printf("- import %s: WARNING - unable to find matching export%n", i);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) VersionRange(org.apache.felix.utils.version.VersionRange) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 25 with BundleWiring

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

the class ShowBundleTree method printDuplicatePackages.

/*
     * Check for bundles in the tree exporting the same package
     * as a possible cause for 'Unresolved constraint...' on a uses-conflict
     */
private void printDuplicatePackages(Tree<Bundle> tree) {
    Set<Bundle> bundles = tree.flatten();
    Map<String, Set<Bundle>> exports = new HashMap<>();
    for (Bundle bundle : bundles) {
        for (BundleRevision revision : bundle.adapt(BundleRevisions.class).getRevisions()) {
            BundleWiring wiring = revision.getWiring();
            if (wiring != null) {
                List<BundleWire> wires = wiring.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE);
                if (wires != null) {
                    for (BundleWire wire : wires) {
                        String name = wire.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).toString();
                        exports.computeIfAbsent(name, k -> new HashSet<>()).add(bundle);
                    }
                }
            }
        }
    }
    for (String pkg : exports.keySet()) {
        if (exports.get(pkg).size() > 1) {
            System.out.printf("%n");
            System.out.printf("WARNING: multiple bundles are exporting package %s%n", pkg);
            for (Bundle bundle : exports.get(pkg)) {
                System.out.printf("- %s%n", bundle);
            }
        }
    }
}
Also used : Constants(org.osgi.framework.Constants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Parser(org.apache.felix.utils.manifest.Parser) Command(org.apache.karaf.shell.api.action.Command) HashSet(java.util.HashSet) BundleCapability(org.osgi.framework.wiring.BundleCapability) Clause(org.apache.felix.utils.manifest.Clause) Map(java.util.Map) Node(org.apache.karaf.bundle.command.bundletree.Node) VersionTable(org.apache.felix.utils.version.VersionTable) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWiring(org.osgi.framework.wiring.BundleWiring) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) VersionRange(org.apache.felix.utils.version.VersionRange) String.format(java.lang.String.format) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) List(java.util.List) Service(org.apache.karaf.shell.api.action.lifecycle.Service) BundleWire(org.osgi.framework.wiring.BundleWire) Option(org.apache.karaf.shell.api.action.Option) Tree(org.apache.karaf.bundle.command.bundletree.Tree) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) HashSet(java.util.HashSet)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)60 Bundle (org.osgi.framework.Bundle)38 BundleWire (org.osgi.framework.wiring.BundleWire)22 Test (org.junit.Test)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)10 ArrayList (java.util.ArrayList)9 BundleCapability (org.osgi.framework.wiring.BundleCapability)9 Hashtable (java.util.Hashtable)8 BundleContext (org.osgi.framework.BundleContext)7 Dictionary (java.util.Dictionary)6 HashMap (java.util.HashMap)6 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 AsmProxyManager (org.apache.aries.proxy.impl.AsmProxyManager)5 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)5 ServiceReference (org.osgi.framework.ServiceReference)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 Version (org.osgi.framework.Version)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 HashSet (java.util.HashSet)4