Do not start Jetty if not ports defined
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 30 May 2023 11:47:43 +0000 (13:47 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 30 May 2023 11:47:43 +0000 (13:47 +0200)
org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java

index 5f1173ce47e170e5bdcd08d64a31a505a2dc79a1..e994463f9606e00d530efef01af0c184b37ad684 100644 (file)
@@ -30,6 +30,7 @@ import org.eclipse.jetty.util.thread.ThreadPool;
 
 import com.sun.net.httpserver.HttpContext;
 import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
 import com.sun.net.httpserver.HttpsConfigurator;
 import com.sun.net.httpserver.HttpsServer;
 
@@ -68,6 +69,17 @@ public class JettyHttpServer extends HttpsServer {
 
        @Override
        public void start() {
+               String httpPortStr = getDeployProperty(CmsDeployProperty.HTTP_PORT);
+               String httpsPortStr = getDeployProperty(CmsDeployProperty.HTTPS_PORT);
+               if (httpPortStr != null && httpsPortStr != null)
+                       throw new IllegalArgumentException("Either an HTTP or an HTTPS port should be configured, not both");
+               if (httpPortStr == null && httpsPortStr == null) {
+                       log.warn("Neither an HTTP or an HTTPS port was configured, not starting Jetty");
+               }
+
+               /// TODO make it more generic
+               String httpHost = getDeployProperty(CmsDeployProperty.HOST);
+
                try {
 
                        ThreadPool threadPool = null;
@@ -80,7 +92,7 @@ public class JettyHttpServer extends HttpsServer {
 
                        server = new Server(threadPool);
 
-                       configureConnectors();
+                       configureConnectors(httpPortStr, httpsPortStr, httpHost);
 
                        if (httpConnector != null) {
                                httpConnector.open();
@@ -111,7 +123,6 @@ public class JettyHttpServer extends HttpsServer {
                        //
 
                        // Addresses
-                       String httpHost = getDeployProperty(CmsDeployProperty.HOST);
                        String fallBackHostname = cmsState != null ? cmsState.getHostname() : "::1";
                        if (httpConnector != null) {
                                httpAddress = new InetSocketAddress(httpHost != null ? httpHost : fallBackHostname,
@@ -131,16 +142,7 @@ public class JettyHttpServer extends HttpsServer {
                }
        }
 
-       protected void configureConnectors() {
-               String httpPortStr = getDeployProperty(CmsDeployProperty.HTTP_PORT);
-               String httpsPortStr = getDeployProperty(CmsDeployProperty.HTTPS_PORT);
-               if (httpPortStr != null && httpsPortStr != null)
-                       throw new IllegalArgumentException("Either an HTTP or an HTTPS port should be configured, not both");
-               if (httpPortStr == null && httpsPortStr == null)
-                       throw new IllegalArgumentException("Neither an HTTP or HTTPS port was configured");
-
-               /// TODO make it more generic
-               String httpHost = getDeployProperty(CmsDeployProperty.HOST);
+       protected void configureConnectors(String httpPortStr, String httpsPortStr, String httpHost) {
 
                // try {
                if (httpPortStr != null || httpsPortStr != null) {