Skip to content

Commit ec0bd45

Browse files
committed
👌 更新嵌入式Tomcat启动类
1 parent 3cafbb8 commit ec0bd45

File tree

4 files changed

+34
-56
lines changed

4 files changed

+34
-56
lines changed

codes/javatool/server/src/main/java/io/github/dunwu/javatool/server/SimpleTomcatServer.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

codes/javatool/server/src/main/java/io/github/dunwu/javatool/server/TomcatServer.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
import org.slf4j.LoggerFactory;
1212
import org.springframework.util.StringUtils;
1313

14-
/**
15-
* 嵌入式 Tomcat 启动类 启动后可访问 http://localhost:8080/javatool-server/
16-
* @author Zhang Peng
17-
*/
1814
public class TomcatServer {
1915

2016
private static final Logger log = LoggerFactory.getLogger(TomcatServer.class);
@@ -29,9 +25,6 @@ public class TomcatServer {
2925
private static final String RELATIVE_DEV_DOCBASE_DIR = "src/main/webapp";
3026
private static final String RELATIVE_DOCBASE_DIR = "./";
3127

32-
/**
33-
* 除了 spring.profiles.active,System.setProperty 设置的属性都是为了配置 server.xml
34-
*/
3528
public static void main(String[] args) throws Exception {
3629
// 设定Spring的profile
3730
if (StringUtils.isEmpty(System.getProperty("spring.profiles.active"))) {
@@ -48,7 +41,7 @@ public static void main(String[] args) throws Exception {
4841
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR);
4942
System.setProperty("tomcat.context.docBase", RELATIVE_DOCBASE_DIR);
5043
if ("develop".equalsIgnoreCase(System.getProperty("spring.profiles.active"))
51-
|| "test".equalsIgnoreCase("spring.profiles.active")) {
44+
|| "test".equalsIgnoreCase("spring.profiles.active")) {
5245
System.setProperty("dubbo.resolve.file", getAbsolutePath() + RELATIVE_DUBBO_RESOVE_FILE);
5346
}
5447
}
@@ -58,7 +51,7 @@ public static void main(String[] args) throws Exception {
5851
}
5952
if (StringUtils.isEmpty(System.getProperty("tomcat.server.shutdownPort"))) {
6053
System.setProperty("tomcat.server.shutdownPort",
61-
String.valueOf(Integer.valueOf(System.getProperty("tomcat.connector.port")) + 10000));
54+
String.valueOf(Integer.valueOf(System.getProperty("tomcat.connector.port")) + 10000));
6255
}
6356

6457
log.info("====================ENV setting====================");
@@ -70,14 +63,16 @@ public static void main(String[] args) throws Exception {
7063
log.info("tomcat.connector.port:" + System.getProperty("tomcat.connector.port"));
7164
log.info("tomcat.server.shutdownPort:" + System.getProperty("tomcat.server.shutdownPort"));
7265

66+
7367
ExtendedTomcat tomcat = new ExtendedTomcat();
7468
tomcat.start();
7569
tomcat.getServer().await();
7670
}
7771

7872
private static String getAbsolutePath() {
7973
String path = null;
80-
String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
74+
String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation()
75+
.getPath();
8176
if (folderPath.indexOf("WEB-INF") > 0) {
8277
path = folderPath.substring(0, folderPath.indexOf("WEB-INF"));
8378
} else if (folderPath.indexOf("target") > 0) {
@@ -87,9 +82,6 @@ private static String getAbsolutePath() {
8782
}
8883
}
8984

90-
/**
91-
* Tomcat 扩展类,添加功能是启动时,加载 src/main/resources/tomcat/conf/server.xml 文件中配置,而非默认配置。
92-
*/
9385
class ExtendedTomcat extends Tomcat {
9486

9587
private Logger log = LoggerFactory.getLogger(this.getClass());
@@ -112,22 +104,22 @@ public Server getServer() {
112104
System.setProperty("catalina.useNaming", "false");
113105
ExtendedCatalina extendedCatalina = new ExtendedCatalina();
114106

115-
// 覆盖默认的skip和scan jar包配置
116-
System.setProperty(Constants.SKIP_JARS_PROPERTY, "");
117-
System.setProperty(Constants.SCAN_JARS_PROPERTY, "");
107+
//覆盖默认的skip和scan jar包配置
108+
System.setProperty(Constants.SKIP_JARS_PROPERTY,"");
109+
System.setProperty(Constants.SCAN_JARS_PROPERTY,"");
118110

119111
Digester digester = extendedCatalina.createStartDigester();
120112
digester.push(extendedCatalina);
121113
try {
122114
server = ((ExtendedCatalina) digester
123-
.parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer();
115+
.parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer();
124116
// 设置catalina.base和catalna.home
125117
this.initBaseDir();
126118
return server;
127119
} catch (Exception e) {
128120
log.error("Error while parsing server.xml", e);
129121
throw new RuntimeException("server未创建,请检查server.xml(路径:" + System.getProperty("catalina.base")
130-
+ RELATIVE_SERVERXML_PATH + ")配置是否正确");
122+
+ RELATIVE_SERVERXML_PATH + ")配置是否正确");
131123
}
132124
}
133125

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# jdbc
2+
jdbc.driver=
3+
jdbc.url=
4+
jdbc.username=
5+
jdbc.password=
6+
7+
# redis
8+
redis.name=
9+
redis.host=
10+
redis.port=
11+
redis.password=
12+
redis.database=
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# jdbc
2+
jdbc.driver=
3+
jdbc.url=
4+
jdbc.username=
5+
jdbc.password=
6+
7+
# redis
8+
redis.name=
9+
redis.host=
10+
redis.port=
11+
redis.password=
12+
redis.database=

0 commit comments

Comments
 (0)