DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.FAILURE);
response.setOverallRequestErrorMessage("No profile service connection available");
return response;
}
DeploymentManager deploymentManager = connection.getDeploymentManager();
// as crazy as it might sound, there is apparently no way for you to ask the profile service
// if a deployment was deployed to the farm profile. Thus, we must resort to a poor man's solution:
// if the deployment name has the "farm/" directory in it, assume it needs to be deployed to the farm
boolean deployFarmed = getDeploymentKey().contains("/farm/");
if (deployFarmed) {
Collection<ProfileKey> profileKeys = deploymentManager.getProfiles();
boolean farmSupported = false;
for (ProfileKey profileKey : profileKeys) {
if (profileKey.getName().equals(FARM_PROFILE_KEY.getName())) {
farmSupported = true;
break;
}
}
if (!farmSupported) {
throw new IllegalStateException("This application server instance is not a node in a cluster, "
+ "so it does not support farmed deployments. Supported deployment profiles are " + profileKeys
+ ".");
}
if (deployExploded) {
throw new IllegalArgumentException(
"Deploying farmed applications in exploded form is not supported by the Profile Service.");
}
try {
deploymentManager.loadProfile(FARM_PROFILE_KEY);
} catch (Exception e) {
LOG.info("Failed to switch to farm profile - could not update " + resourceTypeName + " file '"
+ deploymentFile + "' using [" + packageDetails + "].");
String errorMessage = ThrowableUtil.getAllMessages(e);
return failApplicationDeployment(errorMessage, packageDetails);
}
}
String deploymentName = getDeploymentName();
if (deploymentName == null) {
DeployPackagesResponse response = new DeployPackagesResponse(ContentResponseResult.FAILURE);
response.setOverallRequestErrorMessage("Did not find deployment with key [" + getDeploymentKey() + "]");
return response;
}
// Now stop the original app.
try {
DeploymentProgress progress = deploymentManager.stop(deploymentName);
DeploymentUtils.run(progress);
} catch (Exception e) {
throw new RuntimeException("Failed to stop deployment [" + deploymentName + "].", e);
}
// And then remove it (this will delete the physical file/dir from the deploy dir).
try {
DeploymentProgress progress = deploymentManager.remove(deploymentName);
DeploymentUtils.run(progress);
} catch (Exception e) {
throw new RuntimeException("Failed to remove deployment [" + deploymentName + "].", e);
}
// Deploy away!
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Deploying '" + tempFile + "'...");
}
DeploymentUtils.deployArchive(deploymentManager, tempFile, deployExploded);
} catch (Exception e) {
// Deploy failed - rollback to the original app file...
LOG.debug("Redeploy failed - rolling back to original archive...", e);
String errorMessage = ThrowableUtil.getAllMessages(e);
try {
// Try to delete the new app file, which failed to deploy, if it still exists.
if (deploymentFile.exists()) {
try {
FileUtils.forceDelete(deploymentFile);
} catch (IOException e1) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to delete application file '" + deploymentFile
+ "' that failed to deploy.", e1);
}
}
}
// Now redeploy the original file - this generally should succeed.
DeploymentUtils.deployArchive(deploymentManager, backupOfOriginalFile, deployExploded);
errorMessage += " ***** ROLLED BACK TO ORIGINAL APPLICATION FILE. *****";
// If the redeployment of the original backup succeeded then cleanup the backup from disk
deleteTemporaryFile(backupDir);
// If the redeployment fails the original backup is preserved on disk until agent restart
} catch (Exception e1) {
LOG.debug("Rollback failed!", e1);
errorMessage += " ***** FAILED TO ROLLBACK TO ORIGINAL APPLICATION FILE. *****: "
+ ThrowableUtil.getAllMessages(e1);
}
//since the deployment failed remove the temp application downloaded for deployment
deleteTemporaryFile(tempFile);
LOG.info("Failed to update " + resourceTypeName + " file '" + deploymentFile + "' using [" + packageDetails
+ "].");
return failApplicationDeployment(errorMessage, packageDetails);
} finally {
// Make sure to switch back to the 'applications' profile if we switched to the 'farm' profile above.
if (deployFarmed) {
try {
deploymentManager.loadProfile(APPLICATIONS_PROFILE_KEY);
} catch (Exception e) {
LOG.debug("Failed to switch back to applications profile from farm profile", e);
}
}
}