Persoenliche Mediazentrale
ulrich
2021-04-04 2b6134e50801f9fc66a27a2d565340f5773f251f
src/de/uhilger/mediaz/Server.java
@@ -1,5 +1,5 @@
/*
  mini-server - Ein minimalistischer HTTP-Server
  Mediazentrale - Personal Media Center
  Copyright (C) 2021  Ulrich Hilger
  This program is free software: you can redistribute it and/or modify
@@ -14,17 +14,18 @@
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
 */
package de.uhilger.mediaz;
import com.sun.net.httpserver.HttpServer;
import de.uhilger.mediaz.handler.FileHandler;
import de.uhilger.mediaz.handler.StopServerHandler;
import de.uhilger.mediaz.api.FileHandler;
import de.uhilger.mediaz.api.StopServerHandler;
import java.io.IOException;
import java.util.logging.Logger;
import java.net.InetSocketAddress;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.logging.Level;
/**
 * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines 
@@ -37,8 +38,10 @@
  
  private static final Logger logger = Logger.getLogger(Server.class.getName());
  
  public static final String STR_SLASH = "/";
  public static final String CMD_SERVER_STOP = "/server/stop";
  public static final String RB_SERVER_START_MSG = "msgServerStart";
  public static final String RB_WEBROOT = "webroot";
  public static final String RB_STOP_SERVER = "stopServer";
  public static final String RB_SLASH = "slash";
  
  private int port;
  
@@ -67,8 +70,9 @@
   * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
   */
  public void setContextName(String ctxName) {
    if(!ctxName.startsWith(STR_SLASH)) {
      this.ctx = STR_SLASH + ctxName;
    String slash = App.getRs(RB_SLASH);
    if(!ctxName.startsWith(slash)) {
      this.ctx = slash + ctxName;
    } else {
      this.ctx = ctxName;
    }
@@ -82,11 +86,11 @@
   * in diesem Objekt 
   */
  public void start() throws IOException {
    logger.info("Server starting on port " + port);
    logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext(ctx + STR_SLASH, new FileHandler(App.getInitParameter(App.IP_WWW_DATA)));
    server.createContext(ctx + CMD_SERVER_STOP, new StopServerHandler());
    server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA))));
    server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
    server.setExecutor(Executors.newFixedThreadPool(20));
    server.start();
  }