Skip to content

Commit b8103e9

Browse files
authored
Merge pull request #216 from evolvedbinary/7.x.x/hotfix/config-windows-paths
[7.x.x] Fix some small issues with config paths on Windows
2 parents 9721b50 + 2da9127 commit b8103e9

1 file changed

Lines changed: 33 additions & 3 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,11 @@ public void runEx(String[] args) throws StartException {
284284
if (existHomeDir.isPresent() && Files.exists(existHomeDir.get().resolve(CONFIG_DIR_NAME))) {
285285
log4jConfigurationFile = existHomeDir.map(f -> f.resolve(CONFIG_DIR_NAME).resolve("log4j2.xml"));
286286
}
287+
}
287288

288-
if (log4jConfigurationFile.isPresent() && Files.isReadable(log4jConfigurationFile.get())) {
289-
System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toAbsolutePath().toString());
290-
}
289+
// Always normalise to a file:/ URI so log4j can resolve it on all platforms (especially Windows)
290+
if (log4jConfigurationFile.isPresent() && Files.isReadable(log4jConfigurationFile.get())) {
291+
System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toAbsolutePath().toUri().toString());
291292
}
292293

293294
if (log4jConfigurationFile.isPresent()) {
@@ -319,6 +320,7 @@ public void runEx(String[] args) throws StartException {
319320
Thread.currentThread().setContextClassLoader(cl);
320321

321322
// Invoke main class using new classloader.
323+
args = cleanupArgs(args);
322324
try {
323325
invokeMain(cl, _classname, args);
324326
} catch (final Exception e) {
@@ -327,6 +329,34 @@ public void runEx(String[] args) throws StartException {
327329
}
328330
}
329331

332+
public static String[] cleanupArgs(final String[] args) {
333+
if (args == null) {
334+
return null;
335+
}
336+
if (args.length == 0) {
337+
return args;
338+
}
339+
340+
final String[] nargs = new String[args.length];
341+
for (int i = 0; i < args.length; i++) {
342+
String arg = args[i];
343+
344+
if (arg.startsWith("\"") && arg.indexOf('\"', 1) > -1 && (arg.endsWith("conf.xml") || arg.endsWith("log4j2.xml"))) {
345+
// 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
346+
arg = arg.replaceAll("\"", "");
347+
}
348+
349+
if (arg.indexOf('\\') != -1 && arg.indexOf('\\') < arg.indexOf('/') && (arg.endsWith("conf.xml") || arg.endsWith("log4j2.xml"))) {
350+
// 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
351+
arg = arg.replaceAll("/", "\\\\");
352+
}
353+
354+
nargs[i] = arg;
355+
}
356+
357+
return nargs;
358+
}
359+
330360
private Optional<String> getFromSysPropOrEnv(final String sysPropName, final String envVarName) {
331361
Optional<String> value = Optional.ofNullable(System.getProperty(sysPropName));
332362
if (!value.isPresent()) {

0 commit comments

Comments
 (0)