|
1 | 1 | package io.github.dunwu.javatool.server; |
2 | 2 |
|
3 | 3 | import java.io.File; |
4 | | -import java.util.Optional; |
5 | 4 |
|
6 | 5 | import org.apache.catalina.Server; |
7 | 6 | import org.apache.catalina.startup.Catalina; |
8 | 7 | import org.apache.catalina.startup.Tomcat; |
9 | 8 | import org.apache.tomcat.util.digester.Digester; |
| 9 | +import org.apache.tomcat.util.scan.Constants; |
10 | 10 | import org.slf4j.Logger; |
11 | 11 | import org.slf4j.LoggerFactory; |
| 12 | +import org.springframework.util.StringUtils; |
12 | 13 |
|
13 | 14 | /** |
14 | | - * 嵌入式 Tomcat 启动类 |
15 | | - * 启动后可访问 http://localhost:8080/javatool-server/ |
| 15 | + * 嵌入式 Tomcat 启动类 启动后可访问 http://localhost:8080/javatool-server/ |
16 | 16 | * @author Zhang Peng |
17 | 17 | */ |
18 | 18 | public class TomcatServer { |
19 | 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/"; |
| 20 | + private static final Logger log = LoggerFactory.getLogger(TomcatServer.class); |
| 21 | + |
| 22 | + private static final String CONNECTOR_PORT = "8080"; |
| 23 | + private static final String RELATIVE_DEV_DUBBO_RESOVE_FILE = "src/main/resources/properties/dubbo-resolve.properties"; |
| 24 | + private static final String RELATIVE_DUBBO_RESOVE_FILE = "WEB-INF/classes/properties/dubbo-resolve.properties"; |
| 25 | + |
| 26 | + // 以下设置轻易不要改动 |
| 27 | + private static final String RELATIVE_DEV_BASE_DIR = "src/main/resources/tomcat/"; |
| 28 | + private static final String RELATIVE_BASE_DIR = "WEB-INF/classes/tomcat/"; |
| 29 | + private static final String RELATIVE_DEV_DOCBASE_DIR = "src/main/webapp"; |
| 30 | + private static final String RELATIVE_DOCBASE_DIR = "./"; |
24 | 31 |
|
25 | 32 | /** |
26 | 33 | * 除了 spring.profiles.active,System.setProperty 设置的属性都是为了配置 server.xml |
27 | 34 | */ |
28 | 35 | 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")); |
| 36 | + // 设定Spring的profile |
| 37 | + if (StringUtils.isEmpty(System.getProperty("spring.profiles.active"))) { |
| 38 | + System.setProperty("spring.profiles.active", "develop"); |
| 39 | + } |
32 | 40 |
|
33 | | - // 设定 Tomcat 的 catalina.base |
34 | | - System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR); |
| 41 | + System.setProperty("tomcat.host.appBase", getAbsolutePath()); |
| 42 | + File checkFile = new File(System.getProperty("tomcat.host.appBase") + "/WEB-INF"); |
| 43 | + if (!checkFile.exists()) { |
| 44 | + System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_DEV_BASE_DIR); |
| 45 | + System.setProperty("tomcat.context.docBase", RELATIVE_DEV_DOCBASE_DIR); |
| 46 | + System.setProperty("dubbo.resolve.file", getAbsolutePath() + RELATIVE_DEV_DUBBO_RESOVE_FILE); |
| 47 | + } else { |
| 48 | + System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR); |
| 49 | + System.setProperty("tomcat.context.docBase", RELATIVE_DOCBASE_DIR); |
| 50 | + if ("develop".equalsIgnoreCase(System.getProperty("spring.profiles.active")) |
| 51 | + || "test".equalsIgnoreCase("spring.profiles.active")) { |
| 52 | + System.setProperty("dubbo.resolve.file", getAbsolutePath() + RELATIVE_DUBBO_RESOVE_FILE); |
| 53 | + } |
| 54 | + } |
35 | 55 |
|
36 | | - // 设定 Tomcat 的 port |
37 | | - Optional<String> port = Optional.ofNullable(System.getProperty("tomcat.connector.port")); |
38 | | - System.setProperty("tomcat.connector.port", port.orElse(PORT)); |
| 56 | + if (StringUtils.isEmpty(System.getProperty("tomcat.connector.port"))) { |
| 57 | + System.setProperty("tomcat.connector.port", CONNECTOR_PORT); |
| 58 | + } |
| 59 | + if (StringUtils.isEmpty(System.getProperty("tomcat.server.shutdownPort"))) { |
| 60 | + System.setProperty("tomcat.server.shutdownPort", |
| 61 | + String.valueOf(Integer.valueOf(System.getProperty("tomcat.connector.port")) + 10000)); |
| 62 | + } |
| 63 | + |
| 64 | + log.info("====================ENV setting===================="); |
| 65 | + log.info("spring.profiles.active:" + System.getProperty("spring.profiles.active")); |
| 66 | + log.info("dubbo.resolve.file:" + System.getProperty("dubbo.resolve.file")); |
| 67 | + log.info("catalina.base:" + System.getProperty("catalina.base")); |
| 68 | + log.info("tomcat.host.appBase:" + System.getProperty("tomcat.host.appBase")); |
| 69 | + log.info("tomcat.context.docBase:" + System.getProperty("tomcat.context.docBase")); |
| 70 | + log.info("tomcat.connector.port:" + System.getProperty("tomcat.connector.port")); |
| 71 | + log.info("tomcat.server.shutdownPort:" + System.getProperty("tomcat.server.shutdownPort")); |
39 | 72 |
|
40 | 73 | ExtendedTomcat tomcat = new ExtendedTomcat(); |
41 | | - // 开启JNDI,注意maven必须添加tomcat-dbcp依赖 |
42 | | - // tomcat.enableNaming(); |
43 | 74 | tomcat.start(); |
44 | 75 | tomcat.getServer().await(); |
45 | 76 | } |
46 | 77 |
|
47 | 78 | private static String getAbsolutePath() { |
48 | 79 | String path = null; |
49 | | - String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath() |
50 | | - .substring(1); |
51 | | - if (folderPath.indexOf("target") > 0) { |
| 80 | + String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
| 81 | + if (folderPath.indexOf("WEB-INF") > 0) { |
| 82 | + path = folderPath.substring(0, folderPath.indexOf("WEB-INF")); |
| 83 | + } else if (folderPath.indexOf("target") > 0) { |
52 | 84 | path = folderPath.substring(0, folderPath.indexOf("target")); |
53 | 85 | } |
54 | 86 | return path; |
@@ -76,18 +108,26 @@ public Server getServer() { |
76 | 108 | if (server != null) { |
77 | 109 | return server; |
78 | 110 | } |
| 111 | + // 默认不开启JNDI. 开启时, 注意maven必须添加tomcat-dbcp依赖 |
| 112 | + System.setProperty("catalina.useNaming", "false"); |
79 | 113 | ExtendedCatalina extendedCatalina = new ExtendedCatalina(); |
| 114 | + |
| 115 | + // 覆盖默认的skip和scan jar包配置 |
| 116 | + System.setProperty(Constants.SKIP_JARS_PROPERTY, ""); |
| 117 | + System.setProperty(Constants.SCAN_JARS_PROPERTY, ""); |
| 118 | + |
80 | 119 | Digester digester = extendedCatalina.createStartDigester(); |
81 | 120 | digester.push(extendedCatalina); |
82 | 121 | try { |
83 | 122 | server = ((ExtendedCatalina) digester |
84 | 123 | .parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer(); |
| 124 | + // 设置catalina.base和catalna.home |
85 | 125 | this.initBaseDir(); |
| 126 | + return server; |
86 | 127 | } catch (Exception e) { |
87 | 128 | log.error("Error while parsing server.xml", e); |
88 | | - server = null; |
89 | | - } finally { |
90 | | - return server; |
| 129 | + throw new RuntimeException("server未创建,请检查server.xml(路径:" + System.getProperty("catalina.base") |
| 130 | + + RELATIVE_SERVERXML_PATH + ")配置是否正确"); |
91 | 131 | } |
92 | 132 | } |
93 | 133 |
|
|
0 commit comments