1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.blackbox.joran.spi;
16
17 import jakarta.servlet.http.HttpServlet;
18 import org.eclipse.jetty.server.Server;
19 import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
20 import org.eclipse.jetty.ee10.servlet.ServletHolder;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 public class ConfigEmbeddedJetty {
26
27 int port;
28 Server server = new Server(port);
29 Map<String, HttpServlet> servletPathMap = new HashMap<>();
30
31 public ConfigEmbeddedJetty(int port) {
32 this.port = port;
33 }
34
35 public Map<String, HttpServlet> getServletMap() {
36 return servletPathMap;
37 }
38
39
40 public void init() throws Exception {
41 Server server = new Server(port);
42
43
44 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
45 context.setContextPath("/");
46
47
48 servletPathMap.forEach( (path, servlet) -> context.addServlet(new ServletHolder(servlet), path));
49
50
51 server.setHandler(context);
52
53 System.out.println("Starting jetty server on port: " + port);
54
55 server.start();
56
57 System.out.println("After Jetty server start(). Joining");
58
59 while(!server.isStarted()) {
60 Thread.sleep(10);
61 }
62 System.out.println("Jetty server started");
63 }
64
65 public void stop() throws Exception {
66 server.stop();
67 }
68 }