if (packages.size() != 1) {
DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.FAILURE);
response.setOverallRequestErrorMessage("When deploying WAR files only one can be updated at a time.");
return response;
}
ResourcePackageDetails packageDetails = packages.iterator().next();
// Find location of existing application
Configuration pluginConfig = getResourceContext().getPluginConfiguration();
File appFile = new File(pluginConfig.getSimple(PROPERTY_FILENAME).getStringValue());
if (!appFile.exists()) {
return failApplicationDeployment("Could not find application to update at location: " + appFile,
packageDetails);
}
boolean isExploded = appFile.isDirectory();
// save the new version of the app to a temp location
File tempFile;
try {
tempFile = writeAppBitsToTempFile(appFile, contentServices, packageDetails);
} catch (Exception e) {
return failApplicationDeployment("Error writing new application bits to temporary file - cause: " + e,
packageDetails);
}
// delete the current app but don't undeploy. This option should maintain the existing mbeans while
// removing the app. Back up the bits in case we need to restore if the new app fails to deploy
File backupFile = null;
try {
backupFile = deleteApp(pluginConfig, appFile, true, false);
} catch (Exception e) {
if (appFile.exists()) {
return failApplicationDeployment("Error undeploying existing app - cause: " + e, packageDetails);
}
// log but proceed with no backup
log.warn("Failed to create app backup but proceeding with redeploy of " + appFile.getPath() + ": " + e);
}
FileContentDelegate contentDelegate = new FileContentDelegate(appFile.getParentFile());
try {
// Write the new bits for the application. If successful Tomcat will pick it up and complete the deploy.
contentDelegate.createContent(appFile, tempFile, isExploded);
} catch (Exception e) {
// Deploy failed - rollback to the original app file...
String errorMessage = ThrowableUtil.getAllMessages(e);
try {
FileUtils.purge(appFile, true);
contentDelegate.createContent(appFile, backupFile, isExploded);
errorMessage += " ***** ROLLED BACK TO ORIGINAL APPLICATION FILE. *****";
} catch (Exception e1) {
errorMessage += " ***** FAILED TO ROLLBACK TO ORIGINAL APPLICATION FILE. *****: "
+ ThrowableUtil.getAllMessages(e1);
}
return failApplicationDeployment(errorMessage, packageDetails);
}
// Deploy was successful!
deleteBackupOfOriginalFile(backupFile);
DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.SUCCESS);
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(packageDetails.getKey(),
ContentResponseResult.SUCCESS);
response.addPackageResponse(packageResponse);
return response;
}