Skip to content

Commit

Permalink
Correctly sort dir files alphabetically to match Liberty behavior (#429)
Browse files Browse the repository at this point in the history
* Simplify dir processing. Correctly sort alphabetically to match Liberty behavior
  • Loading branch information
evie-lau authored Nov 15, 2023
1 parent 03286ff commit 653a3be
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -203,7 +200,7 @@ private static void initializeAppsLocation(CommonLoggerI log, File serverXML, Fi
props = new Properties();
defaultProps = new Properties();

Document doc = parseDocument(new FileInputStream(serverXMLFile));
Document doc = parseDocument(serverXMLFile);

// Server variable precedence in ascending order if defined in
// multiple locations.
Expand Down Expand Up @@ -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);
Expand All @@ -435,8 +424,7 @@ private static ArrayList<Document> getIncludeDocs(String loc) throws IOException
if (loc.startsWith("http:") || loc.startsWith("https:")) {
if (isValidURL(loc)) {
URL url = new URL(loc);
URLConnection connection = url.openConnection();
doc = parseDocument(connection.getInputStream());
doc = parseDocument(url);
docs.add(doc);
}
} else if (loc.startsWith("file:")) {
Expand Down Expand Up @@ -511,17 +499,18 @@ 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) throws IOException {
DirectoryStream<Path> 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<Document> docs) {
// OpenLiberty reference code for behavior: https://github.com/OpenLiberty/open-liberty
// ServerXMLConfiguration.java:parseDirectoryFiles() and XMLConfigParser.java:parseInclude()
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());
}
}
}

/**
Expand All @@ -532,9 +521,22 @@ private static void parseDocumentsInDirectory(File directory, ArrayList<Document
* @throws IOException
* @throws SAXException
*/
private static Document parseDocument(File file) throws FileNotFoundException, IOException, SAXException {
InputStream is = new FileInputStream(file.getCanonicalPath());
return parseDocument(is);
private static Document parseDocument(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 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 {
Expand Down Expand Up @@ -657,26 +659,28 @@ private static File getConfigDropinsDir() {
private static void parseConfigDropinsDirVariables(String inDir)
throws XPathExpressionException, SAXException, IOException {
File configDropins = getConfigDropinsDir();
if (configDropins == null || !configDropins.exists()) {
return;
}

if (configDropins != null && configDropins.exists()) {
File dir = new File(configDropins, inDir);

if (dir.exists()) {
File[] cfgFiles = dir.listFiles();
File dir = new File(configDropins, inDir);
if (!dir.exists()) {
return;
}

for (int i = 0; i < cfgFiles.length; i++) {
if (cfgFiles[i].isFile()) {
parseDropinsFilesVariables(cfgFiles[i]);
}
}
File[] cfgFiles = dir.listFiles();
Arrays.sort(cfgFiles, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
for (File file : cfgFiles) {
if (file.isFile()) {
parseDropinsFilesVariables(file);
}
}
}

private static void parseDropinsFilesVariables(File file)
throws SAXException, IOException, XPathExpressionException {
// get input XML Document
Document doc = parseDropinsXMLFile(file);
Document doc = parseDocument(file);
if (doc != null) {
parseVariablesForBothValues(doc);
parseIncludeVariables(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedActionException;
Expand All @@ -41,14 +38,14 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.StreamSupport;
import java.util.logging.Level;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.NameFileComparator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -249,7 +246,7 @@ private Set<String> getConfigDropinsFeatures(Set<String> origResult, File server
File[] configDropinsXmls = configDropinsFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml") && (dropinsFilesToIgnore == null || !dropinsFilesToIgnore.contains(name));
return name.toLowerCase().endsWith(".xml") && (dropinsFilesToIgnore == null || !dropinsFilesToIgnore.contains(name));
}
});
if (configDropinsXmls == null || configDropinsXmls.length == 0) {
Expand Down Expand Up @@ -487,18 +484,10 @@ private ArrayList<File> parseIncludeFileOrDirectory(String includeFileName, File
}

if (includeFile.isDirectory()) {
try (DirectoryStream<Path> 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((dir, name) -> name.toLowerCase().endsWith(".xml"));
Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
for (File file : files) {
includeFiles.add(file);
}
} else {
includeFiles.add(includeFile);
Expand Down

0 comments on commit 653a3be

Please sign in to comment.