* available.
* @throws CoreException if error occurred while writing to a Manifest file.
*/
public boolean write(IProgressMonitor monitor, ApplicationDeploymentInfo previousInfo) throws CoreException {
ApplicationDeploymentInfo deploymentInfo = appModule.getDeploymentInfo();
if (deploymentInfo == null) {
return false;
}
// Fetch the previous name, in case the app name was changed. This will
// allow the old
// entry to be replaced by the new one, since application entries are
// looked up by application name.
String previousName = previousInfo != null ? previousInfo.getDeploymentName() : null;
String appName = deploymentInfo.getDeploymentName();
Map<Object, Object> deploymentInfoYaml = parseManifestFromFile();
if (deploymentInfoYaml == null) {
deploymentInfoYaml = new LinkedHashMap<Object, Object>();
}
Object applicationsObj = deploymentInfoYaml.get(APPLICATIONS_PROP);
List<Map<Object, Object>> applicationsList = null;
if (applicationsObj == null) {
applicationsList = new ArrayList<Map<Object, Object>>();
deploymentInfoYaml.put(APPLICATIONS_PROP, applicationsList);
}
else if (applicationsObj instanceof List<?>) {
applicationsList = (List<Map<Object, Object>>) applicationsObj;
}
else {
throw CloudErrorUtil.toCoreException("Expected a top-level list of applications in: " + relativePath //$NON-NLS-1$
+ ". Unable to continue writing manifest values."); //$NON-NLS-1$
}
Map<Object, Object> applicationWithSameName = null;
Map<Object, Object> oldApplication = null;
// Each application listing should be a map. Find both an entry with the
// same name as the application name
// As well as an entry with an older name of the application, in case
// the application has changed.
for (Object appMap : applicationsList) {
if (appMap instanceof Map<?, ?>) {
Map<Object, Object> properties = (Map<Object, Object>) appMap;
String name = getStringValue(properties, NAME_PROP);
if (appName.equals(name)) {
applicationWithSameName = properties;
}
else if (previousName != null && previousName.equals(name)) {
oldApplication = properties;
}
}
}
// The order of priority in terms of replacing an existing entry is : 1.
// old application entry that
// has been changed will get replaced 2. existing entry with same name
// as app will now get replaced2.
Map<Object, Object> application = oldApplication != null ? oldApplication : applicationWithSameName;
if (application == null) {
application = new LinkedHashMap<Object, Object>();
applicationsList.add(application);
}
application.put(NAME_PROP, appName);
String memory = getMemoryAsString(deploymentInfo.getMemory());
if (memory != null) {
application.put(MEMORY_PROP, memory);
}
int instances = deploymentInfo.getInstances();
if (instances > 0) {
application.put(INSTANCES_PROP, instances);
}
List<String> urls = deploymentInfo.getUris();
if (urls != null && !urls.isEmpty()) {
// Persist only the first URL
String url = urls.get(0);
ApplicationUrlLookupService lookup = ApplicationUrlLookupService.getCurrentLookup(cloudServer);
CloudApplicationURL cloudUrl = lookup.getCloudApplicationURL(url);
String subdomain = cloudUrl.getSubdomain();
String domain = cloudUrl.getDomain();
if (subdomain != null) {
application.put(SUB_DOMAIN_PROP, subdomain);
}
if (domain != null) {
application.put(DOMAIN_PROP, domain);
}
}
List<EnvironmentVariable> envvars = deploymentInfo.getEnvVariables();
Map<Object, Object> varMap = new LinkedHashMap<Object, Object>();
// Clear the list of environment variables first.
application.put(ENV_PROP, varMap);
if (envvars != null) {
for (EnvironmentVariable var : envvars) {
varMap.put(var.getVariable(), var.getValue());
}
}
Staging staging = deploymentInfo.getStaging();
if (staging != null && staging.getBuildpackUrl() != null) {
application.put(BUILDPACK_PROP, staging.getBuildpackUrl());
}
String archiveURL = deploymentInfo.getArchive();
if (archiveURL != null) {
application.put(PATH_PROP, archiveURL);
}
// Regardless if there are services or not, always clear list of
// services in the manifest, and replace with new list. The list of
// services in the
// deployment info has to match the content in the manifest.
Map<Object, Object> services = new LinkedHashMap<Object, Object>();
application.put(SERVICES_PROP, services);
List<CloudService> servicesToBind = deploymentInfo.getServices();
if (servicesToBind != null) {
for (CloudService service : servicesToBind) {
String serviceName = service.getName();