Skip to content

Commit a20a35e

Browse files
author
Mahdi
committed
Fix tomcat module
1 parent 05c9fcd commit a20a35e

7 files changed

Lines changed: 60 additions & 93 deletions

File tree

01-basic/src/main/java/ir/moke/module/basic/LogService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
public class LogService {
77
private static final Logger logger = LoggerFactory.getLogger(LogService.class);
88

9-
public static void generateLog() {
10-
while (true) {
9+
public static void generateLog(boolean enable) {
10+
while (enable) {
1111
sleep();
1212
sendInfoLog();
1313
sendWarnLog();
@@ -17,23 +17,23 @@ public static void generateLog() {
1717
}
1818
}
1919

20-
public static void sendInfoLog() {
20+
private static void sendInfoLog() {
2121
logger.info("This is info log");
2222
}
2323

24-
public static void sendDebugLog() {
24+
private static void sendDebugLog() {
2525
logger.debug("This is debug log");
2626
}
2727

28-
public static void sendWarnLog() {
28+
private static void sendWarnLog() {
2929
logger.warn("This is warn log");
3030
}
3131

32-
public static void sendErrorLog() {
32+
private static void sendErrorLog() {
3333
logger.error("This is error log");
3434
}
3535

36-
public static void sendTraceLog() {
36+
private static void sendTraceLog() {
3737
logger.trace("This is trace log");
3838
}
3939

01-basic/src/main/java/ir/moke/module/basic/ModuleRunner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import ir.moke.jos.api.IModule;
44

55
public class ModuleRunner implements IModule {
6+
private static boolean enable = false;
67

78
@Override
89
public void start() {
9-
LogService.generateLog();
10+
enable = true;
11+
LogService.generateLog(enable);
1012
}
1113

1214
@Override
1315
public void stop() {
14-
// In this module is not usable !
16+
enable = false;
1517
}
1618
}

05-apache-tomcat/README.md

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

05-apache-tomcat/pom.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727

2828
<!-- Tomcat -->
2929
<dependency>
30-
<groupId>org.apache.tomcat</groupId>
31-
<artifactId>tomcat-catalina</artifactId>
32-
<version>10.1.24</version>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.apache.tomcat</groupId>
36-
<artifactId>tomcat-jasper</artifactId>
30+
<groupId>org.apache.tomcat.embed</groupId>
31+
<artifactId>tomcat-embed-core</artifactId>
3732
<version>10.1.24</version>
33+
<exclusions>
34+
<exclusion>
35+
<groupId>org.slf4j</groupId>
36+
<artifactId>slf4j-api</artifactId>
37+
</exclusion>
38+
</exclusions>
3839
</dependency>
3940
</dependencies>
4041

05-apache-tomcat/src/main/java/ir/moke/module/tomcat/HttpContainer.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import ir.moke.module.tomcat.servlet.HelloServlet;
44
import org.apache.catalina.Context;
5+
import org.apache.catalina.connector.Connector;
56
import org.apache.catalina.startup.Tomcat;
6-
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

@@ -17,30 +17,46 @@
1717
public class HttpContainer {
1818
public static final HttpContainer instance = new HttpContainer();
1919
private static final Logger logger = LoggerFactory.getLogger(HttpContainer.class);
20-
private static final String contextPath = "/api";
2120
private static final String host = System.getenv("HTTP_SERVER_HOST");
2221
private static final String port = System.getenv("HTTP_SERVER_PORT");
23-
private static final String appBase = "/tmp/tomcat";
22+
private static final String contextPath = "";
23+
private static final String baseDir = "/tmp/tomcat";
2424
private static Tomcat tomcat;
25+
private static Connector connector;
2526

2627
private HttpContainer() {
2728
createAppBaseDirectory();
28-
TomcatURLStreamHandlerFactory.disable();
29+
}
30+
31+
private static void init() {
2932
tomcat = new Tomcat();
3033
tomcat.setHostname(Optional.ofNullable(host).orElse("0.0.0.0"));
3134
tomcat.setPort(Integer.parseInt(Optional.ofNullable(port).orElse("8080")));
32-
tomcat.setBaseDir(appBase);
35+
tomcat.setBaseDir(baseDir);
36+
connector = tomcat.getConnector();
3337

34-
Context context = tomcat.addWebapp(contextPath, appBase);
38+
Context context = tomcat.addContext(contextPath, baseDir);
3539

3640
tomcat.addServlet(contextPath, "hello", new HelloServlet());
3741
context.addServletMappingDecoded("/hello", "hello");
3842
}
3943

44+
private void createAppBaseDirectory() {
45+
try {
46+
Path appBasePath = Path.of(baseDir);
47+
if (!Files.exists(appBasePath)) {
48+
Files.createDirectory(appBasePath);
49+
}
50+
} catch (IOException e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
4055
public void start() {
4156
try {
42-
tomcat.getConnector();
57+
init();
4358
tomcat.start();
59+
tomcat.getService().addConnector(connector);
4460
logger.info("Tomcat server started");
4561
tomcat.getServer().await();
4662
} catch (Exception e) {
@@ -49,26 +65,18 @@ public void start() {
4965
}
5066

5167
public void stop() {
52-
try (Stream<Path> pathStream = Files.walk(Path.of(appBase))) {
68+
try (Stream<Path> pathStream = Files.walk(Path.of(baseDir))) {
69+
connector.stop();
70+
tomcat.getService().removeConnector(connector);
71+
tomcat.stop();
72+
tomcat.destroy();
73+
5374
pathStream.sorted(Comparator.reverseOrder())
5475
.map(Path::toFile)
5576
.forEach(java.io.File::delete);
56-
57-
tomcat.stop();
5877
logger.info("Tomcat server stopped");
5978
} catch (Exception e) {
6079
logger.error("Tomcat error", e);
6180
}
6281
}
63-
64-
private void createAppBaseDirectory() {
65-
try {
66-
Path appBasePath = Path.of(appBase);
67-
if (!Files.exists(appBasePath)) {
68-
Files.createDirectory(appBasePath);
69-
}
70-
} catch (IOException e) {
71-
throw new RuntimeException(e);
72-
}
73-
}
7482
}

05-apache-tomcat/src/main/java/ir/moke/module/tomcat/ModuleRunner.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7+
import java.util.Properties;
8+
79
public class ModuleRunner implements IModule {
810
private static final Logger logger = LoggerFactory.getLogger(ModuleRunner.class);
11+
private static final Properties sysProps = System.getProperties();
912

1013
@Override
1114
public void start() {
15+
/*
16+
* Only for bypass FUCKING org.apache.catalina.loader.WebappClassLoaderBase#clearReferencesJdbc
17+
* somebody tell to tomcat team to fix this fucking NPE bug .
18+
* */
19+
sysProps.setProperty("org.graalvm.nativeimage.imagecode", "runtime");
20+
21+
1222
logger.info("Start module");
1323
HttpContainer.instance.start();
1424
}
@@ -17,5 +27,6 @@ public void start() {
1727
public void stop() {
1828
logger.info("Stop module");
1929
HttpContainer.instance.stop();
30+
sysProps.remove("org.graalvm.nativeimage.imagecode");
2031
}
2132
}

05-apache-tomcat/src/main/java/module-info.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module p05_apache_tomcat {
22
requires org.slf4j;
33
requires jos.api;
4-
requires jakarta.servlet;
5-
requires org.apache.tomcat.jasper;
6-
requires org.apache.tomcat.catalina;
4+
requires org.apache.tomcat.embed.core;
75
exports ir.moke.module.tomcat.servlet;
86

97
provides ir.moke.jos.api.IModule with ir.moke.module.tomcat.ModuleRunner;

0 commit comments

Comments
 (0)