use of org.osgi.framework.wiring.BundleWiring in project logging-log4j2 by apache.
the class ResolverUtil method loadImplementationsInBundle.
private void loadImplementationsInBundle(final Test test, final String packageName) {
final BundleWiring wiring = FrameworkUtil.getBundle(ResolverUtil.class).adapt(BundleWiring.class);
final Collection<String> list = wiring.listResources(packageName, "*.class", BundleWiring.LISTRESOURCES_RECURSE);
for (final String name : list) {
addIfMatching(test, name);
}
}
use of org.osgi.framework.wiring.BundleWiring in project aries by apache.
the class WovenProxyGeneratorTest method testWovenClassPlusInterfaces.
/**
* This test checks that we can add interfaces to classes that don't implement
* them using dynamic subclassing. This is a little odd, but it came for
* free with support for proxying abstract classes!
* @throws Exception
*/
@Test
public void testWovenClassPlusInterfaces() throws Exception {
Bundle b = mock(Bundle.class);
BundleWiring wiring = getWiring(weavingLoader);
when(b.adapt(BundleWiring.class)).thenReturn(wiring);
Object toCall = new AsmProxyManager().createDelegatingProxy(b, Arrays.asList(getProxyClass(ProxyTestClassAbstract.class), Callable.class), new Callable() {
public Object call() throws Exception {
return weavingLoader.loadClass(ProxyTestClassChildOfAbstract.class.getName()).newInstance();
}
}, null);
// Should proxy the abstract method on the class
Method m = getProxyClass(ProxyTestClassAbstract.class).getMethod("getMessage");
assertEquals("Working", m.invoke(toCall));
// Should be a callable too!
assertEquals("Callable Works too!", ((Callable) toCall).call());
}
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 BundleWiringStateMBeanTest method testRevisionsWiring.
@Test
public void testRevisionsWiring() throws Exception {
TabularData jmxWiringTable = brsMBean.getRevisionsWiring(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
Assert.assertEquals(1, jmxWiringTable.size());
CompositeData jmxWiring = (CompositeData) jmxWiringTable.values().iterator().next();
Assert.assertEquals(BundleWiringStateMBean.BUNDLE_WIRING_TYPE, jmxWiring.getCompositeType());
Assert.assertEquals(bundleA.getBundleId(), jmxWiring.get(BundleWiringStateMBean.BUNDLE_ID));
BundleWiring bw = (BundleWiring) bundleA.adapt(BundleWiring.class);
assertBundleWiring(bw, jmxWiring);
}
Aggregations