Persoenliche Mediazentrale
ulrich
2021-05-06 f70acbb491c6421623cca57292a75f1820efad4d
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;
f70acb 25 import java.util.ResourceBundle;
c3b1d1 26 import java.util.Timer;
U 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 {
2b6134 36   
U 37   /* Der Logger fuer diesen StopServerHandler */
38   private static final Logger logger = Logger.getLogger(StopServerHandler.class.getName());
39   
40   /* ResourceBundle-Kennungen */
41   public static final String RB_STOPPING_SERVER = "stoppingServer";
42   public static final String RB_SERVER_STOPPED = "serverStopped";
43   public static final String RB_MEDIAZ_END = "mediazEnd";
c3b1d1 44
f70acb 45   private ResourceBundle rb;
U 46   
c3b1d1 47   /**
U 48    * Den Server geordnet herunterfahren und 
49    * dann die Anwendung beenden.
50    * 
51    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
52    * Anfertigen und Senden der Antwort
53    * @throws IOException falls etwas schief geht entsteht dieser Fehler
54    */
55   @Override
56   public void handle(HttpExchange e) throws IOException {
57     Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString());
f70acb 58     String response = getResString(RB_SERVER_STOPPED);
c3b1d1 59     e.sendResponseHeaders(200, response.length());
U 60     OutputStream os = e.getResponseBody();
61     os.write(response.getBytes());
62     os.flush();
63     os.close();
f70acb 64     logger.info(getResString(RB_STOPPING_SERVER));
c3b1d1 65     e.getHttpContext().getServer().stop(1);
U 66     Timer timer = new Timer();
67     timer.schedule(new AppStopper(), 2000);
68   }
69
70   /**
71    * Die Klasse AppStopper erm&ouml;glicht das asnychrone bzw. 
72    * zeitgesteuerte Stoppen der Anwendung.
73    */
74   class AppStopper extends TimerTask {
75
76     @Override
77     public void run() {
f70acb 78       logger.info(getResString(RB_MEDIAZ_END));
U 79       System.exit(0);
c3b1d1 80     }
U 81   }
82
f70acb 83   protected String getResString(String key) {
U 84     if(rb == null) {
85       rb = ResourceBundle.getBundle(App.RB_NAME);
86     }
87     return rb.getString(key);
88   }
c3b1d1 89 }