Validate.notBlank(propertyFilename, "Property filename required");
Validate.notNull(properties, "Property map required");
final String filePath = projectOperations.getPathResolver()
.getIdentifier(propertyFilePath, propertyFilename);
MutableFile mutableFile = null;
Properties props;
if (sorted) {
props = new Properties() {
private static final long serialVersionUID = 1L;
// Override the keys() method to order the keys alphabetically
@SuppressWarnings("all")
public synchronized Enumeration keys() {
final Object[] keys = keySet().toArray();
Arrays.sort(keys);
return new Enumeration() {
int i = 0;
public boolean hasMoreElements() {
return i < keys.length;
}
public Object nextElement() {
return keys[i++];
}
};
}
};
}
else {
props = new Properties();
}
if (fileManager.exists(filePath)) {
mutableFile = fileManager.updateFile(filePath);
loadProperties(props, mutableFile.getInputStream());
}
else {
// Unable to find the file, so let's create it
mutableFile = fileManager.createFile(filePath);
}
boolean saveNeeded = false;
for (final Entry<String, String> entry : properties.entrySet()) {
final String key = entry.getKey();
final String newValue = entry.getValue();
final String existingValue = props.getProperty(key);
if (existingValue == null || !existingValue.equals(newValue)
&& changeExisting) {
props.setProperty(key, newValue);
saveNeeded = true;
}
}
if (saveNeeded) {
storeProps(props, mutableFile.getOutputStream(), "Updated at "
+ new Date());
}
}