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