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);
}
}
Aggregations