X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=swt%2Frcp%2Forg.argeo.cms.swt.rcp%2Fsrc%2Forg%2Fargeo%2Fcms%2Fui%2Frcp%2Fservlet%2FCmsRcpServletFactory.java;h=9c2f3095cb5dc987f6055e66ded44bce0eb28e52;hb=0a0a90e832a2fe24dc55d1cf67d75e276ad61b2c;hp=09b1e41b4f82b9aa5f4f4f9e03c593f7e7e1f39b;hpb=8c6e16aa43d9523e1ec57a41a06b3ceba7d23fdb;p=lgpl%2Fargeo-commons.git diff --git a/swt/rcp/org.argeo.cms.swt.rcp/src/org/argeo/cms/ui/rcp/servlet/CmsRcpServletFactory.java b/swt/rcp/org.argeo.cms.swt.rcp/src/org/argeo/cms/ui/rcp/servlet/CmsRcpServletFactory.java index 09b1e41b4..9c2f3095c 100644 --- a/swt/rcp/org.argeo.cms.swt.rcp/src/org/argeo/cms/ui/rcp/servlet/CmsRcpServletFactory.java +++ b/swt/rcp/org.argeo.cms.swt.rcp/src/org/argeo/cms/ui/rcp/servlet/CmsRcpServletFactory.java @@ -12,6 +12,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; +import java.util.concurrent.CompletableFuture; import org.argeo.api.cms.CmsApp; import org.argeo.cms.ui.rcp.CmsRcpDisplayFactory; @@ -23,10 +24,7 @@ import com.sun.net.httpserver.HttpServer; /** Publishes one {@link CmsRcpServlet} per {@link CmsApp}. */ public class CmsRcpServletFactory { private final static Logger logger = System.getLogger(CmsRcpServletFactory.class.getName()); - private HttpServer httpServer; -// private BundleContext bundleContext = FrameworkUtil.getBundle(CmsRcpServletFactory.class).getBundleContext(); -// -// private Map> registrations = Collections.synchronizedMap(new HashMap<>()); + private CompletableFuture httpServer =new CompletableFuture<>(); public void init() { @@ -46,37 +44,33 @@ public class CmsRcpServletFactory { public void addCmsApp(CmsApp cmsApp, Map properties) { final String contextName = properties.get(CmsApp.CONTEXT_NAME_PROPERTY); if (contextName != null) { - httpServer.createContext("/" + contextName, new HttpHandler() { + httpServer.thenAcceptAsync((httpServer) -> { + httpServer.createContext("/" + contextName, new HttpHandler() { - @Override - public void handle(HttpExchange exchange) throws IOException { - String path = exchange.getRequestURI().getPath(); - String uiName = path != null ? path.substring(path.lastIndexOf('/') + 1) : ""; - CmsRcpDisplayFactory.openCmsApp(cmsApp, uiName, null); - exchange.sendResponseHeaders(200, -1); - logger.log(Level.DEBUG, "Opened RCP UI " + uiName + " of CMS App /" + contextName); - } + @Override + public void handle(HttpExchange exchange) throws IOException { + String path = exchange.getRequestURI().getPath(); + String uiName = path != null ? path.substring(path.lastIndexOf('/') + 1) : ""; + CmsRcpDisplayFactory.openCmsApp(cmsApp, uiName, null); + exchange.sendResponseHeaders(200, -1); + logger.log(Level.DEBUG, "Opened RCP UI " + uiName + " of CMS App /" + contextName); + } + }); }); logger.log(Level.DEBUG, "Registered RCP CMS APP /" + contextName); -// CmsRcpServlet servlet = new CmsRcpServlet(cmsApp); -// Hashtable serviceProperties = new Hashtable<>(); -// serviceProperties.put("osgi.http.whiteboard.servlet.pattern", "/" + contextName + "/*"); -// ServiceRegistration sr = bundleContext.registerService(Servlet.class, servlet, serviceProperties); -// registrations.put(contextName, sr); } } public void removeCmsApp(CmsApp cmsApp, Map properties) { String contextName = properties.get(CmsApp.CONTEXT_NAME_PROPERTY); if (contextName != null) { - httpServer.removeContext("/" + contextName); -// ServiceRegistration sr = registrations.get(contextName); -// sr.unregister(); + httpServer.thenAcceptAsync((httpServer) -> { + httpServer.removeContext("/" + contextName); + }); } } public void setHttpServer(HttpServer httpServer) { - this.httpServer = httpServer; Integer httpPort = httpServer.getAddress().getPort(); String baseUrl = "http://localhost:" + httpPort + "/"; Path runFile = CmsRcpDisplayFactory.getUrlRunFile(); @@ -100,6 +94,7 @@ public class CmsRcpServletFactory { throw new RuntimeException("Cannot write run file to " + runFile, e); } logger.log(DEBUG, "RCP available under " + baseUrl + ", written to " + runFile); + this.httpServer.complete(httpServer); } protected boolean isPortAvailable(int port) {