]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java
Introduce simple SWT app
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsDeploymentImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import static org.argeo.api.cms.CmsConstants.CONTEXT_PATH;
4
5 import java.util.Map;
6 import java.util.TreeMap;
7
8 import org.argeo.api.cms.CmsConstants;
9 import org.argeo.api.cms.CmsDeployment;
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.api.cms.CmsState;
12 import org.argeo.cms.CmsDeployProperty;
13 import org.argeo.cms.CmsSshd;
14 import org.argeo.cms.internal.http.CmsAuthenticator;
15 import org.argeo.cms.internal.http.PublicCmsAuthenticator;
16
17 import com.sun.net.httpserver.HttpContext;
18 import com.sun.net.httpserver.HttpHandler;
19 import com.sun.net.httpserver.HttpServer;
20
21 /** Reference implementation of {@link CmsDeployment}. */
22 public class CmsDeploymentImpl implements CmsDeployment {
23 private final CmsLog log = CmsLog.getLog(getClass());
24
25 private CmsState cmsState;
26
27 // Expectations
28 private boolean httpExpected = false;
29 private boolean sshdExpected = false;
30
31 // HTTP
32 private HttpServer httpServer;
33 private Map<String, HttpHandler> httpHandlers = new TreeMap<>();
34 private Map<String, CmsAuthenticator> httpAuthenticators = new TreeMap<>();
35
36 // SSHD
37 private CmsSshd cmsSshd;
38
39 public void start() {
40 log.debug(() -> "CMS deployment available");
41 }
42
43 public void stop() {
44 }
45
46 public void setCmsState(CmsState cmsState) {
47 this.cmsState = cmsState;
48
49 String httpPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTP_PORT.getProperty());
50 String httpsPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTPS_PORT.getProperty());
51 httpExpected = httpPort != null || httpsPort != null;
52
53 String sshdPort = this.cmsState.getDeployProperty(CmsDeployProperty.SSHD_PORT.getProperty());
54 sshdExpected = sshdPort != null;
55 }
56
57 public void setHttpServer(HttpServer httpServer) {
58 this.httpServer = httpServer;
59 // create contexts whose handles had already been published
60 for (String contextPath : httpHandlers.keySet()) {
61 HttpHandler httpHandler = httpHandlers.get(contextPath);
62 CmsAuthenticator authenticator = httpAuthenticators.get(contextPath);
63 createHttpContext(contextPath, httpHandler, authenticator);
64 }
65 }
66
67 public void addHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
68 final String contextPath = properties.get(CONTEXT_PATH);
69 if (contextPath == null) {
70 log.warn("Property " + CONTEXT_PATH + " not set on HTTP handler " + properties + ". Ignoring it.");
71 return;
72 }
73 boolean isPublic = Boolean.parseBoolean(properties.get(CmsConstants.CONTEXT_PUBLIC));
74 CmsAuthenticator authenticator = isPublic ? new PublicCmsAuthenticator() : new CmsAuthenticator();
75 httpHandlers.put(contextPath, httpHandler);
76 httpAuthenticators.put(contextPath, authenticator);
77 if (httpServer == null) {
78 return;
79 } else {
80 createHttpContext(contextPath, httpHandler, authenticator);
81 }
82 }
83
84 public void createHttpContext(String contextPath, HttpHandler httpHandler, CmsAuthenticator authenticator) {
85 HttpContext httpContext = httpServer.createContext(contextPath);
86 // we want to set the authenticator BEFORE the handler actually becomes active
87 httpContext.setAuthenticator(authenticator);
88 httpContext.setHandler(httpHandler);
89 log.debug(() -> "Added handler " + contextPath + " : " + httpHandler.getClass().getName());
90 }
91
92 public void removeHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
93 final String contextPath = properties.get(CmsConstants.CONTEXT_PATH);
94 if (contextPath == null)
95 return; // ignore silently
96 httpHandlers.remove(contextPath);
97 if (httpServer == null)
98 return;
99 httpServer.removeContext(contextPath);
100 log.debug(() -> "Removed handler " + contextPath + " : " + httpHandler.getClass().getName());
101 }
102
103 public boolean allExpectedServicesAvailable() {
104 if (httpExpected && httpServer == null)
105 return false;
106 if (sshdExpected && cmsSshd == null)
107 return false;
108 return true;
109 }
110
111 public void setCmsSshd(CmsSshd cmsSshd) {
112 this.cmsSshd = cmsSshd;
113 }
114
115 }