Util { public static void fixContextClassloader(String cls, String method, Class<?> clsArg, ClassLoader bundleLoader) { BundleReference br = getBundleReference(bundleLoader); if (br == null) { return; } final ClassLoader cl = findContextClassloader(br.getBundle(), cls, method, clsArg); if (cl != null) { BaseActivator.activator.log(Level.FINE, "Temporarily setting Thread Context Classloader to: " + cl); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { Thread.currentThread().setContextClassLoader(cl); return null; } }); } else { BaseActivator.activator.log(Level.FINE, "No classloader found for " + cls + ":" + method + "(" + clsArg + ")"); } }  static void storeContextClassloader(); static void restoreContextClassloader(); static ServiceLoader<S> serviceLoaderLoad(Class<S> service, Class<C> caller); static ServiceLoader<S> serviceLoaderLoad(
        Class<S> service, ClassLoader specifiedClassLoader, Class<C> caller); static void fixContextClassloader(String cls, String method, Class<?> clsArg, ClassLoader bundleLoader);  }
@Test public void testFixContextClassLoaderSimpleViaEmbeddedJar() throws Exception { BaseActivator activator = new BaseActivator() { public void start(BundleContext context) throws Exception { } }; BaseActivator.activator = activator; URL url = getClass().getResource("/embedded3.jar"); assertNotNull("precondition", url); Bundle providerBundle = EasyMock.createMock(Bundle.class); final ClassLoader providerCL = new TestBundleClassLoader(new URL [] {url}, getClass().getClassLoader(), providerBundle); EasyMock.expect(providerBundle.getBundleContext()).andThrow(new IllegalStateException("Disable getBundleClassLoaderViaAdapt")); EasyMock.expect(providerBundle.getBundleId()).andReturn(42L).anyTimes(); EasyMock.expect(providerBundle.getEntryPaths((String) EasyMock.anyObject())).andReturn(null).anyTimes(); Dictionary<String, String> providerHeaders = new Hashtable<String, String>(); providerHeaders.put(Constants.BUNDLE_CLASSPATH, ".,embedded3.jar"); EasyMock.expect(providerBundle.getHeaders()).andReturn(providerHeaders).anyTimes(); EasyMock.expect(providerBundle.getResource("embedded3.jar")).andReturn(url).anyTimes(); providerBundle.loadClass((String) EasyMock.anyObject()); EasyMock.expectLastCall().andAnswer(new IAnswer<Class<?>>() { @Override public Class<?> answer() throws Throwable { return providerCL.loadClass((String) EasyMock.getCurrentArguments()[0]); } }).anyTimes(); EasyMock.replay(providerBundle); activator.registerProviderBundle(MySPI.class.getName(), providerBundle, new HashMap<String, Object>()); Bundle clientBundle = EasyMock.createMock(Bundle.class); EasyMock.replay(clientBundle); ClassLoader clientCL = new TestBundleClassLoader(new URL [] {}, getClass().getClassLoader(), clientBundle); Thread.currentThread().setContextClassLoader(null); Util.fixContextClassloader(ServiceLoader.class.getName(), "load", MySPI.class, clientCL); assertSame(providerCL, Thread.currentThread().getContextClassLoader()); }
@Test public void testNotInitialized() throws Exception { BaseActivator.activator = null; URL url = getClass().getResource("/embedded3.jar"); assertNotNull("precondition", url); Bundle providerBundle = EasyMock.createMock(Bundle.class); final ClassLoader providerCL = new TestBundleClassLoader(new URL [] {url}, getClass().getClassLoader(), providerBundle); EasyMock.expect(providerBundle.getBundleId()).andReturn(42L).anyTimes(); EasyMock.expect(providerBundle.getEntryPaths((String) EasyMock.anyObject())).andReturn(null).anyTimes(); Dictionary<String, String> providerHeaders = new Hashtable<String, String>(); providerHeaders.put(Constants.BUNDLE_CLASSPATH, ".,embedded3.jar"); EasyMock.expect(providerBundle.getHeaders()).andReturn(providerHeaders).anyTimes(); EasyMock.expect(providerBundle.getResource("embedded3.jar")).andReturn(url).anyTimes(); providerBundle.loadClass((String) EasyMock.anyObject()); EasyMock.expectLastCall().andAnswer(new IAnswer<Class<?>>() { @Override public Class<?> answer() throws Throwable { return providerCL.loadClass((String) EasyMock.getCurrentArguments()[0]); } }).anyTimes(); EasyMock.replay(providerBundle); Bundle clientBundle = EasyMock.createMock(Bundle.class); EasyMock.replay(clientBundle); ClassLoader clientCL = new TestBundleClassLoader(new URL [] {}, getClass().getClassLoader(), clientBundle); Thread.currentThread().setContextClassLoader(null); Util.fixContextClassloader(ServiceLoader.class.getName(), "load", MySPI.class, clientCL); assertSame("The system is not yet initialized, so the TCCL should not be set", null, Thread.currentThread().getContextClassLoader()); }