Search in sources :

Example 1 with TwillRunnable

use of org.apache.twill.api.TwillRunnable in project cdap by caskdata.

the class ExploreCustomClassLoaderTwillRunnable method initialize.

@Override
public void initialize(TwillContext context) {
    super.initialize(context);
    if (getClass().getClassLoader() instanceof URLClassLoader) {
        LOG.debug("Using ResourcesClassLoader to load config files bundled with application.");
        customClassLoader = new ResourcesClassLoader(((URLClassLoader) getClass().getClassLoader()).getURLs(), getClass().getClassLoader());
    } else {
        LOG.warn("Classloader is not URLCLassLoader, config files bundled with application might not be loaded.");
        customClassLoader = getClass().getClassLoader();
    }
    ClassLoader previousClassLoader = ClassLoaders.setContextClassLoader(customClassLoader);
    try {
        @SuppressWarnings("unchecked") Class<? extends TwillRunnable> twillRunnableClass = (Class<? extends TwillRunnable>) customClassLoader.loadClass(CLASS_NAME);
        twillRunnable = new InstantiatorFactory(false).get(TypeToken.of(twillRunnableClass)).create();
        twillRunnable.initialize(context);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        ClassLoaders.setContextClassLoader(previousClassLoader);
    }
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) URLClassLoader(java.net.URLClassLoader) TwillRunnable(org.apache.twill.api.TwillRunnable) AbstractTwillRunnable(org.apache.twill.api.AbstractTwillRunnable) URLClassLoader(java.net.URLClassLoader)

Aggregations

InstantiatorFactory (co.cask.cdap.common.lang.InstantiatorFactory)1 URLClassLoader (java.net.URLClassLoader)1 AbstractTwillRunnable (org.apache.twill.api.AbstractTwillRunnable)1 TwillRunnable (org.apache.twill.api.TwillRunnable)1