commit | author | age
|
c3b1d1
|
1 |
/* |
043915
|
2 |
Mediazentrale - Personal Media Center |
c3b1d1
|
3 |
Copyright (C) 2021 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.mediaz.handler; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
|
21 |
import com.sun.net.httpserver.HttpHandler; |
|
22 |
import de.uhilger.mediaz.App; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.OutputStream; |
|
25 |
import java.util.Timer; |
|
26 |
import java.util.TimerTask; |
|
27 |
import java.util.logging.Logger; |
|
28 |
|
|
29 |
/** |
|
30 |
* Ein HTTP-Handler zum Stoppen der Anwendung |
|
31 |
* |
|
32 |
* @author Ulrich Hilger |
|
33 |
*/ |
|
34 |
public class StopServerHandler implements HttpHandler { |
|
35 |
|
|
36 |
/** |
|
37 |
* Den Server geordnet herunterfahren und |
|
38 |
* dann die Anwendung beenden. |
|
39 |
* |
|
40 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
41 |
* Anfertigen und Senden der Antwort |
|
42 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
43 |
*/ |
|
44 |
@Override |
|
45 |
public void handle(HttpExchange e) throws IOException { |
|
46 |
Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString()); |
|
47 |
String response = "Server stopped"; |
|
48 |
e.sendResponseHeaders(200, response.length()); |
|
49 |
OutputStream os = e.getResponseBody(); |
|
50 |
os.write(response.getBytes()); |
|
51 |
os.flush(); |
|
52 |
os.close(); |
|
53 |
Logger.getLogger(StopServerHandler.class.getName()).info("stopping server."); |
|
54 |
e.getHttpContext().getServer().stop(1); |
|
55 |
Timer timer = new Timer(); |
|
56 |
timer.schedule(new AppStopper(), 2000); |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Die Klasse AppStopper ermöglicht das asnychrone bzw. |
|
61 |
* zeitgesteuerte Stoppen der Anwendung. |
|
62 |
*/ |
|
63 |
class AppStopper extends TimerTask { |
|
64 |
|
|
65 |
@Override |
|
66 |
public void run() { |
043915
|
67 |
Logger.getLogger(StopServerHandler.class.getName()).info("Mediazentrale beendet."); |
c3b1d1
|
68 |
App.stop(); |
U |
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
} |