X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=library%2FWebLibraryServer.java;h=50dc3d942dcf39aeeadeb68d88c58a714e7551cf;hp=9073b8cad58895f02ccbd88d2a8d2a9deebf6397;hb=6d465e886ae46c9da99117cc4302948f700a51c4;hpb=877a44c78f1c4985aa362f6cdce385a4f860b51d diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index 9073b8c..50dc3d9 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -70,6 +70,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { private Map imprts = new HashMap(); + private boolean exiting; + public WebLibraryServer(boolean secure) throws IOException { super(secure); @@ -97,6 +99,42 @@ public class WebLibraryServer extends WebLibraryServerHtml { new Thread(this).start(); } + @Override + protected Response stop(WLoginResult login) { + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Exit not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting..."); + } + + exiting = true; + Instance.getInstance().getTraceHandler().trace("Exiting"); + + boolean ok; + do { + synchronized (imprts) { + ok = imprts.isEmpty(); + } + if (!ok) { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Instance.getInstance().getTraceHandler() + .trace("Waiting to exit..."); + } + } + } while (!ok); + + doStop(); + + return NanoHTTPD.newFixedLengthResponse(Status.OK, + NanoHTTPD.MIME_PLAINTEXT, "Exited"); + } + @Override protected WLoginResult login(boolean badLogin, boolean badCookie) { return new WLoginResult(false, false); @@ -303,6 +341,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed"); } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } String luid = uriParts[off + 0]; String type = uriParts[off + 1]; @@ -383,6 +426,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed"); } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } String type = uriParts[off + 0]; String id = uriParts[off + 1]; @@ -409,6 +457,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Import not allowed"); } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } final URL url = new URL(urlStr); final Progress pg = new Progress(); @@ -476,6 +529,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed"); } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } String luid = uriParts[off + 0];