package org.minimalj.frontend.impl.nanoserver;

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

public class NanoHttpdApplication {
	private static final int TIME_OUT = 5 * 60 * 1000;
	
	public static void main(final String[] args) throws Exception {
		Frontend.setInstance(new JsonFrontend());
		Application.initApplication(args);
		MjWebSocketDaemon secureWebSocketDaemon = new MjWebSocketDaemon(443, true);
		MjWebSocketDaemon webSocketDaemon = new MjWebSocketDaemon(8080, false);
		secureWebSocketDaemon.start(TIME_OUT);
		webSocketDaemon.start(TIME_OUT);
		
        try {
            System.in.read();
        } catch (Throwable ignored) {
        }

        secureWebSocketDaemon.stop();
        webSocketDaemon.stop();
        System.out.println("Server stopped.\n");
	}

}

@@ -41,16 +41,45 @@
 erver;%0A%0A
+import java.io.IOException;%0A%0A
 import o
@@ -206,16 +206,54 @@
 rontend;
+%0Aimport org.minimalj.util.StringUtils;
 %0A%0Apublic
@@ -282,16 +282,61 @@
 ation %7B%0A
+%09private static final boolean SECURE = true;%0A
 %09private
@@ -387,21 +387,22 @@
 %09%0A%09p
-ublic
+rivate
  static 
 void
@@ -401,217 +401,441 @@
 tic 
-void main(final String%5B%5D args) throws Exception %7B%0A%09%09Frontend.setInstance(new JsonFrontend());%0A%09%09Application.initApplication(args);%0A%09%09MjWebSocketDaemon secureWebSocketDaemon = new MjWebSocketDaemon(443, true
+int getPort(boolean secure) %7B%0A%09%09String portString = System.getProperty(%22MjFrontendPort%22 + (secure ? %22Ssl%22 : %22%22), secure ? %22-1%22 : %228080%22);%0A%09%09return !StringUtils.isEmpty(portString) ? Integer.valueOf(portString) : -1 ;%0A%09%7D%0A%09%0A%09private static MjWebSocketDaemon start(boolean secure) throws IOException %7B%0A%09%09int port = getPort(secure);%0A%09%09if (port %3E 0) %7B%0A%09%09%09System.out.println(%22Start web frontend on %22 + port + (secure ? %22(Secure)%22 : %22%22)
 );%0A
+%09
 %09%09Mj
@@ -850,26 +850,17 @@
 tDaemon 
-webSocketD
+d
 aemon = 
@@ -885,182 +885,489 @@
 mon(
-8080, false);%0A%09%09secureWebSocketDaemon.start(TIME_OUT);%0A%09%09webSocketDaemon.start(TIME_OUT);%0A%09%09%0A        try %7B%0A            System.in.read();%0A        %7D catch (Throwable ignored) %7B
+port, secure);%0A%09%09%09daemon.start(TIME_OUT);%0A%09%09%09return daemon;%0A%09%09%7D else %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D%0A%09%0A%09private static void stop(MjWebSocketDaemon daemon) %7B%0A%09%09if (daemon != null) %7B%0A%09%09%09System.out.println(%22Stop web frontend on %22 + daemon.getListeningPort());%0A%09%09%09daemon.stop();%0A%09%09%7D%0A%09%7D%0A%09%0A%09public static void main(final String%5B%5D args) throws Exception %7B%0A%09%09Frontend.setInstance(new JsonFrontend());%0A%09%09Application.initApplication(args);%0A%09%09%0A%09%09MjWebSocketDaemon daemon = null, secureDaemon = null;
 %0A   
@@ -1371,18 +1371,21 @@
         
-%7D%0A
+try %7B
 %0A       
@@ -1389,118 +1389,184 @@
     
-secureWebSocketDaemon.stop();%0A        webSocketDaemon.stop();%0A        System.out.println(%22Server stopped.%5Cn%22);
+%09daemon = start(!SECURE);%0A        %09secureDaemon = start(SECURE);%0A            System.in.read();%0A        %7D finally %7B%0A        %09stop(secureDaemon);%0A        %09stop(daemon);%0A        %7D
 %0A%09%7D%0A

make ports configurable