X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fruntime%2FCmsDeploymentImpl.java;h=d3f4b575aa7ff6486f5708a7fbfa93728af4bb8c;hb=HEAD;hp=e2d1fb97a592d38e6a64e85918353dbb5b49ce6a;hpb=55870eba50d8b28e72a3102fd18a17a6f23f7bad;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java b/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java index e2d1fb97a..d3f4b575a 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java @@ -3,14 +3,17 @@ package org.argeo.cms.internal.runtime; import static org.argeo.api.cms.CmsConstants.CONTEXT_PATH; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; import org.argeo.api.cms.CmsConstants; import org.argeo.api.cms.CmsDeployment; import org.argeo.api.cms.CmsLog; +import org.argeo.api.cms.CmsSshd; import org.argeo.api.cms.CmsState; import org.argeo.cms.CmsDeployProperty; -import org.argeo.cms.CmsSshd; import org.argeo.cms.internal.http.CmsAuthenticator; import org.argeo.cms.internal.http.PublicCmsAuthenticator; @@ -29,12 +32,12 @@ public class CmsDeploymentImpl implements CmsDeployment { private boolean sshdExpected = false; // HTTP - private HttpServer httpServer; + private CompletableFuture httpServer = new CompletableFuture<>(); private Map httpHandlers = new TreeMap<>(); private Map httpAuthenticators = new TreeMap<>(); // SSHD - private CmsSshd cmsSshd; + private CompletableFuture cmsSshd = new CompletableFuture<>(); public void start() { log.debug(() -> "CMS deployment available"); @@ -49,18 +52,32 @@ public class CmsDeploymentImpl implements CmsDeployment { String httpPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTP_PORT.getProperty()); String httpsPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTPS_PORT.getProperty()); httpExpected = httpPort != null || httpsPort != null; + if (!httpExpected) + httpServer.complete(null); String sshdPort = this.cmsState.getDeployProperty(CmsDeployProperty.SSHD_PORT.getProperty()); sshdExpected = sshdPort != null; + if (!sshdExpected) + cmsSshd.complete(null); } public void setHttpServer(HttpServer httpServer) { - this.httpServer = httpServer; - // create contexts whose handles had already been published - for (String contextPath : httpHandlers.keySet()) { - HttpHandler httpHandler = httpHandlers.get(contextPath); - CmsAuthenticator authenticator = httpAuthenticators.get(contextPath); - createHttpContext(contextPath, httpHandler, authenticator); + Objects.requireNonNull(httpServer); + if (this.httpServer.isDone()) + if (httpExpected) + throw new IllegalStateException("HTTP server is already set"); + else + return;// ignore + // create contexts whose handlers had already been published + synchronized (httpHandlers) { + synchronized (httpAuthenticators) { + this.httpServer.complete(httpServer); + for (String contextPath : httpHandlers.keySet()) { + HttpHandler httpHandler = httpHandlers.get(contextPath); + CmsAuthenticator authenticator = httpAuthenticators.get(contextPath); + createHttpContext(contextPath, httpHandler, authenticator); + } + } } } @@ -72,9 +89,13 @@ public class CmsDeploymentImpl implements CmsDeployment { } boolean isPublic = Boolean.parseBoolean(properties.get(CmsConstants.CONTEXT_PUBLIC)); CmsAuthenticator authenticator = isPublic ? new PublicCmsAuthenticator() : new CmsAuthenticator(); - httpHandlers.put(contextPath, httpHandler); - httpAuthenticators.put(contextPath, authenticator); - if (httpServer == null) { + synchronized (httpHandlers) { + synchronized (httpAuthenticators) { + httpHandlers.put(contextPath, httpHandler); + httpAuthenticators.put(contextPath, authenticator); + } + } + if (!httpServer.isDone()) { return; } else { createHttpContext(contextPath, httpHandler, authenticator); @@ -82,7 +103,15 @@ public class CmsDeploymentImpl implements CmsDeployment { } public void createHttpContext(String contextPath, HttpHandler httpHandler, CmsAuthenticator authenticator) { - HttpContext httpContext = httpServer.createContext(contextPath); + if (!httpExpected) { + if (log.isTraceEnabled()) + log.warn("Ignore HTTP context " + contextPath + " as we don't provide an HTTP server"); + return; + } + if (!this.httpServer.isDone()) + throw new IllegalStateException("HTTP server is not set"); + // TODO use resultNow when switching to Java 21 + HttpContext httpContext = httpServer.join().createContext(contextPath); // we want to set the authenticator BEFORE the handler actually becomes active httpContext.setAuthenticator(authenticator); httpContext.setHandler(httpHandler); @@ -94,22 +123,34 @@ public class CmsDeploymentImpl implements CmsDeployment { if (contextPath == null) return; // ignore silently httpHandlers.remove(contextPath); - if (httpServer == null) + if (!httpExpected || !httpServer.isDone()) return; - httpServer.removeContext(contextPath); + // TODO use resultNow when switching to Java 21 + httpServer.join().removeContext(contextPath); log.debug(() -> "Removed handler " + contextPath + " : " + httpHandler.getClass().getName()); } public boolean allExpectedServicesAvailable() { - if (httpExpected && httpServer == null) + if (httpExpected && !httpServer.isDone()) return false; - if (sshdExpected && cmsSshd == null) + if (sshdExpected && !cmsSshd.isDone()) return false; return true; } public void setCmsSshd(CmsSshd cmsSshd) { - this.cmsSshd = cmsSshd; + Objects.requireNonNull(cmsSshd); + this.cmsSshd.complete(cmsSshd); + } + + @Override + public CompletionStage getHttpServer() { + return httpServer.minimalCompletionStage(); + } + + @Override + public CompletionStage getCmsSshd() { + return cmsSshd.minimalCompletionStage(); } }