/** * Finds the {@link MapReduceClassLoader} from the {@link ClassLoader} inside the given {@link Configuration}. * * @throws IllegalArgumentException if no {@link MapReduceClassLoader} can be found from the {@link Configuration}. */ public static MapReduceClassLoader getFromConfiguration(Configuration configuration) { return Delegators.getDelegate(configuration.getClassLoader(), MapReduceClassLoader.class); }
/** * Finds the {@link MapReduceClassLoader} from the {@link ClassLoader} inside the given {@link Configuration}. * * @throws IllegalArgumentException if no {@link MapReduceClassLoader} can be found from the {@link Configuration}. */ public static MapReduceClassLoader getFromConfiguration(Configuration configuration) { return Delegators.getDelegate(configuration.getClassLoader(), MapReduceClassLoader.class); }
@Test public void testWeakReferenceClassLoader() throws Exception { // Creates a jar that has Application class in it. Location jar = AppJarHelper.createDeploymentJar(new LocalLocationFactory(TMP_FOLDER.newFolder()), ClassLoaderTest.class); // Create a class loader that load from that jar. File unpackDir = TMP_FOLDER.newFolder(); BundleJarUtil.unJar(jar, unpackDir); ClassLoader cl = new DirectoryClassLoader(unpackDir, null, "lib"); // Wrap it with the WeakReference ClassLoader ClassLoader classLoader = new WeakReferenceDelegatorClassLoader(cl); // Load class from the wrapped ClassLoader, should succeed and should be loaded by the delegating ClassLoader. Class<?> cls = classLoader.loadClass(ClassLoaderTest.class.getName()); Assert.assertSame(cl, cls.getClassLoader()); Assert.assertSame(cl, Delegators.getDelegate(classLoader, ClassLoader.class)); // There is no good way to test the GC of the weak reference referent since it depends on GC. }