@Test public void testExtendClassLoaderChain() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { ComponentContext ctx = createMock(ComponentContext.class); BundleContext bundleContext = createMock(BundleContext.class); Bundle apiBundle = createMock(Bundle.class); Bundle implBundle = createMock(Bundle.class); Bundle webBundle = createMock(Bundle.class); BundleRevision bundleRevision = createMock(BundleRevision.class); BundleWiring bundleWiring = createMock(BundleWiring.class); Dictionary<String, String> apiHeaders = new Hashtable<String, String>(); apiHeaders.put(WebAppBundleTrackerCustomizer.HEADER_EXPOSED_CONTENT_TYPE, WebAppBundleTrackerCustomizer.HEADER_EXPOSED_CONTENT_TYPE_API_VALUE); Dictionary<String, String> implHeaders = new Hashtable<String, String>(); implHeaders.put(WebAppBundleTrackerCustomizer.HEADER_EXPOSED_CONTENT_TYPE, WebAppBundleTrackerCustomizer.HEADER_EXPOSED_CONTENT_TYPE_IMPL_VALUE); expect(apiBundle.getHeaders()).andReturn(apiHeaders).anyTimes(); expect(implBundle.getHeaders()).andReturn(implHeaders).anyTimes(); expect(apiBundle.getState()).andReturn(Bundle.ACTIVE); expect(implBundle.getState()).andReturn(Bundle.ACTIVE); expect(apiBundle.getSymbolicName()).andReturn("api").anyTimes(); expect(implBundle.getSymbolicName()).andReturn("impl").anyTimes(); expect(ctx.getBundleContext()).andReturn(bundleContext); expect(bundleContext.getBundles()).andReturn(new Bundle[] { apiBundle, implBundle }); bundleContext.addBundleListener(isA(SynchronousBundleListener.class)); expectLastCall(); bundleContext.removeBundleListener(isA(SynchronousBundleListener.class)); expectLastCall(); expect(webBundle.adapt(BundleRevision.class)).andReturn(bundleRevision); expect(bundleRevision.getWiring()).andReturn(bundleWiring); expect(bundleWiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE)).andReturn(new ArrayList<BundleWire>()); expect(bundleWiring.getRequiredWires(BundleRevision.BUNDLE_NAMESPACE)).andReturn(new ArrayList<BundleWire>()); replay(ctx, bundleContext, apiBundle, implBundle, webBundle, bundleRevision, bundleWiring); Field field = PluggableDelegatingClassLoaderDelegateHook.getInstance().getClass().getDeclaredField("delegates"); field.setAccessible(true); WebAppBundleClassloaderCustomizer webAppBundleClassloaderCustomizer = createWebAppBundleClassloaderCustomizer(); Field field2 = webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook().getClass().getDeclaredField("webAppBundles"); field2.setAccessible(true); webAppBundleClassloaderCustomizer.activate(ctx); assertTrue(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook().getApiBundles().contains(apiBundle)); assertTrue(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook().getImplBundles().contains(implBundle)); assertTrue(((List<ClassLoaderDelegateHook>) field.get(PluggableDelegatingClassLoaderDelegateHook.getInstance())).contains(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook())); assertTrue(webAppBundleClassloaderCustomizer.extendClassLoaderChain(webBundle).length == 0); assertTrue(webAppBundleClassloaderCustomizer.getWebAppBundleTrackerCustomizer().getExposeAdditionalApiBundles().isEmpty()); assertTrue(((Map<Bundle, Set<String>>) field2.get(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook())).containsKey(webBundle)); webAppBundleClassloaderCustomizer.deactivate(ctx); assertTrue(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook().getApiBundles().isEmpty()); assertTrue(webAppBundleClassloaderCustomizer.getWebAppBundleClassLoaderDelegateHook().getImplBundles().isEmpty()); assertTrue(((List<ClassLoaderDelegateHook>) field.get(PluggableDelegatingClassLoaderDelegateHook.getInstance())).isEmpty()); verify(ctx, bundleContext, apiBundle, implBundle, webBundle, bundleRevision, bundleWiring); }
@Override public ClassLoader[] extendClassLoaderChain(Bundle bundle) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extending web application bundle " + bundle); } this.webAppBundleCustomizer.processAdditionalAPIBundles(this.bundleTracker.getBundles()); this.wabClassLoaderDelegateHook.addWebAppBundle(bundle); return AccessController.doPrivileged(new PrivilegedAction<ClassLoader[]>() { @Override public ClassLoader[] run() { return WebAppBundleClassloaderCustomizer.this.wabClassLoaderDelegateHook.getImplBundlesClassloaders(); } }); }
WebAppBundleClassloaderCustomizer implements ClassLoaderCustomizer, JarScannerCustomizer { @Override public ClassLoader[] extendClassLoaderChain(Bundle bundle) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extending web application bundle " + bundle); } this.webAppBundleCustomizer.processAdditionalAPIBundles(this.bundleTracker.getBundles()); this.wabClassLoaderDelegateHook.addWebAppBundle(bundle); return AccessController.doPrivileged(new PrivilegedAction<ClassLoader[]>() { @Override public ClassLoader[] run() { return WebAppBundleClassloaderCustomizer.this.wabClassLoaderDelegateHook.getImplBundlesClassloaders(); } }); } }
WebAppBundleClassloaderCustomizer implements ClassLoaderCustomizer, JarScannerCustomizer { @Override public ClassLoader[] extendClassLoaderChain(Bundle bundle) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extending web application bundle " + bundle); } this.webAppBundleCustomizer.processAdditionalAPIBundles(this.bundleTracker.getBundles()); this.wabClassLoaderDelegateHook.addWebAppBundle(bundle); return AccessController.doPrivileged(new PrivilegedAction<ClassLoader[]>() { @Override public ClassLoader[] run() { return WebAppBundleClassloaderCustomizer.this.wabClassLoaderDelegateHook.getImplBundlesClassloaders(); } }); }  }
WebAppBundleClassloaderCustomizer implements ClassLoaderCustomizer, JarScannerCustomizer { @Override public ClassLoader[] extendClassLoaderChain(Bundle bundle) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extending web application bundle " + bundle); } this.webAppBundleCustomizer.processAdditionalAPIBundles(this.bundleTracker.getBundles()); this.wabClassLoaderDelegateHook.addWebAppBundle(bundle); return AccessController.doPrivileged(new PrivilegedAction<ClassLoader[]>() { @Override public ClassLoader[] run() { return WebAppBundleClassloaderCustomizer.this.wabClassLoaderDelegateHook.getImplBundlesClassloaders(); } }); }  @Override void addClassFileTransformer(ClassFileTransformer cft, Bundle bundle); @Override ClassLoader createThrowawayClassLoader(Bundle bundle); @Override ClassLoader[] extendClassLoaderChain(Bundle bundle); @Override JarScanner[] extendJarScannerChain(Bundle arg0); }
WebAppBundleClassloaderCustomizer implements ClassLoaderCustomizer, JarScannerCustomizer { @Override public ClassLoader[] extendClassLoaderChain(Bundle bundle) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extending web application bundle " + bundle); } this.webAppBundleCustomizer.processAdditionalAPIBundles(this.bundleTracker.getBundles()); this.wabClassLoaderDelegateHook.addWebAppBundle(bundle); return AccessController.doPrivileged(new PrivilegedAction<ClassLoader[]>() { @Override public ClassLoader[] run() { return WebAppBundleClassloaderCustomizer.this.wabClassLoaderDelegateHook.getImplBundlesClassloaders(); } }); }  @Override void addClassFileTransformer(ClassFileTransformer cft, Bundle bundle); @Override ClassLoader createThrowawayClassLoader(Bundle bundle); @Override ClassLoader[] extendClassLoaderChain(Bundle bundle); @Override JarScanner[] extendJarScannerChain(Bundle arg0);  }