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 1c8b517e..bd2dbcb9 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 @@ -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. @@ -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 names; - private static Set namelessLocations; - private static Set locations; - private static HashMap locationsAndNames; - private static Properties props; - private static Properties defaultProps; - private static Map libertyDirectoryPropertyToFile = null; + private Set names; + private Set namelessLocations; + private Set locations; + private HashMap locationsAndNames; + private Properties props; + private Properties defaultProps; + private Map libertyDirectoryPropertyToFile = null; private static final XPathExpression XPATH_SERVER_APPLICATION; private static final XPathExpression XPATH_SERVER_WEB_APPLICATION; @@ -104,86 +100,49 @@ public Set getNamelessLocations() { return namelessLocations; } - public static Properties getProperties() { + public Properties getProperties() { return props; } - public static Map getLibertyDirPropertyFiles() { + public Map 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 bootstrapProp, File serverEnvFile) { - this(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, true, null); - } - - public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile, - Map 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 bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map 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 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 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 bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map 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 bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map libertyDirPropertyFiles) { try { - ServerConfigDocument.log = log; + this.log = log; serverXMLFile = serverXML; configDirectory = configDir; if (libertyDirPropertyFiles != null) { @@ -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); @@ -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()) { @@ -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); @@ -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); @@ -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()) { @@ -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()) { @@ -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) { @@ -416,7 +375,7 @@ private static void parseDropinsFile(File file) throws IOException, XPathExpress } } - private static ArrayList getIncludeDocs(String loc) throws IOException, SAXException { + private ArrayList getIncludeDocs(String loc) throws IOException, SAXException { ArrayList docs = new ArrayList(); Document doc = null; File locFile = null; @@ -467,7 +426,7 @@ private static ArrayList getIncludeDocs(String loc) throws IOException * @throws IOException * @throws SAXException */ - private static void parseDocumentFromFileOrDirectory(File f, String locationString, ArrayList docs) throws FileNotFoundException, IOException, SAXException { + private void parseDocumentFromFileOrDirectory(File f, String locationString, ArrayList 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. @@ -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 docs) { + private void parseDocumentsInDirectory(File directory, ArrayList docs) { // OpenLiberty reference code for behavior: https://github.com/OpenLiberty/open-liberty // ServerXMLConfiguration.java:parseDirectoryFiles() and XMLConfigParser.java:parseInclude() File[] files = directory.listFiles(); @@ -521,7 +480,7 @@ private static void parseDocumentsInDirectory(File directory, ArrayList 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; } diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/ServerConfigDocumentTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/ServerConfigDocumentTest.java index f0381438..ac3e19f6 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/util/ServerConfigDocumentTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/util/ServerConfigDocumentTest.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2023. + * (C) Copyright IBM Corporation 2023, 2024. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import java.io.File; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -53,7 +52,7 @@ public void testAppLocationUsesLibertyProperty() throws Exception { File serverXML = new File(serverDirectory, "server.xml"); - ServerConfigDocument scd = ServerConfigDocument.getInstance(log, serverXML, serverDirectory, null, null, null, true, libertyDirectoryPropertyToFile); + ServerConfigDocument scd = new ServerConfigDocument(log, serverXML, serverDirectory, null, null, null, true, libertyDirectoryPropertyToFile); Set locations = scd.getLocations(); assertTrue("Expected six app locations", locations.size() == 6);