From: Mathieu Baudier Date: Sat, 29 Oct 2022 05:45:13 +0000 (+0200) Subject: Fix race condition by OSGi start up X-Git-Tag: v2.3.11~63 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=0a0a90e832a2fe24dc55d1cf67d75e276ad61b2c Fix race condition by OSGi start up --- diff --git a/swt/rcp/org.argeo.cms.swt.rcp/OSGI-INF/cmsRcpServletFactory.xml b/swt/rcp/org.argeo.cms.swt.rcp/OSGI-INF/cmsRcpServletFactory.xml index fe0bc64db..5f6b94e04 100644 --- a/swt/rcp/org.argeo.cms.swt.rcp/OSGI-INF/cmsRcpServletFactory.xml +++ b/swt/rcp/org.argeo.cms.swt.rcp/OSGI-INF/cmsRcpServletFactory.xml @@ -1,6 +1,6 @@ - + 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) {