Skip to content

Commit bfee217

Browse files
committed
Detect path of exploded war correctly on Windows
Previously, AbstractEmbeddedServletContainerFactory detected an exploded war by looking for `/WEB-INF/` in the path of its code source's location. This failed on Windows due to the use of `\` rather than `/` separators. This commit updates AbstractEmbeddedServletContainerFactory to uses the OS's separator rather than hardcoding `/`. Closes gh-8100
1 parent 265a712 commit bfee217

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,15 +83,21 @@ else if (this.logger.isDebugEnabled()) {
8383
}
8484

8585
private File getExplodedWarFileDocumentRoot() {
86-
File file = getCodeSourceArchive();
86+
return getExplodedWarFileDocumentRoot(getCodeSourceArchive());
87+
}
88+
89+
File getExplodedWarFileDocumentRoot(File codeSourceFile) {
8790
if (this.logger.isDebugEnabled()) {
88-
this.logger.debug("Code archive: " + file);
91+
this.logger.debug("Code archive: " + codeSourceFile);
8992
}
90-
if (file != null && file.exists()
91-
&& file.getAbsolutePath().contains("/WEB-INF/")) {
92-
String path = file.getAbsolutePath();
93-
path = path.substring(0, path.indexOf("/WEB-INF/"));
94-
return new File(path);
93+
if (codeSourceFile != null && codeSourceFile.exists()) {
94+
String path = codeSourceFile.getAbsolutePath();
95+
int webInfPathIndex = path
96+
.indexOf(File.separatorChar + "WEB-INF" + File.separatorChar);
97+
if (webInfPathIndex >= 0) {
98+
path = path.substring(0, webInfPathIndex);
99+
return new File(path);
100+
}
95101
}
96102
return null;
97103
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -915,6 +915,24 @@ public void localeCharsetMappingsAreConfigured() throws Exception {
915915
assertThat(getCharset(Locale.ITALIAN)).isNull();
916916
}
917917

918+
@Test
919+
public void explodedWarFileDocumentRootWhenRunningFromExplodedWar() throws Exception {
920+
AbstractEmbeddedServletContainerFactory factory = getFactory();
921+
File webInfClasses = this.temporaryFolder.newFolder("test.war", "WEB-INF", "lib",
922+
"spring-boot.jar");
923+
File documentRoot = factory.getExplodedWarFileDocumentRoot(webInfClasses);
924+
assertThat(documentRoot)
925+
.isEqualTo(webInfClasses.getParentFile().getParentFile().getParentFile());
926+
}
927+
928+
@Test
929+
public void explodedWarFileDocumentRootWhenRunningFromPackagedWar() throws Exception {
930+
AbstractEmbeddedServletContainerFactory factory = getFactory();
931+
File codeSourceFile = this.temporaryFolder.newFile("test.war");
932+
File documentRoot = factory.getExplodedWarFileDocumentRoot(codeSourceFile);
933+
assertThat(documentRoot).isNull();
934+
}
935+
918936
protected abstract void addConnector(int port,
919937
AbstractEmbeddedServletContainerFactory factory);
920938

0 commit comments

Comments
 (0)