|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.launchscript;
|
18 | 18 |
|
19 |
| -import org.springframework.boot.SpringApplication; |
20 |
| -import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URL; |
| 21 | +import java.security.CodeSource; |
| 22 | +import java.security.ProtectionDomain; |
| 23 | + |
| 24 | +import javax.servlet.ServletException; |
| 25 | +import javax.servlet.http.HttpServlet; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | + |
| 29 | +import org.apache.catalina.Context; |
| 30 | +import org.apache.catalina.LifecycleException; |
| 31 | +import org.apache.catalina.startup.Tomcat; |
21 | 32 |
|
22 |
| -@SpringBootApplication |
23 | 33 | public class LaunchScriptTestApplication {
|
24 | 34 |
|
25 |
| - public static void main(String[] args) { |
26 |
| - SpringApplication.run(LaunchScriptTestApplication.class, args); |
| 35 | + public static void main(String[] args) throws LifecycleException { |
| 36 | + System.out.println("Starting " + LaunchScriptTestApplication.class.getSimpleName() + " (" + findSource() + ")"); |
| 37 | + Tomcat tomcat = new Tomcat(); |
| 38 | + tomcat.getConnector().setPort(getPort(args)); |
| 39 | + Context context = tomcat.addContext(getContextPath(args), null); |
| 40 | + tomcat.addServlet(context.getPath(), "test", new HttpServlet() { |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 44 | + throws ServletException, IOException { |
| 45 | + resp.getWriter().println("Launched"); |
| 46 | + } |
| 47 | + |
| 48 | + }); |
| 49 | + context.addServletMappingDecoded("/", "test"); |
| 50 | + tomcat.start(); |
| 51 | + } |
| 52 | + |
| 53 | + private static URL findSource() { |
| 54 | + try { |
| 55 | + ProtectionDomain domain = LaunchScriptTestApplication.class.getProtectionDomain(); |
| 56 | + CodeSource codeSource = (domain != null) ? domain.getCodeSource() : null; |
| 57 | + return (codeSource != null) ? codeSource.getLocation() : null; |
| 58 | + } |
| 59 | + catch (Exception ex) { |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + private static int getPort(String[] args) { |
| 65 | + String port = getProperty(args, "server.port"); |
| 66 | + return (port != null) ? Integer.parseInt(port) : 8080; |
| 67 | + } |
| 68 | + |
| 69 | + private static String getContextPath(String[] args) { |
| 70 | + String contextPath = getProperty(args, "server.servlet.context-path"); |
| 71 | + return (contextPath != null) ? contextPath : ""; |
| 72 | + } |
| 73 | + |
| 74 | + private static String getProperty(String[] args, String property) { |
| 75 | + String value = System.getProperty(property); |
| 76 | + if (value != null) { |
| 77 | + return value; |
| 78 | + } |
| 79 | + String prefix = "--" + property + "="; |
| 80 | + for (String arg : args) { |
| 81 | + if (arg.startsWith(prefix)) { |
| 82 | + return arg.substring(prefix.length()); |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
27 | 86 | }
|
28 | 87 |
|
29 | 88 | }
|
0 commit comments