// Path: falchion-container/src/main/java/net/unit8/falchion/api/ListJvmHandler.java
// public class ListJvmHandler extends AbstractApi {
//     public ListJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
//         getContainer().getPool().getActiveProcesses().forEach(p ->
//                 arrayBuilder.add(Json.createObjectBuilder()
//                         .add("id", p.getId())
//                         .add("pid", p.getPid())));
// 
//         sendJson(exchange, arrayBuilder.build());
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/ReadyJvmHandler.java
// public class ReadyJvmHandler extends AbstractApi {
//     private static final Pattern RE_ID = Pattern.compile("/([a-zA-Z0-9]+)/ready$");
// 
//     public ReadyJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
//         try {
//             if (m.find()) {
//                 String id = m.group(1);
//                 JvmProcess process = getContainer()
//                         .getPool()
//                         .getProcessByPid(Long.parseLong(id));
//                 process.ready();
// 
//                 sendNoContent(exchange);
//             } else {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//             }
//         } catch (Exception ex) {
//             sendBadRequest(exchange, exchange.getRequestURI().getPath());
//         }
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/RefreshContainerHandler.java
// public class RefreshContainerHandler extends AbstractApi {
//     private static final Logger LOG = LoggerFactory.getLogger(RefreshContainerHandler.class);
//     private static final Pattern RE_ID = Pattern.compile("/refresh/([a-zA-Z0-9.-]+)$");
// 
//     public RefreshContainerHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
//         Container container = getContainer();
//         JvmPool pool = container.getPool();
//         if (m.find()) {
//             String version = m.group(1);
//             String classpath = container.createClasspath(container.getBasedir(), version);
//             if (Objects.equals(classpath, container.getBasedir())) {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//                 return;
//             }
//             pool.setClasspath(classpath);
//             LOG.info("The version of the application has been changed. New version is '{}'", version);
//         }
//         pool.refresh();
//         sendNoContent(exchange);
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/ShowJvmHandler.java
// public class ShowJvmHandler extends AbstractApi {
//     private static final Logger LOG = LoggerFactory.getLogger(ShowJvmHandler.class);
//     private static final Pattern RE_ID = Pattern.compile("/([a-zA-Z0-9]+)$");
// 
//     public ShowJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
// 
//         try {
//             if (m.find()) {
//                 String id = m.group(1);
//                 JsonObjectBuilder jvmStatus = Json.createObjectBuilder();
//                 JsonArrayBuilder monitorStats = Json.createArrayBuilder();
//                 JvmProcess process = getContainer()
//                         .getPool()
//                         .getProcess(id);
// 
//                 process.getMonitorStats()
//                         .stream()
//                         .filter(Objects::nonNull)
//                         .map(MonitorStat::toJson)
//                         .forEach(monitorStats::add);
// 
//                 jvmStatus.add("id", process.getId());
//                 jvmStatus.add("pid", process.getPid());
//                 jvmStatus.add("uptime", process.getUptime());
//                 jvmStatus.add("options", process.getJvmOptions().stream()
//                         .collect(Collectors.joining(" ")));
//                 jvmStatus.add("stats", monitorStats.build());
//                 sendJson(exchange, jvmStatus.build());
//             } else {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//             }
//         } catch (Exception ex) {
//             LOG.error("", ex);
//             sendBadRequest(exchange, exchange.getRequestURI().getPath());
//         }
//     }
// }


import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import me.geso.routes.RoutingResult;
import me.geso.routes.WebRouter;
import net.unit8.falchion.api.ListJvmHandler;
import net.unit8.falchion.api.ReadyJvmHandler;
import net.unit8.falchion.api.RefreshContainerHandler;
import net.unit8.falchion.api.ShowJvmHandler;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
package net.unit8.falchion;



/**
 * Provides RESTful APIs for managing the container.
 *
 * @author kawasima
 */
public class ApiServer {
    final HttpServer httpServer;
    private ExecutorService executor;

    public ApiServer(Container container, int port) {
        ListJvmHandler listJvmHandler = new ListJvmHandler(container);
// Path: falchion-container/src/main/java/net/unit8/falchion/api/ListJvmHandler.java
// public class ListJvmHandler extends AbstractApi {
//     public ListJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
//         getContainer().getPool().getActiveProcesses().forEach(p ->
//                 arrayBuilder.add(Json.createObjectBuilder()
//                         .add("id", p.getId())
//                         .add("pid", p.getPid())));
// 
//         sendJson(exchange, arrayBuilder.build());
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/ReadyJvmHandler.java
// public class ReadyJvmHandler extends AbstractApi {
//     private static final Pattern RE_ID = Pattern.compile("/([a-zA-Z0-9]+)/ready$");
// 
//     public ReadyJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
//         try {
//             if (m.find()) {
//                 String id = m.group(1);
//                 JvmProcess process = getContainer()
//                         .getPool()
//                         .getProcessByPid(Long.parseLong(id));
//                 process.ready();
// 
//                 sendNoContent(exchange);
//             } else {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//             }
//         } catch (Exception ex) {
//             sendBadRequest(exchange, exchange.getRequestURI().getPath());
//         }
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/RefreshContainerHandler.java
// public class RefreshContainerHandler extends AbstractApi {
//     private static final Logger LOG = LoggerFactory.getLogger(RefreshContainerHandler.class);
//     private static final Pattern RE_ID = Pattern.compile("/refresh/([a-zA-Z0-9.-]+)$");
// 
//     public RefreshContainerHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
//         Container container = getContainer();
//         JvmPool pool = container.getPool();
//         if (m.find()) {
//             String version = m.group(1);
//             String classpath = container.createClasspath(container.getBasedir(), version);
//             if (Objects.equals(classpath, container.getBasedir())) {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//                 return;
//             }
//             pool.setClasspath(classpath);
//             LOG.info("The version of the application has been changed. New version is '{}'", version);
//         }
//         pool.refresh();
//         sendNoContent(exchange);
//     }
// }
// 
// Path: falchion-container/src/main/java/net/unit8/falchion/api/ShowJvmHandler.java
// public class ShowJvmHandler extends AbstractApi {
//     private static final Logger LOG = LoggerFactory.getLogger(ShowJvmHandler.class);
//     private static final Pattern RE_ID = Pattern.compile("/([a-zA-Z0-9]+)$");
// 
//     public ShowJvmHandler(Container container) {
//         super(container);
//     }
// 
//     @Override
//     public void handle(HttpExchange exchange) throws IOException {
//         Matcher m = RE_ID.matcher(exchange.getRequestURI().getPath());
// 
//         try {
//             if (m.find()) {
//                 String id = m.group(1);
//                 JsonObjectBuilder jvmStatus = Json.createObjectBuilder();
//                 JsonArrayBuilder monitorStats = Json.createArrayBuilder();
//                 JvmProcess process = getContainer()
//                         .getPool()
//                         .getProcess(id);
// 
//                 process.getMonitorStats()
//                         .stream()
//                         .filter(Objects::nonNull)
//                         .map(MonitorStat::toJson)
//                         .forEach(monitorStats::add);
// 
//                 jvmStatus.add("id", process.getId());
//                 jvmStatus.add("pid", process.getPid());
//                 jvmStatus.add("uptime", process.getUptime());
//                 jvmStatus.add("options", process.getJvmOptions().stream()
//                         .collect(Collectors.joining(" ")));
//                 jvmStatus.add("stats", monitorStats.build());
//                 sendJson(exchange, jvmStatus.build());
//             } else {
//                 sendBadRequest(exchange, exchange.getRequestURI().getPath());
//             }
//         } catch (Exception ex) {
//             LOG.error("", ex);
//             sendBadRequest(exchange, exchange.getRequestURI().getPath());
//         }
//     }
// }


// Path: falchion-container/src/main/java/net/unit8/falchion/ApiServer.java
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import me.geso.routes.RoutingResult;
import me.geso.routes.WebRouter;
import net.unit8.falchion.api.ListJvmHandler;
import net.unit8.falchion.api.ReadyJvmHandler;
import net.unit8.falchion.api.RefreshContainerHandler;
import net.unit8.falchion.api.ShowJvmHandler;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

package net.unit8.falchion;



/**
 * Provides RESTful APIs for managing the container.
 *
 * @author kawasima
 */
public class ApiServer {
    final HttpServer httpServer;
    private ExecutorService executor;

    public ApiServer(Container container, int port) {
        ListJvmHandler listJvmHandler = new ListJvmHandler(container);
        RefreshContainerHandler refreshContainerHandler = new RefreshContainerHandler(container);