|
| 1 | +package io.github.dunwu.javatool.server; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.apache.catalina.Server; |
| 7 | +import org.apache.catalina.startup.Catalina; |
| 8 | +import org.apache.catalina.startup.Tomcat; |
| 9 | +import org.apache.tomcat.util.digester.Digester; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +/** |
| 14 | + * 嵌入式 Tomcat 启动类 |
| 15 | + * 启动后可访问 http://localhost:8080/javatool-server/ |
| 16 | + * @author Zhang Peng |
| 17 | + */ |
| 18 | +public class TomcatServer { |
| 19 | + |
| 20 | + private static final String PORT = "8080"; |
| 21 | + // private static final String RELATIVE_DUBBO_RESOVE_FILE = |
| 22 | + // "src/main/resources/properties/dubbo-resolve.properties"; |
| 23 | + private static final String RELATIVE_BASE_DIR = "src/main/resources/tomcat/"; |
| 24 | + |
| 25 | + /** |
| 26 | + * 除了 spring.profiles.active,System.setProperty 设置的属性都是为了配置 server.xml |
| 27 | + */ |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + // 设定 Spring 的 profile |
| 30 | + Optional<String> profile = Optional.ofNullable(System.getProperty("spring.profiles.active")); |
| 31 | + System.setProperty("spring.profiles.active", profile.orElse("develop")); |
| 32 | + |
| 33 | + // 设定 Tomcat 的 catalina.base |
| 34 | + System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR); |
| 35 | + |
| 36 | + // 设定 Tomcat 的 port |
| 37 | + Optional<String> port = Optional.ofNullable(System.getProperty("tomcat.connector.port")); |
| 38 | + System.setProperty("tomcat.connector.port", port.orElse(PORT)); |
| 39 | + |
| 40 | + ExtendedTomcat tomcat = new ExtendedTomcat(); |
| 41 | + // 开启JNDI,注意maven必须添加tomcat-dbcp依赖 |
| 42 | + // tomcat.enableNaming(); |
| 43 | + tomcat.start(); |
| 44 | + tomcat.getServer().await(); |
| 45 | + } |
| 46 | + |
| 47 | + private static String getAbsolutePath() { |
| 48 | + String path = null; |
| 49 | + String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath() |
| 50 | + .substring(1); |
| 51 | + if (folderPath.indexOf("target") > 0) { |
| 52 | + path = folderPath.substring(0, folderPath.indexOf("target")); |
| 53 | + } |
| 54 | + return path; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Tomcat 扩展类,添加功能是启动时,加载 src/main/resources/tomcat/conf/server.xml 文件中配置,而非默认配置。 |
| 60 | + */ |
| 61 | +class ExtendedTomcat extends Tomcat { |
| 62 | + |
| 63 | + private Logger log = LoggerFactory.getLogger(this.getClass()); |
| 64 | + |
| 65 | + private static final String RELATIVE_SERVERXML_PATH = "/conf/server.xml"; |
| 66 | + |
| 67 | + private static class ExtendedCatalina extends Catalina { |
| 68 | + @Override |
| 69 | + public Digester createStartDigester() { |
| 70 | + return super.createStartDigester(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Server getServer() { |
| 76 | + if (server != null) { |
| 77 | + return server; |
| 78 | + } |
| 79 | + ExtendedCatalina extendedCatalina = new ExtendedCatalina(); |
| 80 | + Digester digester = extendedCatalina.createStartDigester(); |
| 81 | + digester.push(extendedCatalina); |
| 82 | + try { |
| 83 | + server = ((ExtendedCatalina) digester |
| 84 | + .parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer(); |
| 85 | + this.initBaseDir(); |
| 86 | + } catch (Exception e) { |
| 87 | + log.error("Error while parsing server.xml", e); |
| 88 | + server = null; |
| 89 | + } finally { |
| 90 | + return server; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments