Search in sources :

Example 1 with ProgramClassLoaderProvider

use of io.cdap.cdap.app.runtime.ProgramClassLoaderProvider in project cdap by caskdata.

the class ArtifactClassLoaderFactory method createClassLoader.

/**
 * Create a classloader that loads classes from a directory where an artifact jar has been expanded, with access to
 * packages that all program type has access to. The classloader created is only for artifact inspection purpose
 * and shouldn't be used for program execution as it doesn't have the proper class filtering for the specific
 * program type for the program being executed.
 *
 * @param unpackDir the directory where the artifact jar has been expanded
 * @return a closeable classloader based off the specified artifact; on closing the returned {@link ClassLoader},
 *         all temporary resources created for the classloader will be removed
 */
CloseableClassLoader createClassLoader(File unpackDir) {
    ProgramRunner programRunner = null;
    try {
        // Try to create a ProgramClassLoader from the Spark runtime system if it is available.
        // It is needed because we don't know what program types that an artifact might have.
        // TODO: CDAP-5613. We shouldn't always expose the Spark classes.
        programRunner = programRunnerFactory.create(ProgramType.SPARK);
    } catch (Exception e) {
        // If Spark is not supported, exception is expected. We'll use the default filter.
        LOG.trace("Spark is not supported. Not using ProgramClassLoader from Spark", e);
    }
    ProgramClassLoader programClassLoader = null;
    if (programRunner instanceof ProgramClassLoaderProvider) {
        programClassLoader = new ProgramClassLoader(cConf, unpackDir, ((ProgramClassLoaderProvider) programRunner).createProgramClassLoaderParent());
    }
    if (programClassLoader == null) {
        programClassLoader = new ProgramClassLoader(cConf, unpackDir, FilterClassLoader.create(getClass().getClassLoader()));
    }
    final ClassLoader finalProgramClassLoader = programClassLoader;
    final ProgramRunner finalProgramRunner = programRunner;
    return new CloseableClassLoader(programClassLoader, () -> {
        Closeables.closeQuietly((Closeable) finalProgramClassLoader);
        if (finalProgramRunner instanceof Closeable) {
            Closeables.closeQuietly((Closeable) finalProgramRunner);
        }
    });
}
Also used : ProgramClassLoader(io.cdap.cdap.internal.app.runtime.ProgramClassLoader) ProgramClassLoaderProvider(io.cdap.cdap.app.runtime.ProgramClassLoaderProvider) Closeable(java.io.Closeable) ProgramClassLoader(io.cdap.cdap.internal.app.runtime.ProgramClassLoader) FilterClassLoader(io.cdap.cdap.common.lang.FilterClassLoader) DirectoryClassLoader(io.cdap.cdap.common.lang.DirectoryClassLoader) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) ProgramRunner(io.cdap.cdap.app.runtime.ProgramRunner)

Aggregations

CloseableClassLoader (io.cdap.cdap.api.artifact.CloseableClassLoader)1 ProgramClassLoaderProvider (io.cdap.cdap.app.runtime.ProgramClassLoaderProvider)1 ProgramRunner (io.cdap.cdap.app.runtime.ProgramRunner)1 DirectoryClassLoader (io.cdap.cdap.common.lang.DirectoryClassLoader)1 FilterClassLoader (io.cdap.cdap.common.lang.FilterClassLoader)1 ProgramClassLoader (io.cdap.cdap.internal.app.runtime.ProgramClassLoader)1 Closeable (java.io.Closeable)1