commit | author | age
|
9c7249
|
1 |
/* |
678b07
|
2 |
mini-server - Ein minimalistischer HTTP-Server |
U |
3 |
Copyright (C) 2021 Ulrich Hilger |
9c7249
|
4 |
|
678b07
|
5 |
This program is free software: you can redistribute it and/or modify |
U |
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. |
9c7249
|
9 |
|
678b07
|
10 |
This program is distributed in the hope that it will be useful, |
U |
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. |
9c7249
|
14 |
|
678b07
|
15 |
You should have received a copy of the GNU Affero General Public License |
U |
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
d0bb21
|
17 |
*/ |
9c7249
|
18 |
package de.uhilger.minsrv; |
U |
19 |
|
|
20 |
import java.io.IOException; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.logging.Level; |
|
23 |
import java.util.logging.Logger; |
|
24 |
|
|
25 |
/** |
6d3836
|
26 |
* <p>Die Hauptklasse des mini-server</p> |
U |
27 |
* |
|
28 |
* <p>Folgende Kommandozeilenparameter werden verarbeitet </p> |
|
29 |
* |
|
30 |
* <p>ctx - Kontext des Servers <br> |
|
31 |
* www-data - lokales Datenverzeichnis <br> |
|
32 |
* port - Port</p>> |
|
33 |
* |
|
34 |
* <p>Beispiel: <code>java -jar mini-server.jar ctx="srv" www-data="/home/fred/www" |
|
35 |
* port=9090</code></p> |
|
36 |
* |
|
37 |
* <p>Startet den Server auf http://localhost:9090/srv und liefert Inhalte aus |
|
38 |
* dem Verzeichnis <code>/home/fred/www</code> aus. |
|
39 |
* |
|
40 |
* Ein Aufruf von http://localhost:9090/srv/pfad/zum/inhalt/index.html liefert |
|
41 |
* also die Datei 'index.html' aus dem Ordner /home/fred/www/pfad/zum/inhalt |
|
42 |
* aus.</p> |
|
43 |
* |
|
44 |
* <p>Mit http://localhost:9090/srv/server/stop werden Server und App |
|
45 |
* beendet.</p> |
d0bb21
|
46 |
* |
8abbcf
|
47 |
* @author Ulrich Hilger |
9c7249
|
48 |
* @version 0.1, 25.03.2021 |
U |
49 |
*/ |
|
50 |
public class App { |
|
51 |
|
|
52 |
private static final Logger logger = Logger.getLogger(App.class.getName()); |
d0bb21
|
53 |
|
9c7249
|
54 |
public static final String IP_PORT = "port"; |
U |
55 |
public static final String IP_WWW_DATA = "www-data"; |
678b07
|
56 |
public static final String IP_CTX = "ctx"; |
d0bb21
|
57 |
|
U |
58 |
private static HashMap initParams; |
|
59 |
|
9c7249
|
60 |
/** |
6d3836
|
61 |
* <p>Start-Methode dieser Anwendung</p> |
d0bb21
|
62 |
* |
678b07
|
63 |
* @param args Kommandozeilenparameter |
9c7249
|
64 |
*/ |
U |
65 |
public static void main(String[] args) { |
|
66 |
initParams = new HashMap(); |
d0bb21
|
67 |
for (String arg : args) { |
9c7249
|
68 |
String[] argParts = arg.split("="); |
U |
69 |
initParams.put(argParts[0], argParts[1]); |
|
70 |
} |
d0bb21
|
71 |
|
U |
72 |
String portStr = getInitParameter(IP_PORT); |
|
73 |
if (portStr != null) { |
|
74 |
Server server = new Server(Integer.parseInt(portStr)); |
|
75 |
try { |
|
76 |
String ctxName = getInitParameter(IP_CTX); |
|
77 |
if (ctxName != null) { |
|
78 |
server.setContextName(ctxName); |
|
79 |
server.start(); |
|
80 |
} else { |
|
81 |
logger.severe("Der Parameter " + IP_CTX + " muss angegeben werden."); |
|
82 |
} |
|
83 |
} catch (IOException ex) { |
|
84 |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); |
|
85 |
} |
|
86 |
} else { |
|
87 |
logger.severe("Der Parameter " + IP_PORT + " muss angegeben werden."); |
9c7249
|
88 |
} |
U |
89 |
} |
d0bb21
|
90 |
|
678b07
|
91 |
/** |
U |
92 |
* Diese Anwendung stoppen |
|
93 |
*/ |
9c7249
|
94 |
public static void stop() { |
U |
95 |
System.exit(0); |
|
96 |
} |
d0bb21
|
97 |
|
678b07
|
98 |
/** |
U |
99 |
* Einen Kommandozeilenparameter ermitteln |
d0bb21
|
100 |
* |
678b07
|
101 |
* @param pname Names des Parameters |
d0bb21
|
102 |
* @return Inhalt des Parameters oder null, wenn der Parameter nicht gefunden |
U |
103 |
* wurde |
678b07
|
104 |
*/ |
9c7249
|
105 |
public static String getInitParameter(String pname) { |
U |
106 |
String param = null; |
|
107 |
Object o = initParams.get(pname); |
d0bb21
|
108 |
if (o != null) { |
9c7249
|
109 |
param = o.toString(); |
U |
110 |
} |
|
111 |
return param; |
d0bb21
|
112 |
} |
9c7249
|
113 |
} |