Skip to content

Commit 016695f

Browse files
authored
Merge pull request #217 from evolvedbinary/6.x.x/hotfix/config-windows-paths
[6.x.x] Fix some small issues with config paths on Windows
2 parents ec385f3 + 79ed46c commit 016695f

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

  • exist-start/src/main/java/org/exist/start

exist-start/src/main/java/org/exist/start/Main.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ public void runEx(String[] args) throws StartException {
279279
if (existHomeDir.isPresent() && Files.exists(existHomeDir.get().resolve(CONFIG_DIR_NAME))) {
280280
log4jConfigurationFile = existHomeDir.map(f -> f.resolve(CONFIG_DIR_NAME).resolve("log4j2.xml"));
281281
}
282+
}
282283

283-
if (log4jConfigurationFile.isPresent() && Files.isReadable(log4jConfigurationFile.get())) {
284-
// System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toUri().toASCIIString());
285-
System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toAbsolutePath().toString());
286-
}
284+
// Always normalise to a file:/ URI so log4j can resolve it on all platforms (especially Windows)
285+
if (log4jConfigurationFile.isPresent() && Files.isReadable(log4jConfigurationFile.get())) {
286+
System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toAbsolutePath().toUri().toString());
287287
}
288288

289289
if (log4jConfigurationFile.isPresent()) {
@@ -312,6 +312,7 @@ public void runEx(String[] args) throws StartException {
312312
Thread.currentThread().setContextClassLoader(cl);
313313

314314
// Invoke main class using new classloader.
315+
args = cleanupArgs(args);
315316
try {
316317
invokeMain(cl, _classname, args);
317318
} catch (final Exception e) {
@@ -320,6 +321,34 @@ public void runEx(String[] args) throws StartException {
320321
}
321322
}
322323

324+
public static String[] cleanupArgs(final String[] args) {
325+
if (args == null) {
326+
return null;
327+
}
328+
if (args.length == 0) {
329+
return args;
330+
}
331+
332+
final String[] nargs = new String[args.length];
333+
for (int i = 0; i < args.length; i++) {
334+
String arg = args[i];
335+
336+
if (arg.startsWith("\"") && arg.indexOf('\"', 1) > -1 && (arg.endsWith("conf.xml") || arg.endsWith("log4j2.xml"))) {
337+
// String that starts with quote, has a quote somewhere in it, and ends with conf.xml or log4j2.xml - such invalid strings can be produced by startup.bat on Windows
338+
arg = arg.replaceAll("\"", "");
339+
}
340+
341+
if (arg.indexOf('\\') != -1 && arg.indexOf('\\') < arg.indexOf('/') && (arg.endsWith("conf.xml") || arg.endsWith("log4j2.xml"))) {
342+
// String that contains both '\' path separators followed by `/` path separators, and ends with conf.xml or log4j2.xml - such invalid strings can be produced by startup.bat on Windows
343+
arg = arg.replaceAll("/", "\\\\");
344+
}
345+
346+
nargs[i] = arg;
347+
}
348+
349+
return nargs;
350+
}
351+
323352
private Optional<String> getFromSysPropOrEnv(final String sysPropName, final String envVarName) {
324353
Optional<String> value = Optional.ofNullable(System.getProperty(sysPropName));
325354
if (!value.isPresent()) {

0 commit comments

Comments
 (0)