1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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      // new ConfigFileServlet()
40      public void init() throws Exception {
41          Server server = new Server(port);
42  
43          // Create a handler for the root context
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          // Set the handler for the server
51          server.setHandler(context);
52  
53          System.out.println("Starting jetty server on port: " + port);
54          // Start the server
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  }