@Test public void testCleanupOldJars() throws Exception { String className = "TestCleanupOldJars"; String folder = TEST_UTIL.getDataTestDir().toString(); File jarFile = ClassLoaderTestHelper.buildJar( folder, className, null, ClassLoaderTestHelper.localDirPath(conf)); File tmpJarFile = new File(jarFile.getParent(), "/tmp/" + className + ".test.jar"); if (tmpJarFile.exists()) tmpJarFile.delete(); assertFalse("tmp jar file should not exist", tmpJarFile.exists()); IOUtils.copyBytes(new FileInputStream(jarFile), new FileOutputStream(tmpJarFile), conf, true); assertTrue("tmp jar file should be created", tmpJarFile.exists()); Path path = new Path(jarFile.getAbsolutePath()); ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader(); CoprocessorClassLoader.parentDirLockSet.clear(); ClassLoader classLoader = CoprocessorClassLoader.getClassLoader(path, parent, "111", conf); assertNotNull("Classloader should be created", classLoader); assertFalse("tmp jar file should be removed", tmpJarFile.exists()); }
public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException { CoprocessorClassLoader cl = getIfCached(path); String pathStr = path.toString(); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } if (!pathStr.endsWith(".jar")) { throw new IOException(pathStr + ": not a jar file?"); } Lock lock = locker.acquireLock(pathStr); try { cl = getIfCached(path); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } cl = AccessController.doPrivileged( new PrivilegedAction<CoprocessorClassLoader>() { @Override public CoprocessorClassLoader run() { return new CoprocessorClassLoader(parent); } }); cl.init(path, pathPrefix, conf); CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl); if (prev != null) { LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" +" is already cached for " + pathStr); cl = prev; } return cl; } finally { lock.unlock(); } }
CoprocessorClassLoader extends ClassLoaderBase { public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException { CoprocessorClassLoader cl = getIfCached(path); String pathStr = path.toString(); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } if (!pathStr.endsWith(".jar")) { throw new IOException(pathStr + ": not a jar file?"); } Lock lock = locker.acquireLock(pathStr); try { cl = getIfCached(path); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } cl = AccessController.doPrivileged( new PrivilegedAction<CoprocessorClassLoader>() { @Override public CoprocessorClassLoader run() { return new CoprocessorClassLoader(parent); } }); cl.init(path, pathPrefix, conf); CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl); if (prev != null) { LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" +" is already cached for " + pathStr); cl = prev; } return cl; } finally { lock.unlock(); } } }
CoprocessorClassLoader extends ClassLoaderBase { public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException { CoprocessorClassLoader cl = getIfCached(path); String pathStr = path.toString(); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } if (!pathStr.endsWith(".jar")) { throw new IOException(pathStr + ": not a jar file?"); } Lock lock = locker.acquireLock(pathStr); try { cl = getIfCached(path); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } cl = AccessController.doPrivileged( new PrivilegedAction<CoprocessorClassLoader>() { @Override public CoprocessorClassLoader run() { return new CoprocessorClassLoader(parent); } }); cl.init(path, pathPrefix, conf); CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl); if (prev != null) { LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" +" is already cached for " + pathStr); cl = prev; } return cl; } finally { lock.unlock(); } } private  CoprocessorClassLoader(ClassLoader parent); }
CoprocessorClassLoader extends ClassLoaderBase { public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException { CoprocessorClassLoader cl = getIfCached(path); String pathStr = path.toString(); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } if (!pathStr.endsWith(".jar")) { throw new IOException(pathStr + ": not a jar file?"); } Lock lock = locker.acquireLock(pathStr); try { cl = getIfCached(path); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } cl = AccessController.doPrivileged( new PrivilegedAction<CoprocessorClassLoader>() { @Override public CoprocessorClassLoader run() { return new CoprocessorClassLoader(parent); } }); cl.init(path, pathPrefix, conf); CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl); if (prev != null) { LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" +" is already cached for " + pathStr); cl = prev; } return cl; } finally { lock.unlock(); } } private  CoprocessorClassLoader(ClassLoader parent); static CoprocessorClassLoader getIfCached(final Path path); static Collection<? extends ClassLoader> getAllCached(); static void clearCache(); static CoprocessorClassLoader getClassLoader(final Path path,
      final ClassLoader parent, final String pathPrefix,
      final Configuration conf); @Override Class<?> loadClass(String name); @Override URL getResource(String name); }
CoprocessorClassLoader extends ClassLoaderBase { public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException { CoprocessorClassLoader cl = getIfCached(path); String pathStr = path.toString(); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } if (!pathStr.endsWith(".jar")) { throw new IOException(pathStr + ": not a jar file?"); } Lock lock = locker.acquireLock(pathStr); try { cl = getIfCached(path); if (cl != null) { LOG.debug("Found classloader "+ cl + " for "+ pathStr); return cl; } cl = AccessController.doPrivileged( new PrivilegedAction<CoprocessorClassLoader>() { @Override public CoprocessorClassLoader run() { return new CoprocessorClassLoader(parent); } }); cl.init(path, pathPrefix, conf); CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl); if (prev != null) { LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" +" is already cached for " + pathStr); cl = prev; } return cl; } finally { lock.unlock(); } } private  CoprocessorClassLoader(ClassLoader parent); static CoprocessorClassLoader getIfCached(final Path path); static Collection<? extends ClassLoader> getAllCached(); static void clearCache(); static CoprocessorClassLoader getClassLoader(final Path path,
      final ClassLoader parent, final String pathPrefix,
      final Configuration conf); @Override Class<?> loadClass(String name); @Override URL getResource(String name);  }