Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove static vars from ServerConfigDocument #435

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2017, 2023.
* (C) Copyright IBM Corporation 2017, 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,22 +53,18 @@
// Moved from ci.maven/liberty-maven-plugin/src/main/java/net/wasdev/wlp/maven/plugins/ServerConfigDocument.java
public class ServerConfigDocument {

private static ServerConfigDocument instance;
private CommonLoggerI log;

private static CommonLoggerI log;
private File configDirectory;
private File serverXMLFile;

private static DocumentBuilder docBuilder;

private static File configDirectory;
private static File serverXMLFile;

private static Set<String> names;
private static Set<String> namelessLocations;
private static Set<String> locations;
private static HashMap<String, String> locationsAndNames;
private static Properties props;
private static Properties defaultProps;
private static Map<String, File> libertyDirectoryPropertyToFile = null;
private Set<String> names;
private Set<String> namelessLocations;
private Set<String> locations;
private HashMap<String, String> locationsAndNames;
private Properties props;
private Properties defaultProps;
private Map<String, File> libertyDirectoryPropertyToFile = null;

private static final XPathExpression XPATH_SERVER_APPLICATION;
private static final XPathExpression XPATH_SERVER_WEB_APPLICATION;
Expand Down Expand Up @@ -104,86 +100,49 @@ public Set<String> getNamelessLocations() {
return namelessLocations;
}

public static Properties getProperties() {
public Properties getProperties() {
return props;
}

public static Map<String, File> getLibertyDirPropertyFiles() {
public Map<String, File> getLibertyDirPropertyFiles() {
return libertyDirectoryPropertyToFile;
}

public static Properties getDefaultProperties() {
public Properties getDefaultProperties() {
return defaultProps;
}

private static File getServerXML() {
public File getServerXML() {
return serverXMLFile;
}

public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile) {
this(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, true, null);
}

public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence) {
this(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, null);
}

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);
}

private static DocumentBuilder getDocumentBuilder() {
if (docBuilder == null) {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringComments(true);
docBuilderFactory.setCoalescing(true);
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setValidating(false);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// fail catastrophically if we can't create a document builder
throw new RuntimeException(e);
}
}
return docBuilder;
}

/**
* Nulls out cached instance so a new one will be created next time a getInstance() is done.
* Not thread-safe.
*/
public static void markInstanceStale() {
instance = null;
}

public static ServerConfigDocument getInstance(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile) throws IOException {
return getInstance(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, true, null);
}

public static ServerConfigDocument getInstance(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence) throws IOException {
return getInstance(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, null);
}
private DocumentBuilder getDocumentBuilder() {
DocumentBuilder docBuilder;

public static ServerConfigDocument getInstance(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) throws IOException {
// Initialize if instance is not created yet, or source server xml file
// location has been changed.
if (instance == null || !serverXML.getCanonicalPath().equals(getServerXML().getCanonicalPath())) {
instance = new ServerConfigDocument(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, libertyDirPropertyFiles);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringComments(true);
docBuilderFactory.setCoalescing(true);
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setValidating(false);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// fail catastrophically if we can't create a document builder
throw new RuntimeException(e);
}
return instance;

return docBuilder;
}

private static void initializeAppsLocation(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
private void initializeAppsLocation(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) {
try {
ServerConfigDocument.log = log;
this.log = log;
serverXMLFile = serverXML;
configDirectory = configDir;
if (libertyDirPropertyFiles != null) {
Expand Down Expand Up @@ -268,7 +227,7 @@ private static void initializeAppsLocation(CommonLoggerI log, File serverXML, Fi
}

//Checks for application names in the document. Will add locations without names to a Set
private static void parseNames(Document doc, String expression) throws XPathExpressionException, IOException, SAXException {
private void parseNames(Document doc, String expression) throws XPathExpressionException, IOException, SAXException {
// parse input document
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
Expand Down Expand Up @@ -315,7 +274,7 @@ private static void parseNames(Document doc, String expression) throws XPathExpr
}
}

public static String findNameForLocation(String location) {
public String findNameForLocation(String location) {
String appName = locationsAndNames.get(location);

if (appName == null || appName.isEmpty()) {
Expand All @@ -325,7 +284,7 @@ public static String findNameForLocation(String location) {
return appName;
}

private static void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {
private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {

NodeList nodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);

Expand All @@ -349,7 +308,7 @@ private static void parseApplication(Document doc, XPathExpression expression) t
}
}

private static void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException {
private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException {
// parse include document in source server xml
NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET);

Expand Down Expand Up @@ -378,7 +337,7 @@ private static void parseInclude(Document doc) throws XPathExpressionException,
}
}

private static void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException {
private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException {
File configDropins = getConfigDropinsDir();

if (configDropins == null || !configDropins.exists()) {
Expand All @@ -396,7 +355,7 @@ private static void parseConfigDropinsDir() throws XPathExpressionException, IOE
}
}

private static void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException {
private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException {
Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
for (File file : files) {
if (file.isFile()) {
Expand All @@ -405,7 +364,7 @@ private static void parseDropinsFiles(File[] files) throws XPathExpressionExcept
}
}

private static void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException {
private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException {
// get input XML Document
Document doc = parseDocument(file);
if (doc != null) {
Expand All @@ -416,7 +375,7 @@ private static void parseDropinsFile(File file) throws IOException, XPathExpress
}
}

private static ArrayList<Document> getIncludeDocs(String loc) throws IOException, SAXException {
private ArrayList<Document> getIncludeDocs(String loc) throws IOException, SAXException {
ArrayList<Document> docs = new ArrayList<Document>();
Document doc = null;
File locFile = null;
Expand Down Expand Up @@ -467,7 +426,7 @@ private static ArrayList<Document> getIncludeDocs(String loc) throws IOException
* @throws IOException
* @throws SAXException
*/
private static void parseDocumentFromFileOrDirectory(File f, String locationString, ArrayList<Document> docs) throws FileNotFoundException, IOException, SAXException {
private void parseDocumentFromFileOrDirectory(File f, String locationString, ArrayList<Document> docs) throws FileNotFoundException, IOException, SAXException {
Document doc = null;
// Earlier call to VariableUtility.resolveVariables() already converts all \ to /
boolean isLibertyDirectory = locationString.endsWith("/"); // Liberty uses this to determine if directory.
Expand Down Expand Up @@ -499,7 +458,7 @@ private static void parseDocumentFromFileOrDirectory(File f, String locationStri
* @param docs - ArrayList to store parsed Documents.
* @throws IOException
*/
private static void parseDocumentsInDirectory(File directory, ArrayList<Document> docs) {
private void parseDocumentsInDirectory(File directory, ArrayList<Document> docs) {
// OpenLiberty reference code for behavior: https://github.com/OpenLiberty/open-liberty
// ServerXMLConfiguration.java:parseDirectoryFiles() and XMLConfigParser.java:parseInclude()
File[] files = directory.listFiles();
Expand All @@ -521,7 +480,7 @@ private static void parseDocumentsInDirectory(File directory, ArrayList<Document
* @throws IOException
* @throws SAXException
*/
private static Document parseDocument(File file) throws FileNotFoundException, IOException {
private Document parseDocument(File file) throws FileNotFoundException, IOException {
try (FileInputStream is = new FileInputStream(file)) {
return parseDocument(is);
} catch (SAXException ex) {
Expand All @@ -532,20 +491,20 @@ private static Document parseDocument(File file) throws FileNotFoundException, I
}
}

private static Document parseDocument(URL url) throws IOException, SAXException {
private Document parseDocument(URL url) throws IOException, SAXException {
URLConnection connection = url.openConnection();
try (InputStream is = connection.getInputStream()) {
return parseDocument(is);
}
}

private static Document parseDocument(InputStream in) throws SAXException, IOException {
private Document parseDocument(InputStream in) throws SAXException, IOException {
try (InputStream ins = in) { // ins will be auto-closed
return getDocumentBuilder().parse(ins);
}
}

private static void parseProperties(InputStream ins) throws Exception {
private void parseProperties(InputStream ins) throws Exception {
try {
props.load(ins);
} catch (Exception e) {
Expand All @@ -557,7 +516,7 @@ private static void parseProperties(InputStream ins) throws Exception {
}
}

private static boolean isValidURL(String url) {
private boolean isValidURL(String url) {
try {
URL testURL = new URL(url);
testURL.toURI();
Expand All @@ -568,19 +527,19 @@ private static boolean isValidURL(String url) {
}


private static void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException {
private void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException {
parseVariables(doc, true, false, false);
}

private static void parseVariablesForValues(Document doc) throws XPathExpressionException {
private void parseVariablesForValues(Document doc) throws XPathExpressionException {
parseVariables(doc, false, true, false);
}

private static void parseVariablesForBothValues(Document doc) throws XPathExpressionException {
private void parseVariablesForBothValues(Document doc) throws XPathExpressionException {
parseVariables(doc, false, false, true);
}

private static void parseVariables(Document doc, boolean defaultValues, boolean values, boolean both) throws XPathExpressionException {
private void parseVariables(Document doc, boolean defaultValues, boolean values, boolean both) throws XPathExpressionException {
// parse input document
NodeList nodeList = (NodeList) XPATH_SERVER_VARIABLE.evaluate(doc, XPathConstants.NODESET);

Expand All @@ -605,7 +564,7 @@ private static void parseVariables(Document doc, boolean defaultValues, boolean
}
}

private static String getValue(NamedNodeMap attr, String nodeName) {
private String getValue(NamedNodeMap attr, String nodeName) {
String value = null;
Node valueNode = attr.getNamedItem(nodeName);
if (valueNode != null) {
Expand All @@ -614,7 +573,7 @@ private static String getValue(NamedNodeMap attr, String nodeName) {
return value;
}

private static void parseIncludeVariables(Document doc) throws XPathExpressionException, IOException, SAXException {
private void parseIncludeVariables(Document doc) throws XPathExpressionException, IOException, SAXException {
// parse include document in source server xml
NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET);

Expand All @@ -641,7 +600,7 @@ private static void parseIncludeVariables(Document doc) throws XPathExpressionEx
}
}

private static File getConfigDropinsDir() {
private File getConfigDropinsDir() {
File configDropins = null;

// if configDirectory exists and contains configDropins directory,
Expand All @@ -656,7 +615,7 @@ private static File getConfigDropinsDir() {
return configDropins;
}

private static void parseConfigDropinsDirVariables(String inDir)
private void parseConfigDropinsDirVariables(String inDir)
throws XPathExpressionException, SAXException, IOException {
File configDropins = getConfigDropinsDir();
if (configDropins == null || !configDropins.exists()) {
Expand All @@ -677,7 +636,7 @@ private static void parseConfigDropinsDirVariables(String inDir)
}
}

private static void parseDropinsFilesVariables(File file)
private void parseDropinsFilesVariables(File file)
throws SAXException, IOException, XPathExpressionException {
// get input XML Document
Document doc = parseDocument(file);
Expand All @@ -693,7 +652,7 @@ private static void parseDropinsFilesVariables(File file)
* 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 static File findConfigFile(String fileName, File specificFile, boolean giveConfigDirPrecedence) {
private File findConfigFile(String fileName, File specificFile, boolean giveConfigDirPrecedence) {
File f = new File(configDirectory, fileName);

if (giveConfigDirPrecedence) {
Expand All @@ -718,7 +677,7 @@ private static File findConfigFile(String fileName, File specificFile, boolean g
/*
* Get the file from configDrectory if it exists, or null if not
*/
private static File getFileFromConfigDirectory(String file) {
private File getFileFromConfigDirectory(String file) {
File f = new File(configDirectory, file);
if (configDirectory != null && f.exists()) {
return f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,9 @@ protected Map<String, Object> createMapBasedInstallKernelInstance(String bundle,
mapBasedInstallKernel.put("target.user.directory", usrDir);
if (isDebugEnabled()) {
mapBasedInstallKernel.put("debug", Level.FINEST);
} /* else { removed due to "java.lang.ClassNotFoundException: com.ibm.websphere.ras.DataFormatHelper" reported in ci.gradle issue 867
} else { //removed due to "java.lang.ClassNotFoundException: com.ibm.websphere.ras.DataFormatHelper" reported in ci.gradle issue 867
mapBasedInstallKernel.put("debug", Level.INFO);
} */
}
return mapBasedInstallKernel;
}

Expand Down
Loading