Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-41 servlet: use runtime class loader instead of system one to load… #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract class Builder {
public static interface BuilderContext {
ClassOrInterfaceDeclaration self();

ClassLoader classLoader();

Descriptor descriptor();

TypeCache type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public ClassOrInterfaceDeclaration self() {
return cl;
}

@Override
public ClassLoader classLoader() {
return classLoader;
}

@Override
public Descriptor descriptor() {
return descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.javaparser.ast.stmt.BlockStmt;

import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletException;
import unknow.server.maven.Utils;
import unknow.server.maven.servlet.Builder;

Expand All @@ -34,7 +35,7 @@ public class LoadInitializer extends Builder {
public void add(BuilderContext ctx) {
List<String> clazz = new ArrayList<>(ctx.descriptor().initializer);
try {
Enumeration<URL> e = ClassLoader.getSystemResources("/META-INF/services/" + ServletContainerInitializer.class.getName());
Enumeration<URL> e = ctx.classLoader().getResources("META-INF/services/" + ServletContainerInitializer.class.getName());
while (e.hasMoreElements()) {
URL nextElement = e.nextElement();
try (BufferedReader br = new BufferedReader(new InputStreamReader(nextElement.openStream()))) {
Expand All @@ -46,7 +47,8 @@ public void add(BuilderContext ctx) {
} catch (Exception ex) {
logger.warn("Failed to process ServletContainerInitializer", ex);
}
BlockStmt b = ctx.self().addMethod("loadInitializer", Keyword.PROTECTED, Keyword.FINAL).addMarkerAnnotation(Override.class).createBody();
BlockStmt b = ctx.self().addMethod("loadInitializer", Keyword.PROTECTED, Keyword.FINAL).addThrownException(ctx.type().getClass(ServletException.class))
.addMarkerAnnotation(Override.class).createBody();
for (String s : clazz)
b.addStatement(new MethodCallExpr(new ObjectCreationExpr(null, ctx.type().getClass(s), Utils.list()), "onStartup",
Utils.list(new NullLiteralExpr(), new NameExpr("ctx"))));
Expand Down