-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #882 from mattl-netflix/feature/CASS-1608
Preliminary refactoring in advance of backporting PropertiesFileTuner…
- Loading branch information
Showing
6 changed files
with
39 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 23 additions & 55 deletions
78
priam/src/main/java/com/netflix/priam/tuner/PropertiesFileTuner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,55 @@ | ||
package com.netflix.priam.tuner; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Splitter; | ||
import com.google.inject.Inject; | ||
import com.netflix.priam.config.IConfiguration; | ||
import org.apache.commons.configuration2.PropertiesConfiguration; | ||
import org.apache.commons.configuration2.ex.ConfigurationException; | ||
import org.apache.commons.lang3.text.StrSubstitutor; | ||
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.commons.text.StringSubstitutor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.inject.Inject; | ||
import com.netflix.priam.config.IConfiguration; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Support tuning standard .properties files | ||
* <p> | ||
*/ | ||
public class PropertiesFileTuner | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(JVMOptionsTuner.class); | ||
private static final Logger logger = LoggerFactory.getLogger(PropertiesFileTuner.class); | ||
protected final IConfiguration config; | ||
protected final String propertyPrefix; | ||
|
||
@Inject | ||
public PropertiesFileTuner(IConfiguration config, String prefix) { | ||
public PropertiesFileTuner(IConfiguration config) | ||
{ | ||
this.config = config; | ||
this.propertyPrefix = prefix; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void updateAndSaveProperties(String configPath) throws IOException, ConfigurationException | ||
public void updateAndSaveProperties(String propertyFile) throws IOException, ConfigurationException | ||
{ | ||
File propertiesFile = new File(configPath); | ||
try { | ||
if (propertiesFile.exists() && !propertiesFile.canWrite()) { | ||
throw new IOException("Can't write and therefore cannot tune" + configPath); | ||
} | ||
|
||
PropertiesConfiguration properties = new PropertiesConfiguration(); | ||
properties.getLayout().load(properties, new FileReader(propertiesFile.getPath())); | ||
|
||
Set<String> keys = new HashSet<>(); | ||
properties.getKeys().forEachRemaining(keys::add); | ||
|
||
Map<String, String> overridenProperties = new HashMap<>(); | ||
String propertyOverrides = config.getProperty("propertyOverrides." + propertyPrefix, null); | ||
|
||
if (propertyOverrides != null) { | ||
properties.getLayout().load(properties, new FileReader(propertyFile)); | ||
String overrides = config.getProperty("propertyOverrides." + FilenameUtils.getBaseName(propertyFile), null); | ||
if (overrides != null) { | ||
// Allow use of the IConfiguration object as template strings | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, Object> map = objectMapper.convertValue(config, Map.class); | ||
StrSubstitutor sub = new StrSubstitutor(map); | ||
String resolvedPropertyOverrides = sub.replace(propertyOverrides); | ||
|
||
String[] pairs = resolvedPropertyOverrides.split(","); | ||
for (String kv : pairs) { | ||
String[] entry = kv.split("="); | ||
if (entry.length != 2) | ||
continue; | ||
keys.add(entry[0]); | ||
overridenProperties.put(entry[0], entry[1]); | ||
} | ||
Map<String, Object> map = new ObjectMapper().convertValue(config, Map.class); | ||
String resolvedOverrides = new StringSubstitutor(map).replace(overrides); | ||
Splitter.on(",").withKeyValueSeparator("=").split(resolvedOverrides).forEach(properties::setProperty); | ||
} | ||
|
||
for (String key: keys) { | ||
if (overridenProperties.containsKey(key)) | ||
properties.setProperty(key, overridenProperties.get(key)); | ||
} | ||
|
||
properties.getLayout().save(properties, new FileWriter(propertiesFile.getPath())); | ||
properties.getLayout().save(properties, new FileWriter(propertyFile)); | ||
} | ||
catch (IOException | ConfigurationException e) { | ||
logger.error("Could not tune " + configPath, e); | ||
logger.error("Could not tune " + propertyFile + ". Does it exist? Is it writable?", e); | ||
throw e; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters