From 496697d84d8f4bc5ee787533e0d2aab6e8b35d74 Mon Sep 17 00:00:00 2001 From: dshimo Date: Tue, 5 Mar 2024 21:24:50 -0600 Subject: [PATCH] simpifly processBootstrapProperties --- .../plugins/config/ServerConfigDocument.java | 66 +++++++++---------- .../config/ServerConfigDocumentTest.java | 55 +++++----------- .../bootstrapInclude/bootstrap.properties | 1 + .../servers/bootstrapOuroboros.properties | 1 - .../bootstrap.properties} | 0 5 files changed, 48 insertions(+), 75 deletions(-) create mode 100644 src/test/resources/servers/bootstrapInclude/bootstrap.properties delete mode 100644 src/test/resources/servers/bootstrapOuroboros.properties rename src/test/resources/servers/{bootstrapInclude.properties => bootstrapOuroboros/bootstrap.properties} (100%) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java index b5ad2905..a09abb4b 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java @@ -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 @@ -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 @@ -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 @@ -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/ @@ -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 @@ -299,32 +305,17 @@ public void processJvmOptions() throws FileNotFoundException, Exception { * @throws Exception * @throws FileNotFoundException */ - public void processBootstrapProperties(Map 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 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 visited = new HashSet(); - if (processedFile != null) { - visited.add(processedFile.getAbsolutePath()); - } - processBootstrapInclude(getBootstrapIncludeProperty(), visited); + visited.add(configDirBootstrapProperties.getAbsolutePath()); + processBootstrapInclude(visited); } } @@ -335,8 +326,9 @@ public void processBootstrapProperties(Map bootstrapProp, File b * @throws Exception * @throws FileNotFoundException */ - private void processBootstrapInclude(String bootstrapIncludeLocation, Set processedBootstrapIncludes) throws FileNotFoundException, Exception { - Path bootstrapIncludePath = Paths.get(bootstrapIncludeLocation); + private void processBootstrapInclude(Set 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()); @@ -347,14 +339,10 @@ private void processBootstrapInclude(String bootstrapIncludeLocation, Set(), null); + configDocument.processBootstrapProperties(); configDocument.processServerEnv(); configDocument.parseVariablesForBothValues(serverXmlDoc); @@ -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(); @@ -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")); } @@ -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 bootstrapPropertyMap = new HashMap(); - 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 @@ -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 bootstrapProp = new HashMap(); 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(); diff --git a/src/test/resources/servers/bootstrapInclude/bootstrap.properties b/src/test/resources/servers/bootstrapInclude/bootstrap.properties new file mode 100644 index 00000000..9ebd0bd6 --- /dev/null +++ b/src/test/resources/servers/bootstrapInclude/bootstrap.properties @@ -0,0 +1 @@ +bootstrap.include=../bootstrap.properties \ No newline at end of file diff --git a/src/test/resources/servers/bootstrapOuroboros.properties b/src/test/resources/servers/bootstrapOuroboros.properties deleted file mode 100644 index f628030e..00000000 --- a/src/test/resources/servers/bootstrapOuroboros.properties +++ /dev/null @@ -1 +0,0 @@ -bootstrap.include=bootstrapOuroboros.properties \ No newline at end of file diff --git a/src/test/resources/servers/bootstrapInclude.properties b/src/test/resources/servers/bootstrapOuroboros/bootstrap.properties similarity index 100% rename from src/test/resources/servers/bootstrapInclude.properties rename to src/test/resources/servers/bootstrapOuroboros/bootstrap.properties