From 35b16ea04ebb92f319d00f96389369a06bfbae14 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 1 Oct 2021 14:15:52 +0100 Subject: [PATCH] Minimize dependencies of launch script test app Closes gh-28164 --- .../build.gradle | 1 - .../build.gradle | 2 +- .../LaunchScriptTestApplication.java | 71 +++++++++++++++++-- .../LaunchVerificationController.java | 30 -------- 4 files changed, 66 insertions(+), 38 deletions(-) delete mode 100644 spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchVerificationController.java diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle index dd9b806a065..b94084dc0a7 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle @@ -17,7 +17,6 @@ configurations { dependencies { app project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository") app project(path: ":spring-boot-project:spring-boot-parent", configuration: "mavenRepository") - app project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-web", configuration: "mavenRepository") app project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "mavenRepository") intTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent"))) diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/build.gradle b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/build.gradle index 7ab763fe565..4c842b1e1c6 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/build.gradle +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/build.gradle @@ -13,7 +13,7 @@ repositories { } dependencies { - implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.apache.tomcat.embed:tomcat-embed-core") } bootJar { diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchScriptTestApplication.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchScriptTestApplication.java index a5d87e0afe2..8bf6791404d 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchScriptTestApplication.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchScriptTestApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -16,14 +16,73 @@ package org.springframework.boot.launchscript; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import java.io.IOException; +import java.net.URL; +import java.security.CodeSource; +import java.security.ProtectionDomain; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.startup.Tomcat; -@SpringBootApplication public class LaunchScriptTestApplication { - public static void main(String[] args) { - SpringApplication.run(LaunchScriptTestApplication.class, args); + public static void main(String[] args) throws LifecycleException { + System.out.println("Starting " + LaunchScriptTestApplication.class.getSimpleName() + " (" + findSource() + ")"); + Tomcat tomcat = new Tomcat(); + tomcat.getConnector().setPort(getPort(args)); + Context context = tomcat.addContext(getContextPath(args), null); + tomcat.addServlet(context.getPath(), "test", new HttpServlet() { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + resp.getWriter().println("Launched"); + } + + }); + context.addServletMappingDecoded("/", "test"); + tomcat.start(); + } + + private static URL findSource() { + try { + ProtectionDomain domain = LaunchScriptTestApplication.class.getProtectionDomain(); + CodeSource codeSource = (domain != null) ? domain.getCodeSource() : null; + return (codeSource != null) ? codeSource.getLocation() : null; + } + catch (Exception ex) { + } + return null; + } + + private static int getPort(String[] args) { + String port = getProperty(args, "server.port"); + return (port != null) ? Integer.parseInt(port) : 8080; + } + + private static String getContextPath(String[] args) { + String contextPath = getProperty(args, "server.servlet.context-path"); + return (contextPath != null) ? contextPath : ""; + } + + private static String getProperty(String[] args, String property) { + String value = System.getProperty(property); + if (value != null) { + return value; + } + String prefix = "--" + property + "="; + for (String arg : args) { + if (arg.startsWith(prefix)) { + return arg.substring(prefix.length()); + } + } + return null; } } diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchVerificationController.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchVerificationController.java deleted file mode 100644 index 67d08ccb055..00000000000 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/spring-boot-launch-script-tests-app/src/main/java/org/springframework/boot/launchscript/LaunchVerificationController.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012-2020 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.launchscript; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class LaunchVerificationController { - - @RequestMapping("/") - public String verifyLaunch() { - return "Launched\n"; - } - -}