Skip to content

Commit

Permalink
bootstrap.include recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
dshimo committed Mar 4, 2024
1 parent 48d57a4 commit 9b2fa45
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
* 2. {server.config.dir}/configDropins/defaults/
* 3. {server.config.dir}/
* 4. {server.config.dir}/configDropins/overrides/
* TODO: potential processing for system property tags? (-D prefixes?)
* @throws FileNotFoundException
* @throws Exception
*/
Expand Down Expand Up @@ -276,13 +277,16 @@ public void initializeFields(CommonLoggerI log, File serverXML, File configDir,

/**
* Process bootstrap.properties and boostrap.include
* @param bootstrapProp
* @param bootstrapFile - Optional specific file
* @param bootstrapProp - Populated in Maven/Gradle
* @param bootstrapFile - Optional specific file which will take precedence over config dir file
* @throws Exception
* @throws FileNotFoundException
*/
public void processBootstrapProperties(Map<String, String> bootstrapProp, File bootstrapFile) throws Exception, FileNotFoundException {
File cfgDirFile = getFileFromConfigDirectory("bootstrap.properties");
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) {
Expand All @@ -291,18 +295,47 @@ public void processBootstrapProperties(Map<String, String> bootstrapProp, File b
}
} else if (bootstrapFile != null && bootstrapFile.exists()) {
parseProperties(new FileInputStream(bootstrapFile));
} else if (cfgDirFile != null) {
parseProperties(new FileInputStream(cfgDirFile));
processedFile = bootstrapFile;
} else if (configDirBootstrapProperties != null) {
parseProperties(new FileInputStream(configDirBootstrapProperties));
processedFile = configDirBootstrapProperties;
}

// Recursive processing for bootstrap.include
if (props.containsKey("bootstrap.include")) {
Path bootstrapIncludePath = Paths.get(props.getProperty("bootstrap.include")).normalize();
File bootstrapIncludeFile = bootstrapIncludePath.isAbsolute() ?
new File(bootstrapIncludePath.toString()) : new File(configDirectory, bootstrapIncludePath.toString());
if (bootstrapIncludeFile.exists()) {
parseProperties(new FileInputStream(bootstrapIncludeFile));
Set<String> visited = new HashSet<String>();
if (processedFile != null) {
visited.add(processedFile.getAbsolutePath());
}
processBootstrapInclude(getBootstrapIncludeProperty(), visited);
}
}

/**
* Recursive processing for a series of bootstrap.include that terminates upon revisit
* @param bootstrapIncludeLocation
* @param processedBootstrapIncludes
* @throws Exception
* @throws FileNotFoundException
*/
private void processBootstrapInclude(String bootstrapIncludeLocation, Set<String> processedBootstrapIncludes) throws FileNotFoundException, Exception {
Path bootstrapIncludePath = Paths.get(bootstrapIncludeLocation);
File bootstrapIncludeFile = bootstrapIncludePath.isAbsolute() ?
new File(bootstrapIncludePath.toString()) : new File(configDirectory, bootstrapIncludePath.toString());

if (processedBootstrapIncludes.contains(bootstrapIncludeFile.getAbsolutePath())) {
return;
}

if (bootstrapIncludeFile.exists()) {
parseProperties(new FileInputStream(bootstrapIncludeFile));
processedBootstrapIncludes.add(bootstrapIncludeFile.getAbsolutePath());
processBootstrapInclude(getBootstrapIncludeProperty(), processedBootstrapIncludes);
}
}

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

//Checks for application names in the document. Will add locations without names to a Set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public void environmentVariables() throws FileNotFoundException, Exception {

}


// 3. bootstrap.properties
@Test
public void processBootstrapProperties() throws FileNotFoundException, Exception {
Expand Down Expand Up @@ -173,10 +172,16 @@ public void processBootstrapProperties() throws FileNotFoundException, Exception
configDocument.processBootstrapProperties(bootstrapPropertyMap, null);
assertEquals("1000", configDocument.getProperties().getProperty("http.port"));

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

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

// 4. Java system properties
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/servers/bootstrapOuroboros.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap.include=bootstrapOuroboros.properties

0 comments on commit 9b2fa45

Please sign in to comment.