@Override public BodyProducer create(HttpContentProducer contentProducer, ServiceTaskExecutor taskExecutor) { final ClassLoader programContextClassLoader = new CombineClassLoader( null, contentProducer.getClass().getClassLoader(), getClass().getClassLoader()); // Capture the context since we need to keep it until the end of the content producing. // We don't need to worry about double capturing of the context when HttpContentConsumer is used. // This is because when HttpContentConsumer is used, the responder constructed here will get closed and this // BodyProducerFactory won't be used. return new BodyProducerAdapter(contentProducer, taskExecutor, context.capture(), defaultTxControl); } }, context.getServiceTaskExecutor(), metricsContext);
@Override public BodyProducer create(HttpContentProducer contentProducer, ServiceTaskExecutor taskExecutor) { final ClassLoader programContextClassLoader = new CombineClassLoader( null, contentProducer.getClass().getClassLoader(), getClass().getClassLoader()); // Capture the context since we need to keep it until the end of the content producing. // We don't need to worry about double capturing of the context when HttpContentConsumer is used. // This is because when HttpContentConsumer is used, the responder constructed here will get closed and this // BodyProducerFactory won't be used. return new BodyProducerAdapter(contentProducer, taskExecutor, context.capture(), defaultTxControl); } }, context.getServiceTaskExecutor(), metricsContext);
static ClassLoader createParent(ClassLoader templateClassLoader) { // Find the ProgramClassLoader from the template ClassLoader ClassLoader programClassLoader = templateClassLoader; while (programClassLoader != null && !(programClassLoader instanceof ProgramClassLoader)) { programClassLoader = programClassLoader.getParent(); } // This shouldn't happen Preconditions.checkArgument(programClassLoader != null, "Cannot find ProgramClassLoader"); // Package filtered classloader of the template classloader, which only classes in "Export-Packages" are loadable. Manifest manifest = ((ProgramClassLoader) programClassLoader).getManifest(); Set<String> exportPackages = ManifestFields.getExportPackages(manifest); ClassLoader filteredTemplateClassLoader = new PackageFilterClassLoader(templateClassLoader, Predicates.in(exportPackages)); // The lib Classloader needs to be able to see all cdap api classes as well. // In this way, parent ClassLoader of the plugin ClassLoader will load class from the parent of the // template program class loader (which is a filtered CDAP classloader), // followed by template export-packages, then by a plugin lib jars. return new CombineClassLoader(programClassLoader.getParent(), filteredTemplateClassLoader); }
/** * Creates a new instance of {@link ClassLoader} that will be used for program method invocation. * By default it is a {@link CombineClassLoader} with program classloader, * plugins export-package classloader and system classloader in that loading order. */ protected ClassLoader createProgramInvocationClassLoader() { // A classloader that can load all export-package classes from all plugins ClassLoader pluginsClassLoader = PluginClassLoaders.createFilteredPluginsClassLoader( program.getApplicationSpecification().getPlugins(), pluginInstantiator); return new CombineClassLoader(null, program.getClassLoader(), pluginsClassLoader, getClass().getClassLoader()); }
static ClassLoader createParent(ClassLoader templateClassLoader) { // Find the ProgramClassLoader from the template ClassLoader ClassLoader programClassLoader = templateClassLoader; while (programClassLoader != null && !(programClassLoader instanceof ProgramClassLoader)) { programClassLoader = programClassLoader.getParent(); } // This shouldn't happen Preconditions.checkArgument(programClassLoader != null, "Cannot find ProgramClassLoader"); // Package filtered classloader of the template classloader, which only classes in "Export-Packages" are loadable. Manifest manifest = ((ProgramClassLoader) programClassLoader).getManifest(); Set<String> exportPackages = ManifestFields.getExportPackages(manifest); ClassLoader filteredTemplateClassLoader = new PackageFilterClassLoader(templateClassLoader, Predicates.in(exportPackages)); // The lib Classloader needs to be able to see all cdap api classes as well. // In this way, parent ClassLoader of the plugin ClassLoader will load class from the parent of the // template program class loader (which is a filtered CDAP classloader), // followed by template export-packages, then by a plugin lib jars. return new CombineClassLoader(programClassLoader.getParent(), filteredTemplateClassLoader); }
/** * @param filter A {@link FilterClassLoader.Filter} for filtering out classes from the * @param extraClasspath extra list of {@link URL} to be added to the end of the classpath for the * {@link MainClassLoader} to be created * @return a new instance from the current context classloader or the system classloader. The returned * {@link MainClassLoader} will be the defining classloader for classes in the context classloader * that the filter rejected. For classes that pass the filter, the defining classloader will be the original * context classloader. * It will return {@code null} if it is not able to create a new instance due to lack of classpath information. */ @Nullable public static MainClassLoader createFromContext(FilterClassLoader.Filter filter, URL...extraClasspath) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } List<URL> classpath = new ArrayList<>(); if (classLoader instanceof URLClassLoader) { classpath.addAll(Arrays.asList(((URLClassLoader) classLoader).getURLs())); } else if (classLoader == ClassLoader.getSystemClassLoader()) { addClassPath(classpath); } else { // No able to create a new MainClassLoader return null; } classpath.addAll(Arrays.asList(extraClasspath)); ClassLoader filtered = new FilterClassLoader(classLoader, filter); ClassLoader parent = new CombineClassLoader(classLoader.getParent(), filtered); return new MainClassLoader(classpath.toArray(new URL[classpath.size()]), parent); }
@Nullable PluginInstantiator pluginInstantiator) { if (plugins.isEmpty() || pluginInstantiator == null) { return new CombineClassLoader(null); return new CombineClassLoader(null, pluginClassLoaders); } catch (IOException e) { throw Throwables.propagate(e);
/** * @param filter A {@link FilterClassLoader.Filter} for filtering out classes from the * @param extraClasspath extra list of {@link URL} to be added to the end of the classpath for the * {@link MainClassLoader} to be created * @return a new instance from the current context classloader or the system classloader. The returned * {@link MainClassLoader} will be the defining classloader for classes in the context classloader * that the filter rejected. For classes that pass the filter, the defining classloader will be the original * context classloader. * It will return {@code null} if it is not able to create a new instance due to lack of classpath information. */ @Nullable public static MainClassLoader createFromContext(FilterClassLoader.Filter filter, URL...extraClasspath) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } List<URL> classpath = new ArrayList<>(); if (classLoader instanceof URLClassLoader) { classpath.addAll(Arrays.asList(((URLClassLoader) classLoader).getURLs())); } else if (classLoader == ClassLoader.getSystemClassLoader()) { addClassPath(classpath); } else { // No able to create a new MainClassLoader return null; } classpath.addAll(Arrays.asList(extraClasspath)); ClassLoader filtered = new FilterClassLoader(classLoader, filter); ClassLoader parent = new CombineClassLoader(classLoader.getParent(), filtered); return new MainClassLoader(classpath.toArray(new URL[classpath.size()]), parent); }
@Nullable PluginInstantiator pluginInstantiator) { if (plugins.isEmpty() || pluginInstantiator == null) { return new CombineClassLoader(null); return new CombineClassLoader(null, pluginClassLoaders); } catch (IOException e) { throw Throwables.propagate(e);
/** * Creates a new instance of {@link ClassLoader} that will be used for program method invocation. * By default it is a {@link CombineClassLoader} with program classloader, * plugins export-package classloader and system classloader in that loading order. */ protected ClassLoader createProgramInvocationClassLoader() { // A classloader that can load all export-package classes from all plugins ClassLoader pluginsClassLoader = PluginClassLoaders.createFilteredPluginsClassLoader( program.getApplicationSpecification().getPlugins(), pluginInstantiator); return new CombineClassLoader(null, program.getClassLoader(), pluginsClassLoader, getClass().getClassLoader()); }
ClassLoader outputClassLoader = new CombineClassLoader(outputFormatClassLoader, Thread.currentThread().getContextClassLoader());
ClassLoader outputClassLoader = new CombineClassLoader(outputFormatClassLoader, Thread.currentThread().getContextClassLoader());
new CombineClassLoader(parentPluginCL.getParent(), parentPluginCL.getExportPackagesClassLoader()); return new PluginClassLoader(key.artifact, unpackedDir, parentCL);
new CombineClassLoader(parentPluginCL.getParent(), parentPluginCL.getExportPackagesClassLoader()); return new PluginClassLoader(key.artifact, unpackedDir, parentCL);
new CombineClassLoader(null, app.getClass().getClassLoader(), getClass().getClassLoader())); try { app.configure(configurer, new DefaultApplicationContext<>(appConfig));
new CombineClassLoader(null, app.getClass().getClassLoader(), getClass().getClassLoader())); try { app.configure(configurer, new DefaultApplicationContext<>(appConfig));
@Test public void testCombineClassLoader() throws ClassNotFoundException { // Creates a CombineClassLoader with two delegates. // One allows "co.cask.cdap.api.app", the other allows "co.cask.cdap.api.annotation" ClassLoader parent = getClass().getClassLoader(); ClassLoader classLoader = new CombineClassLoader(null, new PackageFilterClassLoader(parent, Predicates.equalTo(Application.class.getPackage().getName())), new PackageFilterClassLoader(parent, Predicates.equalTo(Beta.class.getPackage().getName())) ); // Should be able to load classes from those two packages Assert.assertSame(ApplicationConfigurer.class, classLoader.loadClass(ApplicationConfigurer.class.getName())); Assert.assertSame(Property.class, classLoader.loadClass(Property.class.getName())); // For classes not in those packages would failed. try { classLoader.loadClass(Bytes.class.getName()); Assert.fail(); } catch (ClassNotFoundException e) { // Expected } }
ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(new CombineClassLoader( DistributedProgramRunner.this.getClass().getClassLoader(), extraDependencies.stream().map(Class::getClassLoader)::iterator));
ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(new CombineClassLoader( DistributedProgramRunner.this.getClass().getClassLoader(), extraDependencies.stream().map(Class::getClassLoader)::iterator));