/** * Populates the list of {@link URL} that this ClassLoader uses, including all URLs used by the parent of the * given ClassLoader. This is the same as calling {@link #getClassLoaderURLs(ClassLoader, boolean, Collection)} with * {@code childFirst} set to {@code false}. * * @param classLoader the {@link ClassLoader} for searching urls * @param urls a {@link Collection} for storing the {@link URL}s * @return the same {@link Collection} passed from the parameter */ public static <T extends Collection<? super URL>> T getClassLoaderURLs(ClassLoader classLoader, T urls) { return getClassLoaderURLs(classLoader, false, urls); }
/** * Loads a {@link Class} from the given {@link ClassLoader} with the context ClassLoader * set to the given ClassLoader and reset it after loading is done. */ @SuppressWarnings("unchecked") private static <T> Class<T> loadClass(ClassLoader classLoader, String className) throws ClassNotFoundException { ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(classLoader); try { return (Class<T>) classLoader.loadClass(className); } finally { ClassLoaders.setContextClassLoader(oldClassLoader); } }
/** * Gets the URL that has the given class loaded from the given ClassLoader. */ private static URL getURLByClass(ClassLoader classLoader, String className) { URL resource = classLoader.getResource(className.replace('.', '/') + ".class"); if (resource == null) { throw new IllegalStateException("Failed to find .class file resource for class " + className); } return ClassLoaders.getClassPathURL(className, resource); }
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()); ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); List<URL> urls = ClassLoaders.getClassLoaderURLs(systemClassLoader, new ArrayList<URL>()); if (!urls.remove(ClassLoaders.getClassPathURL(mainClassName, resource))) { throw new IllegalStateException("Failed to remove main class resource " + resource); ClassLoaders.setContextClassLoader(mainClassLoader);
URL jaxrsURL = ClassLoaders.getClassPathURL(Path.class); String jaxrsClassPath = null; ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(context.getProgramInvocationClassLoader()); try { job.submit(); } finally { ClassLoaders.setContextClassLoader(oldClassLoader);
/** * Finds the SparkClassLoader from the context ClassLoader hierarchy. * * @return the SparkClassLoader found * @throws IllegalStateException if no SparkClassLoader was found */ public static SparkClassLoader findFromContext() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); SparkClassLoader sparkClassLoader = ClassLoaders.find(contextClassLoader, SparkClassLoader.class); // Should find the Spark ClassLoader Preconditions.checkState(sparkClassLoader != null, "Cannot find SparkClassLoader from context ClassLoader %s", contextClassLoader); return sparkClassLoader; }
public SparkRunnerClassLoader(URL[] urls, @Nullable ClassLoader parent, boolean rewriteYarnClient) { super(urls, parent); // Copy from URLClassLoader, which also uses WeakHashMap this.closeables = new WeakHashMap<>(); this.closeablesLock = new ReentrantLock(); this.rewriter = new SparkClassRewriter(name -> ClassLoaders.openResource(this, name), rewriteYarnClient); }
/** * Creates a new instance for the following set of {@link URL}. * * @param urls the URLs from which to load classes and resources * @param parent the parent classloader for delegation */ public MainClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); this.datasetRewriter = new DatasetClassRewriter(); this.authEnforceRewriter = new AuthEnforceRewriter(); this.resourceLookup = ClassLoaders.createClassResourceLookup(this); this.cache = new HashMap<>(); }
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()); ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); List<URL> urls = ClassLoaders.getClassLoaderURLs(systemClassLoader, new ArrayList<URL>()); if (!urls.remove(ClassLoaders.getClassPathURL(mainClassName, resource))) { throw new IllegalStateException("Failed to remove main class resource " + resource); ClassLoaders.setContextClassLoader(mainClassLoader);
URL jaxrsURL = ClassLoaders.getClassPathURL(Path.class); String jaxrsClassPath = null; ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(context.getProgramInvocationClassLoader()); try { job.submit(); } finally { ClassLoaders.setContextClassLoader(oldClassLoader);
/** * Finds the SparkClassLoader from the context ClassLoader hierarchy. * * @return the SparkClassLoader found * @throws IllegalStateException if no SparkClassLoader was found */ public static SparkClassLoader findFromContext() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); SparkClassLoader sparkClassLoader = ClassLoaders.find(contextClassLoader, SparkClassLoader.class); // Should find the Spark ClassLoader Preconditions.checkState(sparkClassLoader != null, "Cannot find SparkClassLoader from context ClassLoader %s", contextClassLoader); return sparkClassLoader; }
public SparkRunnerClassLoader(URL[] urls, @Nullable ClassLoader parent, boolean rewriteYarnClient) { super(urls, parent); // Copy from URLClassLoader, which also uses WeakHashMap this.closeables = new WeakHashMap<>(); this.closeablesLock = new ReentrantLock(); this.rewriter = new SparkClassRewriter(name -> ClassLoaders.openResource(this, name), rewriteYarnClient); }
/** * Creates a new instance for the following set of {@link URL}. * * @param urls the URLs from which to load classes and resources * @param parent the parent classloader for delegation */ public MainClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); this.datasetRewriter = new DatasetClassRewriter(); this.authEnforceRewriter = new AuthEnforceRewriter(); this.resourceLookup = ClassLoaders.createClassResourceLookup(this); this.cache = new HashMap<>(); }
/** * Loads a {@link Class} from the given {@link ClassLoader} with the context ClassLoader * set to the given ClassLoader and reset it after loading is done. */ @SuppressWarnings("unchecked") private static <T> Class<T> loadClass(ClassLoader classLoader, String className) throws ClassNotFoundException { ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(classLoader); try { return (Class<T>) classLoader.loadClass(className); } finally { ClassLoaders.setContextClassLoader(oldClassLoader); } }
/** * Populates the list of {@link URL} that this ClassLoader uses, including all URLs used by the parent of the * given ClassLoader. This is the same as calling {@link #getClassLoaderURLs(ClassLoader, boolean, Collection)} with * {@code childFirst} set to {@code false}. * * @param classLoader the {@link ClassLoader} for searching urls * @param urls a {@link Collection} for storing the {@link URL}s * @return the same {@link Collection} passed from the parameter */ public static <T extends Collection<? super URL>> T getClassLoaderURLs(ClassLoader classLoader, T urls) { return getClassLoaderURLs(classLoader, false, urls); }
/** * Gets the URL that has the given class loaded from the given ClassLoader. */ private static URL getURLByClass(ClassLoader classLoader, String className) { URL resource = classLoader.getResource(className.replace('.', '/') + ".class"); if (resource == null) { throw new IllegalStateException("Failed to find .class file resource for class " + className); } return ClassLoaders.getClassPathURL(className, resource); }
/** * Finds the SparkClassLoader from the context ClassLoader hierarchy. * * @return the SparkClassLoader found * @throws IllegalStateException if no SparkClassLoader was found */ public static SparkClassLoader findFromContext() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); SparkClassLoader sparkClassLoader = ClassLoaders.find(contextClassLoader, SparkClassLoader.class); // Should find the Spark ClassLoader Preconditions.checkState(sparkClassLoader != null, "Cannot find SparkClassLoader from context ClassLoader %s", contextClassLoader); return sparkClassLoader; }
/** * Creates a new instance for the following set of {@link URL}. * * @param urls the URLs from which to load classes and resources * @param parent the parent classloader for delegation */ public SparkContainerClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); this.sparkClassRewriter = new SparkClassRewriter(name -> ClassLoaders.openResource(this, name), false); }
/** * Constructs an instance that load classes from the given directory. * <p/> * The URLs for class loading are: * <p/> * <pre> * [dir] * [dir]/*.jar * [dir]/lib/*.jar * </pre> */ public ProgramClassLoader(CConfiguration cConf, File dir, ClassLoader parent) { super(dir, cConf.get(Constants.AppFabric.PROGRAM_EXTRA_CLASSPATH), parent, "lib"); this.dir = dir; this.classResourceLookup = ClassLoaders.createClassResourceLookup(this); this.datasetClassCache = new HashMap<>(); this.datasetClassRewriter = new DatasetClassRewriter(); }
/** * Run some code with the context class loader combined from the program class loader and the system class loader. */ public <T> T execute(Callable<T> callable) throws Exception { ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(getProgramInvocationClassLoader()); try { return callable.call(); } finally { ClassLoaders.setContextClassLoader(oldClassLoader); } }