Persoenliche Mediazentrale
ulrich
2021-04-24 94b1c2f60bde70d681b4bf8a5cd04b86e9ccf4ed
commit | author | age
c3b1d1 1 /*
94b1c2 2   Tango - 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  */
94b1c2 18 package de.uhilger.tango.api;
c3b1d1 19
U 20 import com.sun.net.httpserver.HttpExchange;
21 import com.sun.net.httpserver.HttpHandler;
94b1c2 22 import de.uhilger.tango.App;
c3b1d1 23 import java.io.IOException;
U 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 {
2b6134 35   
U 36   /* Der Logger fuer diesen StopServerHandler */
37   private static final Logger logger = Logger.getLogger(StopServerHandler.class.getName());
38   
39   /* ResourceBundle-Kennungen */
40   public static final String RB_STOPPING_SERVER = "stoppingServer";
41   public static final String RB_SERVER_STOPPED = "serverStopped";
42   public static final String RB_MEDIAZ_END = "mediazEnd";
c3b1d1 43
U 44   /**
45    * Den Server geordnet herunterfahren und 
46    * dann die Anwendung beenden.
47    * 
48    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
49    * Anfertigen und Senden der Antwort
50    * @throws IOException falls etwas schief geht entsteht dieser Fehler
51    */
52   @Override
53   public void handle(HttpExchange e) throws IOException {
54     Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString());
2b6134 55     String response = App.getRs(RB_SERVER_STOPPED);
c3b1d1 56     e.sendResponseHeaders(200, response.length());
U 57     OutputStream os = e.getResponseBody();
58     os.write(response.getBytes());
59     os.flush();
60     os.close();
2b6134 61     logger.info(App.getRs(RB_STOPPING_SERVER));
c3b1d1 62     e.getHttpContext().getServer().stop(1);
U 63     Timer timer = new Timer();
64     timer.schedule(new AppStopper(), 2000);
65   }
66
67   /**
68    * Die Klasse AppStopper erm&ouml;glicht das asnychrone bzw. 
69    * zeitgesteuerte Stoppen der Anwendung.
70    */
71   class AppStopper extends TimerTask {
72
73     @Override
74     public void run() {
2b6134 75       logger.info(App.getRs(RB_MEDIAZ_END));
c3b1d1 76       App.stop();
U 77     }
78   }
79
80 }