commit | author | age
|
00c9b9
|
1 |
/* |
U |
2 |
Proto - Ein Rumpf-Konstrukt fuer Webapps |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
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.proto; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
|
21 |
import com.sun.net.httpserver.HttpHandler; |
|
22 |
import static de.uhilger.proto.App.RB_NAME; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.OutputStream; |
|
25 |
import java.util.ResourceBundle; |
|
26 |
import java.util.Timer; |
|
27 |
import java.util.TimerTask; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* Ein HTTP-Handler zum Stoppen der Anwendung |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
*/ |
|
35 |
public class StopServerHandler implements HttpHandler { |
|
36 |
|
|
37 |
public static final String RB_SERVER_STOPPED_MSG = "msgServerStopped"; |
|
38 |
public static final String RB_STOPPING_SERVER_MSG = "msgStoppingServer"; |
|
39 |
public static final String RB_APP_ENDED_MSG = "msgAppEnded"; |
|
40 |
|
|
41 |
/** |
|
42 |
* Den Server geordnet herunterfahren und |
|
43 |
* dann die Anwendung beenden. |
|
44 |
* |
|
45 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
46 |
* Anfertigen und Senden der Antwort |
|
47 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
48 |
*/ |
|
49 |
@Override |
|
50 |
public void handle(HttpExchange e) throws IOException { |
|
51 |
Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString()); |
|
52 |
ResourceBundle rb = ResourceBundle.getBundle(RB_NAME); |
|
53 |
|
|
54 |
String response = rb.getString(RB_SERVER_STOPPED_MSG); //"Server stopped"; |
|
55 |
e.sendResponseHeaders(200, response.length()); |
|
56 |
OutputStream os = e.getResponseBody(); |
|
57 |
os.write(response.getBytes()); |
|
58 |
os.flush(); |
|
59 |
os.close(); |
|
60 |
Logger.getLogger(StopServerHandler.class.getName()).info(rb.getString(RB_STOPPING_SERVER_MSG)); |
|
61 |
e.getHttpContext().getServer().stop(1); |
|
62 |
Timer timer = new Timer(); |
|
63 |
timer.schedule(new AppStopper(), 2000); |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Die Klasse AppStopper ermöglicht das asnychrone bzw. |
|
68 |
* zeitgesteuerte Stoppen der Anwendung. |
|
69 |
*/ |
|
70 |
class AppStopper extends TimerTask { |
|
71 |
|
|
72 |
@Override |
|
73 |
public void run() { |
|
74 |
ResourceBundle rb = ResourceBundle.getBundle(RB_NAME); |
|
75 |
Logger.getLogger(StopServerHandler.class.getName()).info(rb.getString(RB_APP_ENDED_MSG)); |
|
76 |
System.exit(0); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
} |