Skip to content

Commit

Permalink
interface, switch to libertyDirPropMap usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dshimo committed Mar 11, 2024
1 parent da1eaf8 commit 267059f
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,48 @@ public File getServerXML() {
}

/**
*
* Deprecated. Migrate to the simpler constructor.
* @param log
* @param serverXML
* @param configDir
* @param bootstrapFile
* @param bootstrapProp
* @param serverEnvFile
* @param giveConfigDirPrecedence
* @param libertyDirPropertyFiles - Contains a property to file mapping of direcdty locations
* @param libertyDirPropertyFiles - Contains a property to file mapping of directory locations
*/
public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) {
initializeAppsLocation(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, libertyDirPropertyFiles);
}

public ServerConfigDocument() {

}
this.log = log;
serverXMLFile = serverXML;
configDirectory = configDir;
if (libertyDirPropertyFiles != null) {
libertyDirectoryPropertyToFile = new HashMap<String, File>(libertyDirPropertyFiles);
} else {
log.warn("The properties for directories are null and could lead to application locations not being resolved correctly.");
libertyDirectoryPropertyToFile = new HashMap<String,File>();
}
locations = new HashSet<String>();
names = new HashSet<String>();
namelessLocations = new HashSet<String>();
locationsAndNames = new HashMap<String, String>();
props = new Properties();
defaultProps = new Properties();

// LCLS constructor
public ServerConfigDocument(CommonLoggerI log) {
// TODO: populate libertyDirectoryPropertyToFile with workspace information
initializeFields(log, null, null, null);
initializeAppsLocation();
}

public void initializeFields(CommonLoggerI log, File serverXML, File configDir, Map<String, File> libertyDirPropertyFiles) {
/**
* Adapt when ready. Expects the libertyDirPropertyFiles to be populated
* @param log
* @param libertyDirPropertyFiles
*/
public ServerConfigDocument(CommonLoggerI log, Map<String, File> libertyDirPropertyFiles) {
this.log = log;
serverXMLFile = serverXML;
configDirectory = configDir;
if (libertyDirPropertyFiles != null) {
libertyDirectoryPropertyToFile = new HashMap<String, File>(libertyDirPropertyFiles);
configDirectory = libertyDirectoryPropertyToFile.get(ServerFeatureUtil.SERVER_CONFIG_DIR);
serverXMLFile = getFileFromConfigDirectory("server.xml");
} else {
log.warn("The properties for directories are null and could lead to application locations not being resolved correctly.");
libertyDirectoryPropertyToFile = new HashMap<String,File>();
Expand All @@ -164,6 +175,14 @@ public void initializeFields(CommonLoggerI log, File serverXML, File configDir,
locationsAndNames = new HashMap<String, String>();
props = new Properties();
defaultProps = new Properties();

// initializeAppsLocation();
}

// LCLS constructor
// TODO: populate libertyDirectoryPropertyToFile with workspace information
public ServerConfigDocument(CommonLoggerI log) {
this(log, null);
}

private DocumentBuilder getDocumentBuilder() {
Expand All @@ -186,38 +205,35 @@ private DocumentBuilder getDocumentBuilder() {
return docBuilder;
}

private void initializeAppsLocation(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) {
/**
// 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}/
// jvm.options
// a. ${wlp.user.dir}/shared/jvm.options
// b. ${server.config.dir}/configDropins/defaults/
// c. ${server.config.dir}/
// d. ${server.config.dir}/configDropins/overrides/
// 3. bootstrap.properties
// a. additional references by bootstrap.include
// 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
// 6. variable values declared in the server.xml file
// a. ${server.config.dir}/configDropins/defaults/
// b. ${server.config.dir}/server.xml
// c. ${server.config.dir}/configDropins/overrides/
// 7. variables declared on the command line
*/
public void initializeAppsLocation() {
try {
initializeFields(log, serverXML, configDir, libertyDirPropertyFiles);

// 1. Need to parse variables in the server.xml for default values before trying to
// find the include files in case one of the variables is used in the location.
Document doc = parseDocument(serverXMLFile);

// 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}/
// jvm.options
// a. ${wlp.user.dir}/shared/jvm.options
// b. ${server.config.dir}/configDropins/defaults/
// c. ${server.config.dir}/
// d. ${server.config.dir}/configDropins/overrides/
// 3. bootstrap.properties
// a. additional references by bootstrap.include
// 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
// 6. variable values declared in the server.xml file
// a. ${server.config.dir}/configDropins/defaults/
// b. ${server.config.dir}/server.xml
// c. ${server.config.dir}/configDropins/overrides/
// 7. variables declared on the command line

// 1. Need to parse variables in the server.xml for default values before trying to find the include files in case one of the variables is used
// in the location.
parseVariablesForDefaultValues(doc);

// 2. get variables from server.env
Expand All @@ -235,45 +251,23 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf
// 5. Variables loaded from 'variables' directory
processVariablesDirectory();

// 6. variable values declared in server.xml
// resolve variable references along the way
// configDropins/defaults
// server.xml
// configDropins/overrides
// 6. variable values declared in server.xml(s)
processServerXml(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>
parseConfigDropinsDirVariables("defaults");

// 6. variables defined in server.xml - non-default values
parseVariablesForValues(doc);

// 7. variables from configDropins/overrides/<file_name>
parseConfigDropinsDirVariables("overrides");

parseApplication(doc, XPATH_SERVER_APPLICATION);
parseApplication(doc, XPATH_SERVER_WEB_APPLICATION);
parseApplication(doc, XPATH_SERVER_ENTERPRISE_APPLICATION);
parseNames(doc, "/server/application | /server/webApplication | /server/enterpriseApplication");
parseInclude(doc);
parseConfigDropinsDir();

} catch (Exception e) {
e.printStackTrace();
}
}


/**
* server.env file read order
* 1. {wlp.install.dir}/etc/
Expand All @@ -289,8 +283,7 @@ public void processServerEnv() throws Exception, FileNotFoundException {
"etc" + File.separator + serverEnvString));
parsePropertiesFromFile(new File(libertyDirectoryPropertyToFile.get(ServerFeatureUtil.WLP_USER_DIR),
"shared" + File.separator + serverEnvString));
parsePropertiesFromFile(new File(libertyDirectoryPropertyToFile.get(ServerFeatureUtil.SERVER_CONFIG_DIR),
serverEnvString));
parsePropertiesFromFile(getFileFromConfigDirectory(serverEnvString));
}

/**
Expand Down Expand Up @@ -319,15 +312,15 @@ public void processJvmOptions() throws FileNotFoundException, Exception {
* @throws FileNotFoundException
*/
public void processBootstrapProperties() throws Exception, FileNotFoundException {
File configDirBootstrapProperties = getFileFromConfigDirectory("bootstrap.properties");
if (configDirBootstrapProperties == null) {
File bootstrapFile = getFileFromConfigDirectory("bootstrap.properties");
if (bootstrapFile == null) {
return;
}

parseProperties(new FileInputStream(configDirBootstrapProperties));
parsePropertiesFromFile(bootstrapFile);
if (props.containsKey("bootstrap.include")) {
Set<String> visited = new HashSet<String>();
visited.add(configDirBootstrapProperties.getAbsolutePath());
visited.add(bootstrapFile.getAbsolutePath());
processBootstrapInclude(visited);
}
}
Expand Down Expand Up @@ -382,7 +375,7 @@ public void processVariablesDirectory() throws FileNotFoundException, Exception
}

for (File directory : toProcess) {
if (!directory.isDirectory()) {
if (directory == null || !directory.isDirectory()) {
continue;
}
processVariablesDirectory(directory, "");
Expand Down Expand Up @@ -418,6 +411,20 @@ private void processVariablesDirectory(File directory, String propertyPrefix)
}
}

/**
*
* @param doc
* @throws XPathExpressionException
* @throws IOException
* @throws SAXException
*/
public void processServerXml(Document doc) throws XPathExpressionException, IOException, SAXException {
parseIncludeVariables(doc);
parseConfigDropinsDirVariables("defaults");
parseVariablesForValues(doc);
parseConfigDropinsDirVariables("overrides");
}

//Checks for application names in the document. Will add locations without names to a Set
private void parseNames(Document doc, String expression) throws XPathExpressionException, IOException, SAXException {
// parse input document
Expand Down Expand Up @@ -700,6 +707,7 @@ public void parsePropertiesFromFile(File propertiesFile) throws Exception, FileN
if (propertiesFile != null && propertiesFile.exists()) {
parseProperties(new FileInputStream(propertiesFile));
}
log.debug("Ignoring non-existing properties file: " + propertiesFile.getAbsolutePath());
}

private void parseProperties(InputStream ins) throws Exception {
Expand Down Expand Up @@ -846,39 +854,11 @@ private void parseDropinsFilesVariables(File file)
}
}

/*
* If giveConfigDirPrecedence is set to true, return the file from the configDirectory if it exists;
* otherwise return specificFile if it exists, or null if not.
* If giveConfigDirPrecedence is set to false, return specificFile if it exists;
* otherwise return the file from the configDirectory if it exists, or null if not.
*/
private File findConfigFile(String fileName, File specificFile, boolean giveConfigDirPrecedence) {
File f = new File(configDirectory, fileName);

if (giveConfigDirPrecedence) {
if (configDirectory != null && f.exists()) {
return f;
}
if (specificFile != null && specificFile.exists()) {
return specificFile;
}
} else {
if (specificFile != null && specificFile.exists()) {
return specificFile;
}
if (configDirectory != null && f.exists()) {
return f;
}
}

return null;
}

/*
* Get the file from configDrectory if it exists, or null if not
*/
private File getFileFromConfigDirectory(String file) {
File f = new File(configDirectory, file);
private File getFileFromConfigDirectory(String filename) {
File f = new File(configDirectory, filename);
if (configDirectory != null && f.exists()) {
return f;
}
Expand Down
Loading

0 comments on commit 267059f

Please sign in to comment.