X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=library%2FWebLibraryServer.java;h=50dc3d942dcf39aeeadeb68d88c58a714e7551cf;hb=6d465e886ae46c9da99117cc4302948f700a51c4;hp=b3d897d04bbbb8bf5ecc433cd4dee280e415c7e6;hpb=b063fe0a74fdf17f440a850bbb98a2de6233a679;p=nikiroo-utils.git diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index b3d897d..50dc3d9 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -28,7 +28,6 @@ import be.nikiroo.utils.NanoHTTPD; import be.nikiroo.utils.NanoHTTPD.Response; import be.nikiroo.utils.NanoHTTPD.Response.Status; import be.nikiroo.utils.Progress; -import be.nikiroo.utils.Progress.ProgressListener; public class WebLibraryServer extends WebLibraryServerHtml { class WLoginResult extends LoginResult { @@ -71,6 +70,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { private Map imprts = new HashMap(); + private boolean exiting; + public WebLibraryServer(boolean secure) throws IOException { super(secure); @@ -98,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); @@ -304,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]; @@ -384,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]; @@ -410,19 +457,16 @@ 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(); final String luid = lib.getNextId(); - // Keep the latest name - pg.addProgressListener(new ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg.setName(name); - } - }); - synchronized (imprts) { imprts.put(luid, pg); } @@ -485,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];