public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> packages, ContentServices contentServices) {
// You can only update the one application file referenced by this resource, so punch out if multiple are
// specified.
if (packages.size() != 1) {
log.warn("Request to update an EAR/WAR file contained multiple packages.");
DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.FAILURE);
response
.setOverallRequestErrorMessage("When deploying an EAR/WAR, only one EAR/WAR 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(FILENAME_PLUGIN_CONFIG_PROP).getStringValue());
if (!appFile.exists()) {
return failApplicationDeployment("Could not find application to update at location: " + appFile,
packageDetails);
}
File tempFile;
try {
tempFile = writeNewAppBitsToTempFile(appFile, contentServices, packageDetails);
} catch (Exception e) {
return failApplicationDeployment("Error writing new application bits to temporary file - cause: " + e,
packageDetails);
}
//Before the backup find out if the current deployment is exploded or not.
//Do not do this after the backup because the original files are no longer
//on disk and thus .isDirectory() always returns false.
boolean isExploded = appFile.isDirectory();
// Backup the existing app file/dir to <filename>.rej.
File backupOfOriginalFile = new File(appFile.getPath() + BACKUP_FILE_SUFFIX);
appFile.renameTo(backupOfOriginalFile);
// Write the new bits for the application
moveTempFileToDeployLocation(tempFile, appFile, isExploded);
// The file has been written successfully to the deploy dir. Now try to actually deploy it.
MainDeployer mainDeployer = getParentResourceComponent().getMainDeployer();
try {
mainDeployer.redeploy(appFile);
// Deploy was successful, delete the backup
try {
FileUtils.purge(backupOfOriginalFile, true);
} catch (Exception e) {
log.warn("Failed to delete backup of original file: " + backupOfOriginalFile);
}
} catch (Exception e) {
// Deploy failed - rollback to the original app file...
String errorMessage = ThrowableUtil.getAllMessages(e);
try {
FileUtils.purge(appFile, true);
backupOfOriginalFile.renameTo(appFile);
// Need to redeploy the original file - this generally should succeed.
mainDeployer.redeploy(appFile);
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);
}
DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.SUCCESS);
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(packageDetails.getKey(),
ContentResponseResult.SUCCESS);
response.addPackageResponse(packageResponse);
return response;
}