Search in sources :

Example 1 with ProgramClassLoaderProvider

use of co.cask.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
   * @throws IOException if there was an error copying or unpacking the artifact
   */
CloseableClassLoader createClassLoader(File unpackDir) throws IOException {
    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;
    if (programRunner instanceof ProgramClassLoaderProvider) {
        programClassLoader = new ProgramClassLoader(cConf, unpackDir, ((ProgramClassLoaderProvider) programRunner).createProgramClassLoaderParent());
    } else {
        programClassLoader = new ProgramClassLoader(cConf, unpackDir, FilterClassLoader.create(getClass().getClassLoader()));
    }
    final ClassLoader finalProgramClassLoader = programClassLoader;
    final ProgramRunner finalProgramRunner = programRunner;
    return new CloseableClassLoader(programClassLoader, new Closeable() {

        @Override
        public void close() {
            Closeables.closeQuietly((Closeable) finalProgramClassLoader);
            if (finalProgramRunner instanceof Closeable) {
                Closeables.closeQuietly((Closeable) finalProgramRunner);
            }
        }
    });
}
Also used : ProgramClassLoader(co.cask.cdap.internal.app.runtime.ProgramClassLoader) ProgramClassLoaderProvider(co.cask.cdap.app.runtime.ProgramClassLoaderProvider) Closeable(java.io.Closeable) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) FilterClassLoader(co.cask.cdap.common.lang.FilterClassLoader) ProgramClassLoader(co.cask.cdap.internal.app.runtime.ProgramClassLoader) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) ProgramRunner(co.cask.cdap.app.runtime.ProgramRunner) IOException(java.io.IOException)

Aggregations

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