Module combinedPlatformModule;
try {
combinedPlatformModule = loadPlatformModules(appConfig, modulesLoader);
} catch (Exception e) {
throw new DeploymentFailedException("Cannot load platform modules", e);
}
if (appConfig.getAppURI() == null) {
if (appConfig.getAppClassName() != null) {
try {
// In that case we won't be using an S4R classloader, app classes are available from the current
// classloader
// The app module provides bindings specific to the app class loader, in this case the current
// thread's
// class loader.
AppModule appModule = new AppModule(Thread.currentThread().getContextClassLoader());
// NOTE: because the app module can be overriden
Module combinedModule = Modules.override(appModule).with(combinedPlatformModule);
Injector injector = parentInjector.createChildInjector(combinedModule);
logger.info("Starting S4 app with application class [{}]", appConfig.getAppClassName());
return (App) injector.getInstance(Class.forName(appConfig.getAppClassName(), true, modulesLoader));
// server.startApp(app, "appName", clusterName);
} catch (Exception e) {
throw new DeploymentFailedException(String.format(
"Cannot start application: cannot instantiate app class %s due to: %s",
appConfig.getAppClassName(), e.getMessage()), e);
}
} else {
throw new DeploymentFailedException(
"Application class name must be specified when application URI omitted");
}
} else {
try {
URI uri = new URI(appConfig.getAppURI());
// fetch application
File localS4RFileCopy;
try {
localS4RFileCopy = File.createTempFile("tmp", "s4r");
} catch (IOException e1) {
logger.error(
"Cannot deploy app [{}] because a local copy of the S4R file could not be initialized due to [{}]",
appConfig.getAppName(), e1.getClass().getName() + "->" + e1.getMessage());
throw new DeploymentFailedException("Cannot deploy application [" + appConfig.getAppName() + "]",
e1);
}
localS4RFileCopy.deleteOnExit();
try {
if (ByteStreams.copy(fetcher.fetch(uri), Files.newOutputStreamSupplier(localS4RFileCopy)) == 0) {
throw new DeploymentFailedException("Cannot copy archive from [" + uri.toString() + "] to ["
+ localS4RFileCopy.getAbsolutePath() + "] (nothing was copied)");
}
} catch (Exception e) {
throw new DeploymentFailedException("Cannot deploy application [" + appConfig.getAppName()
+ "] from URI [" + uri.toString() + "] ", e);
}
// install locally
Injector injector = parentInjector.createChildInjector(combinedPlatformModule);
App loadedApp = loadS4R(injector, localS4RFileCopy, appConfig.getAppName());
if (loadedApp != null) {
return loadedApp;
} else {
throw new DeploymentFailedException("Cannot deploy application [" + appConfig.getAppName()
+ "] from URI [" + uri.toString() + "] : cannot start application");
}
} catch (URISyntaxException e) {
throw new DeploymentFailedException(String.format(
"Cannot deploy application [%s] : invalid URI for fetching S4R archive %s : %s", new Object[] {
appConfig.getAppName(), appConfig.getAppURI(), e.getMessage() }), e);
}
}
}