From aef89983c09e10297c1e13d61a2cfd6de093a583 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Fri, 3 Nov 2023 11:50:18 -0500 Subject: [PATCH] Simplify dir processing. Correctly sort alphabetically to match Liberty behavior --- .../plugins/config/ServerConfigDocument.java | 107 +++++++++--------- .../plugins/util/ServerFeatureUtil.java | 21 +--- 2 files changed, 58 insertions(+), 70 deletions(-) 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 b4b65cdb..edf0bb3e 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 @@ -22,15 +22,11 @@ import java.io.InputStream; import java.net.URL; import java.net.URLConnection; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; -import java.util.Comparator; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Set; -import java.util.stream.StreamSupport; import java.util.Map; import java.util.Properties; @@ -43,6 +39,7 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; +import org.apache.commons.io.comparator.NameFileComparator; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -384,41 +381,33 @@ private static void parseInclude(Document doc) throws XPathExpressionException, private static void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException { File configDropins = getConfigDropinsDir(); - if (configDropins != null && configDropins.exists()) { - File overrides = new File(configDropins, "overrides"); - if (overrides.exists()) { - parseDropinsFiles(overrides.listFiles()); - } + if (configDropins == null || !configDropins.exists()) { + return; + } - File defaults = new File(configDropins, "defaults"); - if (defaults.exists()) { - parseDropinsFiles(defaults.listFiles()); - } + File overrides = new File(configDropins, "overrides"); + if (overrides.exists()) { + parseDropinsFiles(overrides.listFiles()); } - } - private static void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException { - for (int i = 0; i < files.length; i++) { - if (files[i].isFile()) { - parseDropinsFile(files[i]); - } + File defaults = new File(configDropins, "defaults"); + if (defaults.exists()) { + parseDropinsFiles(defaults.listFiles()); } } - private static Document parseDropinsXMLFile(File file) throws FileNotFoundException, IOException { - try (FileInputStream is = new FileInputStream(file)) { - return parseDocument(is); - } catch (SAXException ex) { - // If the file was not valid XML, assume it was some other non XML - // file in dropins. - log.info("Skipping parsing " + file.getAbsolutePath() + " because it was not recognized as XML."); - return null; + private static void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException { + Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR); + for (File file : files) { + if (file.isFile()) { + parseDropinsFile(file); + } } } private static void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException { // get input XML Document - Document doc = parseDropinsXMLFile(file); + Document doc = parseDocument(file); if (doc != null) { parseApplication(doc, XPATH_SERVER_APPLICATION); parseApplication(doc, XPATH_SERVER_WEB_APPLICATION); @@ -511,17 +500,19 @@ 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) throws IOException { - DirectoryStream dstream = Files.newDirectoryStream(directory.toPath(), "*.xml"); - StreamSupport.stream(dstream.spliterator(), false) - .sorted(Comparator.comparing(Path::toString)) - .forEach(p -> { - try { - docs.add(parseDocument(p.toFile())); - } catch (Exception e) { - log.warn("Unable to parse from file " + p.getFileName() + " from specified include directory: " + directory.getPath()); - } - }); + private static void parseDocumentsInDirectory(File directory, ArrayList docs) { + // OpenLiberty reference code for behavior, parseInclude() method + // https://github.com/OpenLiberty/open-liberty/blob/integration/dev/com.ibm.ws.config/src/com/ibm/ws/config/xml/internal/XMLConfigParser.java#L409C8-L409C8 + // uses Collections.sort on filenames which is case-sensitive (all uppercase comes before lowercase) + File[] files = directory.listFiles(); + Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR); + for (File file : files) { + try { + docs.add(parseDocument(file)); + } catch (Exception e) { + log.warn("Unable to parse from file " + file.getPath() + " from specified include directory: " + directory.getPath()); + } + } } /** @@ -532,9 +523,15 @@ private static void parseDocumentsInDirectory(File directory, ArrayList parseIncludeFileOrDirectory(String includeFileName, File } if (includeFile.isDirectory()) { - try (DirectoryStream dstream = Files.newDirectoryStream(includeFile.toPath(), "*.xml")) { - StreamSupport.stream(dstream.spliterator(), false) - .sorted(Comparator.comparing(Path::toString)) - .forEach(p -> { - try { - includeFiles.add(p.toFile()); - } catch (Exception e) { - debug("Failed to resolve file from path: " + p); - } - }); - } catch (IOException e) { - debug("Unable to open include directory: " + includeFileName); + File[] files = includeFile.listFiles(); + Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR); + for (File file : files) { + includeFiles.add(file); } } else { includeFiles.add(includeFile);