diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java index e870db76383..b2c14c9be6a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,15 +83,21 @@ public abstract class AbstractEmbeddedServletContainerFactory } private File getExplodedWarFileDocumentRoot() { - File file = getCodeSourceArchive(); + return getExplodedWarFileDocumentRoot(getCodeSourceArchive()); + } + + File getExplodedWarFileDocumentRoot(File codeSourceFile) { if (this.logger.isDebugEnabled()) { - this.logger.debug("Code archive: " + file); + this.logger.debug("Code archive: " + codeSourceFile); } - if (file != null && file.exists() - && file.getAbsolutePath().contains("/WEB-INF/")) { - String path = file.getAbsolutePath(); - path = path.substring(0, path.indexOf("/WEB-INF/")); - return new File(path); + if (codeSourceFile != null && codeSourceFile.exists()) { + String path = codeSourceFile.getAbsolutePath(); + int webInfPathIndex = path + .indexOf(File.separatorChar + "WEB-INF" + File.separatorChar); + if (webInfPathIndex >= 0) { + path = path.substring(0, webInfPathIndex); + return new File(path); + } } return null; } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 5ce1d248e32..55c93af9c7b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -915,6 +915,24 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { assertThat(getCharset(Locale.ITALIAN)).isNull(); } + @Test + public void explodedWarFileDocumentRootWhenRunningFromExplodedWar() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + File webInfClasses = this.temporaryFolder.newFolder("test.war", "WEB-INF", "lib", + "spring-boot.jar"); + File documentRoot = factory.getExplodedWarFileDocumentRoot(webInfClasses); + assertThat(documentRoot) + .isEqualTo(webInfClasses.getParentFile().getParentFile().getParentFile()); + } + + @Test + public void explodedWarFileDocumentRootWhenRunningFromPackagedWar() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + File codeSourceFile = this.temporaryFolder.newFile("test.war"); + File documentRoot = factory.getExplodedWarFileDocumentRoot(codeSourceFile); + assertThat(documentRoot).isNull(); + } + protected abstract void addConnector(int port, AbstractEmbeddedServletContainerFactory factory);