Skip to content

Commit

Permalink
simpifly processBootstrapProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
dshimo committed Mar 6, 2024
1 parent a5a71a5 commit 496697d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, F
initializeAppsLocation(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, libertyDirPropertyFiles);
}

public ServerConfigDocument() {

}

// LCLS constructor
public ServerConfigDocument(CommonLoggerI log) {
// TODO: populate libertyDirectoryPropertyToFile with workspace information
Expand Down Expand Up @@ -192,6 +196,10 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
// Server variable precedence in ascending order if defined in multiple locations.
// 1. variable default values in the server.xml file
// 2. environment variables
// server.env
// a. ${wlp.install.dir}/etc/
// b. ${wlp.user.dir}/shared/
// c. ${server.config.dir}/
// 3. bootstrap.properties
// 4. Java system properties
// 5. Variables loaded from files in the ${server.config.dir}/variables directory or other directories as specified by the VARIABLE_SOURCE_DIRS environment variable
Expand All @@ -209,7 +217,7 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
// processJvmOptions();

// 3. get variables from bootstrap.properties
processBootstrapProperties(bootstrapProp, bootstrapFile);
processBootstrapProperties();

// 4. Java system properties
// configured in Maven/Gradle
Expand All @@ -218,14 +226,17 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
processVariablesDirectory();

// 6. variable values declared in server.xml
// processVariablesForValues(doc);
parseVariablesForValues(doc);

// 7. variables delcared on the command line
// configured in Maven/Gradle

// TODO: cleanup rest

// current code parses the includes section for a list of files to iterate through
// includes section needs to resolve the variables because it's in the server.xml
// after includes is determined, configDropins are analyzed
// 4. parse variables from include files (both default and non-default values - which we store separately)

parseIncludeVariables(doc);

// 5. variables from configDropins/defaults/<file_name>
Expand All @@ -249,11 +260,6 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
}
}

public void parsePropertiesFromFile(File propertiesFile) throws Exception, FileNotFoundException {
if (propertiesFile != null && propertiesFile.exists()) {
parseProperties(new FileInputStream(propertiesFile));
}
}

/**
* server.env file read order
Expand Down Expand Up @@ -299,32 +305,17 @@ public void processJvmOptions() throws FileNotFoundException, Exception {
* @throws Exception
* @throws FileNotFoundException
*/
public void processBootstrapProperties(Map<String, String> bootstrapProp, File bootstrapFile) throws Exception, FileNotFoundException {
public void processBootstrapProperties() throws Exception, FileNotFoundException {
File configDirBootstrapProperties = getFileFromConfigDirectory("bootstrap.properties");
File processedFile = null;

// Initial bootstrap.properties processing
if (bootstrapProp != null && !bootstrapProp.isEmpty()) {
for (Map.Entry<String,String> entry : bootstrapProp.entrySet()) {
if (entry.getValue() != null) {
props.setProperty(entry.getKey(),entry.getValue());
}
}
} else if (bootstrapFile != null && bootstrapFile.exists()) {
parseProperties(new FileInputStream(bootstrapFile));
processedFile = bootstrapFile;
} else if (configDirBootstrapProperties != null) {
parseProperties(new FileInputStream(configDirBootstrapProperties));
processedFile = configDirBootstrapProperties;
if (configDirBootstrapProperties == null) {
return;
}

// Recursive processing for bootstrap.include
parseProperties(new FileInputStream(configDirBootstrapProperties));
if (props.containsKey("bootstrap.include")) {
Set<String> visited = new HashSet<String>();
if (processedFile != null) {
visited.add(processedFile.getAbsolutePath());
}
processBootstrapInclude(getBootstrapIncludeProperty(), visited);
visited.add(configDirBootstrapProperties.getAbsolutePath());
processBootstrapInclude(visited);
}
}

Expand All @@ -335,8 +326,9 @@ public void processBootstrapProperties(Map<String, String> bootstrapProp, File b
* @throws Exception
* @throws FileNotFoundException
*/
private void processBootstrapInclude(String bootstrapIncludeLocation, Set<String> processedBootstrapIncludes) throws FileNotFoundException, Exception {
Path bootstrapIncludePath = Paths.get(bootstrapIncludeLocation);
private void processBootstrapInclude(Set<String> processedBootstrapIncludes) throws FileNotFoundException, Exception {
String bootstrapIncludeLocationString = props.getProperty("bootstrap.include");
Path bootstrapIncludePath = Paths.get(bootstrapIncludeLocationString);
File bootstrapIncludeFile = bootstrapIncludePath.isAbsolute() ?
new File(bootstrapIncludePath.toString()) : new File(configDirectory, bootstrapIncludePath.toString());

Expand All @@ -347,14 +339,10 @@ private void processBootstrapInclude(String bootstrapIncludeLocation, Set<String
if (bootstrapIncludeFile.exists()) {
parseProperties(new FileInputStream(bootstrapIncludeFile));
processedBootstrapIncludes.add(bootstrapIncludeFile.getAbsolutePath());
processBootstrapInclude(getBootstrapIncludeProperty(), processedBootstrapIncludes);
processBootstrapInclude(processedBootstrapIncludes);
}
}

private String getBootstrapIncludeProperty() {
return props.getProperty("bootstrap.include");
}

/**
* By default, ${server.config.directory}/variables is processed.
* If VARIABLE_SOURCE_DIRS is defined, those directories are processed instead.
Expand Down Expand Up @@ -704,6 +692,12 @@ private Document parseDocument(InputStream in) throws SAXException, IOException
}
}

public void parsePropertiesFromFile(File propertiesFile) throws Exception, FileNotFoundException {
if (propertiesFile != null && propertiesFile.exists()) {
parseProperties(new FileInputStream(propertiesFile));
}
}

private void parseProperties(InputStream ins) throws Exception {
try {
props.load(ins);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void serverXmlEnvVarVariationLookup() throws FileNotFoundException, Excep
configDocument.parseVariablesForBothValues(serverXmlDoc);
assertEquals("${this.value}", configDocument.getDefaultProperties().getProperty("server.env.defined"));
assertEquals("${this.value}", configDocument.getProperties().getProperty("server.env.defined"));
configDocument.processBootstrapProperties(new HashMap<>(), null);
configDocument.processBootstrapProperties();
configDocument.processServerEnv();

configDocument.parseVariablesForBothValues(serverXmlDoc);
Expand All @@ -105,11 +105,6 @@ public void serverXmlEnvVarVariationLookup() throws FileNotFoundException, Excep
// assertEquals("DEFINED", configDocument.getProperties().getProperty("bootstrap.properties.defined"));
}

// server.env files are read in increasing precedence
// 1. {wlp.install.dir}/etc
// 2. {wlp.user.dir}/shared
// 3. {server.config.dir}
// Liberty Directory Properties: https://openliberty.io/docs/latest/reference/directory-locations-properties.html
@Test
public void processServerEnv() throws FileNotFoundException, Exception {
File wlpInstallDir = WLP_DIR.toFile();
Expand All @@ -125,14 +120,15 @@ public void processServerEnv() throws FileNotFoundException, Exception {
configDocument.processServerEnv();
Properties props = configDocument.getProperties();

// {wlp.install.dir}/etc
// in increasing precedence
// 1. {wlp.install.dir}/etc
assertEquals("true", props.get("etc.unique"));

// {wlp.user.dir}/shared
// 2. {wlp.user.dir}/shared
assertEquals("true", props.get("shared.unique"));
assertEquals("true", props.get("shared.overriden"));
// {server.config.dir}

// 3. {server.config.dir}
assertEquals("old_value", props.get("overriden_value"));
assertEquals("1111", props.get("http.port"));
}
Expand All @@ -149,40 +145,25 @@ public void processBootstrapProperties() throws FileNotFoundException, Exception
File serversDir = SERVERS_RESOURCES_DIR.toFile();
ServerConfigDocument configDocument;

// bootstrap.properties in config dir
// bootstrap.properties
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), null, serversDir, null);
configDocument.processBootstrapProperties(new HashMap<>(), new File("DOES_NOT_EXIST"));
configDocument.processBootstrapProperties();
assertEquals(1, configDocument.getProperties().size());

// use bootstrapFile, kept for flexibility
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), null, serversDir, null);
configDocument.processBootstrapProperties(new HashMap<>(), SERVER_CONFIG_DIR.resolve("bootstrap.properties").toFile());
assertEquals(2, configDocument.getProperties().size());
assertEquals("DEFINED", configDocument.getProperties().getProperty("THAT_VALUE"));

// test bootstrapProperty map overrides
Map<String, String> bootstrapPropertyMap = new HashMap<String, String>();
bootstrapPropertyMap.put("http.port", "1000");
File serverXml = new File(serversDir, "definedVariables.xml");
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), serverXml, serversDir, null);
configDocument.parseVariablesForBothValues(configDocument.parseDocument(serverXml));
assertEquals("9081", configDocument.getProperties().getProperty("http.port"));
configDocument.processBootstrapProperties(bootstrapPropertyMap, null);
assertEquals("1000", configDocument.getProperties().getProperty("http.port"));
assertEquals("extraFeatures.xml", configDocument.getProperties().getProperty("extras.filename"));

// bootstrap.include
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), null, serversDir, null);
configDocument.processBootstrapProperties(new HashMap<>(), SERVERS_RESOURCES_DIR.resolve("bootstrapInclude.properties").toFile());
configDocument.initializeFields(new TestLogger(), null, new File(serversDir, "bootstrapInclude"), null);
configDocument.processBootstrapProperties();
assertEquals(2, configDocument.getProperties().size());
assertTrue(configDocument.getProperties().containsKey("bootstrap.include"));
assertEquals("extraFeatures.xml", configDocument.getProperties().getProperty("extras.filename"));

// bootstrap.include infinite termination check
// bootstrap.include termination check
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), null, serversDir, null);
configDocument.processBootstrapProperties(new HashMap<>(), SERVERS_RESOURCES_DIR.resolve("bootstrapOuroboros.properties").toFile());
configDocument.initializeFields(new TestLogger(), null, new File(serversDir, "bootstrapOuroboros"), null);
configDocument.processBootstrapProperties();
}

// 4. Java system properties
Expand All @@ -209,14 +190,12 @@ public void variablesDir() throws FileNotFoundException, Exception {
// process VARIABLE_SOURCE_DIRS
configDocument = new ServerConfigDocument(new TestLogger());
configDocument.initializeFields(new TestLogger(), null, serversDir, null);
Map<String, String> bootstrapProp = new HashMap<String, String>();
String delimiter = (File.separator.equals("/")) ? ":" : ";";
String variableSourceDirsTestValue = String.join(delimiter,
SERVERS_RESOURCES_DIR.resolve("variables").toString(),
SERVER_CONFIG_DIR.toString(),
"DOES_NOT_EXIST");
bootstrapProp.put("VARIABLE_SOURCE_DIRS", variableSourceDirsTestValue);
configDocument.processBootstrapProperties(bootstrapProp, null);
configDocument.getProperties().put("VARIABLE_SOURCE_DIRS", variableSourceDirsTestValue);
configDocument.processVariablesDirectory();

props = configDocument.getProperties();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap.include=../bootstrap.properties
1 change: 0 additions & 1 deletion src/test/resources/servers/bootstrapOuroboros.properties

This file was deleted.

0 comments on commit 496697d

Please sign in to comment.