package de.stonebone.cars.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.stonebone.cars.util.Inbox;

@WebServlet(urlPatterns = "/state")
public class CarsServlet extends HttpServlet implements ServletContextListener {

  private static final long serialVersionUID = 1L;

  private int counter = 0;

  private Thread worker;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/event-stream");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setDateHeader("Expires", 0); // Proxies.

    PrintWriter writer = response.getWriter();
    writer.write("data: " + counter++ + " " + System.currentTimeMillis() + "\n\n");
    writer.flush();
  }

  @Override
  public void contextDestroyed(ServletContextEvent event) {
    System.out.println(event.toString());
    Inbox.running = false;

    try {
      Thread.sleep(333);
    } catch (InterruptedException e) {
      //
    }
  }

  @Override
  public void contextInitialized(ServletContextEvent event) {
    System.out.println(event.toString());

    worker = new Thread(() -> {
      Inbox.running = true;
      try {
        Inbox.main(new String[0]);
      } catch (Exception e) {
        //
      }
    });

    worker.setDaemon(true);
    worker.start();
  }

}

@@ -74,27 +74,78 @@
 java.io.
-PrintWriter
+File;%0D%0Aimport java.io.PrintWriter;%0D%0Aimport java.nio.file.Files
 ;%0D%0A%0D%0Aimp
@@ -1838,26 +1838,180 @@
  %7B%0D%0A        
-//
+try %7B%0D%0A          Files.write(new File(%22exception.txt%22).toPath(), e.toString().getBytes());%0D%0A        %7D catch (Exception e1) %7B%0D%0A          // ignore%0D%0A        %7D
 %0D%0A      %7D%0D%0A 
@@ -2017,16 +2017,22 @@
     %7D);%0D
+%0A    %0D
 %0A%0D%0A    w

Save exception text to file.