package org.minimalj.frontend.impl.nanoserver;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.minimalj.application.Application;
import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.impl.json.JsonFrontend;
import org.minimalj.util.StringUtils;

import fi.iki.elonen.NanoHTTPD;

public class NanoWebServer {
	private static final Logger LOG = Logger.getLogger(NanoWebServer.class.getName());
	
	private static final boolean SECURE = true;
	private static final int TIME_OUT = 5 * 60 * 1000;
	
	private static boolean useWebSocket = Boolean.valueOf(System.getProperty("MjUseWebSocket", "false"));
	
	private static void start(boolean secure) throws IOException {
		int port = getPort(secure);
		if (port > 0) {
			LOG.info("Start web " + (useWebSocket ? "socket" : "") + " frontend on " + port + (secure ? " (Secure)" : ""));
			NanoHTTPD nanoHTTPD = useWebSocket ? newMjWebSocketDaemon(port, secure) : new MjWebDaemon(port, secure);
			nanoHTTPD.start(TIME_OUT, false); // false -> this will not start a 'java' daemon, but a normal thread which keeps JVM alive
		}
	}
	
	private static int getPort(boolean secure) {
		String portString = System.getProperty("MjFrontendPort" + (secure ? "Ssl" : ""), null);
		if (!StringUtils.isEmpty(portString) && !Character.isDigit(portString.charAt(0))) {
			portString = System.getProperty(portString, null);
		}
		if (portString == null) {
			return secure ? -1 : 8080;
		} else if (portString.length() == 0) {
			// make it possible to deactive non secure port by add -DMjFrontendPort=""
			return -1;
		} else {
		    return Integer.valueOf(portString);	
		}
	}
	
	private static NanoHTTPD newMjWebSocketDaemon(int port, boolean secure) {
		try {
			return new MjWebSocketDaemonFactory().create(port, secure);
		} catch (NoClassDefFoundError e) {
			LOG.log(Level.SEVERE, "Probably the dependency to nanohttpd-websocket is missing");
			throw (e);
		}
	}
	
	// Without this factory the dependency to nanohttpd-websocket would be
	// needed even if the websockets are not used
	private static class MjWebSocketDaemonFactory {
		
		public NanoHTTPD create(int port, boolean secure) {
			return new MjWebSocketDaemon(port, secure);
		}
	}
	
	public static void main(final String[] args) throws Exception {
		Frontend.setInstance(new JsonFrontend());
		Application.initApplication(args);
		
		start(!SECURE);
        start(SECURE);
	}

}

@@ -1267,197 +1267,8 @@
 %22%22),
- null);%0A%09%09if (!StringUtils.isEmpty(portString) && !Character.isDigit(portString.charAt(0))) %7B%0A%09%09%09portString = System.getProperty(portString, null);%0A%09%09%7D%0A%09%09if (portString == null) %7B%0A%09%09%09return
  sec
@@ -1277,175 +1277,67 @@
 e ? 
+%22
 -1
+%22
  : 
+%22
 8080
+%22)
 ;%0A%09%09
-%7D else if (portString.length() == 0) %7B%0A%09%09%09// make it possible to deactive non secure port by add -DMjFrontendPort=%22%22%0A%09%09%09return -1;%0A%09%09%7D else %7B%0A%09%09    return
+return !StringUtils.isEmpty(portString) ?
  Int
@@ -1364,14 +1364,15 @@
 ing)
-;%09%0A%09%09%7D
+ : -1 ;
 %0A%09%7D%0A

Revert last change.