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;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
-/** Implementation of a CMS deployment. */
+/** Reference implementation of {@link CmsDeployment}. */
public class CmsDeploymentImpl implements CmsDeployment {
private final CmsLog log = CmsLog.getLog(getClass());
private boolean sshdExpected = false;
// HTTP
- private HttpServer httpServer;
+ private CompletableFuture<HttpServer> httpServer = new CompletableFuture<>();
private Map<String, HttpHandler> httpHandlers = new TreeMap<>();
private Map<String, CmsAuthenticator> httpAuthenticators = new TreeMap<>();
// SSHD
- private CmsSshd cmsSshd;
+ private CompletableFuture<CmsSshd> cmsSshd = new CompletableFuture<>();
public void start() {
log.debug(() -> "CMS deployment available");
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);
+ }
+ }
}
}
}
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);
}
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);
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<HttpServer> getHttpServer() {
+ return httpServer.minimalCompletionStage();
+ }
+
+ @Override
+ public CompletionStage<CmsSshd> getCmsSshd() {
+ return cmsSshd.minimalCompletionStage();
}
}