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/>. |
043915
|
17 |
*/ |
c3b1d1
|
18 |
package de.uhilger.mediaz; |
U |
19 |
|
|
20 |
import com.sun.net.httpserver.HttpServer; |
2b6134
|
21 |
import de.uhilger.mediaz.api.FileHandler; |
U |
22 |
import de.uhilger.mediaz.api.StopServerHandler; |
c3b1d1
|
23 |
import java.io.IOException; |
U |
24 |
import java.util.logging.Logger; |
|
25 |
import java.net.InetSocketAddress; |
2b6134
|
26 |
import java.util.ResourceBundle; |
c3b1d1
|
27 |
import java.util.concurrent.Executors; |
2b6134
|
28 |
import java.util.logging.Level; |
c3b1d1
|
29 |
|
U |
30 |
/** |
|
31 |
* Die Klasse Server stellt Methoden zur Ausführung eines |
|
32 |
* HTTP-Servers bereit |
|
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 0.1, 25.03.2021 |
|
36 |
*/ |
|
37 |
public class Server { |
|
38 |
|
|
39 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
|
40 |
|
2b6134
|
41 |
public static final String RB_SERVER_START_MSG = "msgServerStart"; |
U |
42 |
public static final String RB_WEBROOT = "webroot"; |
|
43 |
public static final String RB_STOP_SERVER = "stopServer"; |
|
44 |
public static final String RB_SLASH = "slash"; |
c3b1d1
|
45 |
|
U |
46 |
private int port; |
|
47 |
|
|
48 |
private String ctx; |
|
49 |
|
|
50 |
/** |
|
51 |
* Ein neues Objekt der Kalsse Server erzeugen |
|
52 |
* @param port der Port, über den dieser Server erreichbar sein soll |
|
53 |
*/ |
|
54 |
public Server(int port) { |
|
55 |
this.port = port; |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Den Port angeben, unter dem der Server erreichbar sein soll |
|
60 |
* |
|
61 |
* @param port der Port, unter dem der Server erreichbar sein soll |
|
62 |
*/ |
|
63 |
public void setPort(int port) { |
|
64 |
this.port = port; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Den Namen des Kontexts angeben, über den dieser Server |
|
69 |
* erreichbar sein soll |
|
70 |
* @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll |
|
71 |
*/ |
|
72 |
public void setContextName(String ctxName) { |
2b6134
|
73 |
String slash = App.getRs(RB_SLASH); |
U |
74 |
if(!ctxName.startsWith(slash)) { |
|
75 |
this.ctx = slash + ctxName; |
c3b1d1
|
76 |
} else { |
U |
77 |
this.ctx = ctxName; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Die Endpunkte ('Context'e) einrichten, unter denen die Dienste |
|
83 |
* dieses Servers erreichbar sein sollen und den Server starten |
|
84 |
* |
|
85 |
* @throws IOException wenn etwas schief geht, finden sich Angaben |
|
86 |
* in diesem Objekt |
|
87 |
*/ |
|
88 |
public void start() throws IOException { |
2b6134
|
89 |
logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port)); |
c3b1d1
|
90 |
|
U |
91 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
2b6134
|
92 |
server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA)))); |
U |
93 |
server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler()); |
c3b1d1
|
94 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
U |
95 |
server.start(); |
|
96 |
} |
|
97 |
|
|
98 |
} |