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);
}
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);
}
}
}
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();
}
}
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);
}
}
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);
}
}
}
}
Aggregations