Fix race condition by OSGi start up
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 29 Oct 2022 05:45:13 +0000 (07:45 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 29 Oct 2022 05:45:13 +0000 (07:45 +0200)
swt/rcp/org.argeo.cms.swt.rcp/OSGI-INF/cmsRcpServletFactory.xml
swt/rcp/org.argeo.cms.swt.rcp/src/org/argeo/cms/ui/rcp/servlet/CmsRcpServletFactory.java

index fe0bc64dbe7bab4f29efcb99f52c94dbf0780c25..5f6b94e04d50c530a4c08da7f89b0c492e7dab03 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="init" deactivate="destroy" name="CMS RCP Servlet Factory">
    <implementation class="org.argeo.cms.ui.rcp.servlet.CmsRcpServletFactory"/>
-   <reference bind="addCmsApp" cardinality="0..n" interface="org.argeo.api.cms.CmsApp" name="CmsApp" policy="dynamic" unbind="removeCmsApp"/>
    <reference bind="setHttpServer" cardinality="1..1" interface="com.sun.net.httpserver.HttpServer" name="HttpServer" policy="static"/>
+   <reference bind="addCmsApp" cardinality="0..n" interface="org.argeo.api.cms.CmsApp" name="CmsApp" policy="dynamic" unbind="removeCmsApp"/>
 </scr:component>
index 09b1e41b4f82b9aa5f4f4f9e03c593f7e7e1f39b..9c2f3095cb5dc987f6055e66ded44bce0eb28e52 100644 (file)
@@ -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<String, ServiceRegistration<Servlet>> registrations = Collections.synchronizedMap(new HashMap<>());
+       private CompletableFuture<HttpServer> httpServer =new CompletableFuture<>();
 
        public void init() {
 
@@ -46,37 +44,33 @@ public class CmsRcpServletFactory {
        public void addCmsApp(CmsApp cmsApp, Map<String, String> 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<String, String> serviceProperties = new Hashtable<>();
-//                     serviceProperties.put("osgi.http.whiteboard.servlet.pattern", "/" + contextName + "/*");
-//                     ServiceRegistration<Servlet> sr = bundleContext.registerService(Servlet.class, servlet, serviceProperties);
-//                     registrations.put(contextName, sr);
                }
        }
 
        public void removeCmsApp(CmsApp cmsApp, Map<String, String> properties) {
                String contextName = properties.get(CmsApp.CONTEXT_NAME_PROPERTY);
                if (contextName != null) {
-                       httpServer.removeContext("/" + contextName);
-//                     ServiceRegistration<Servlet> 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) {