]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/osgi/DeployConfig.java
Introduce static CMS.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / osgi / DeployConfig.java
index c31f50ded9c8ad8f98a0b40f45d5e0a3758760fa..c575b1da31cefa66f32140c823ad7c836ca58080 100644 (file)
@@ -7,7 +7,9 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Dictionary;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
@@ -27,8 +29,6 @@ import org.argeo.osgi.useradmin.UserAdminConf;
 import org.argeo.util.naming.AttributesDictionary;
 import org.argeo.util.naming.LdifParser;
 import org.argeo.util.naming.LdifWriter;
-import org.eclipse.equinox.http.jetty.JettyConfigurator;
-import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
@@ -37,6 +37,16 @@ import org.osgi.service.cm.ConfigurationListener;
 
 /** Manages the LDIF-based deployment configuration. */
 public class DeployConfig implements ConfigurationListener {
+       private final static LdapName USER_ADMIN_BASE_DN;
+       static {
+               try {
+                       USER_ADMIN_BASE_DN = new LdapName(
+                                       CmsConstants.OU + "=" + CmsConstants.NODE_USER_ADMIN_PID + "," + CmsConstants.DEPLOY_BASEDN);
+               } catch (InvalidNameException e) {
+                       throw new IllegalArgumentException(e);
+               }
+       }
+
        private final CmsLog log = CmsLog.getLog(getClass());
 //     private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
 
@@ -50,24 +60,6 @@ public class DeployConfig implements ConfigurationListener {
 
        private ConfigurationAdmin configurationAdmin;
 
-       public DeployConfig() {
-//             this.dataModels = dataModels;
-               // ConfigurationAdmin configurationAdmin =
-//             // bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
-//             try {
-//                     if (!isInitialized()) { // first init
-//                             isFirstInit = true;
-//                             firstInit();
-//                     }
-//                     this.configurationAdmin = configurationAdmin;
-////                   init(configurationAdmin, isClean, isFirstInit);
-//             } catch (IOException e) {
-//                     throw new RuntimeException("Could not init deploy configs", e);
-//             }
-               // FIXME check race conditions during initialization
-               // bc.registerService(ConfigurationListener.class, this, null);
-       }
-
        private void firstInit() throws IOException {
                log.info("## FIRST INIT ##");
                Files.createDirectories(deployConfigPath.getParent());
@@ -120,92 +112,62 @@ public class DeployConfig implements ConfigurationListener {
                }
 
                // http server
-//             Dictionary<String, Object> webServerConfig = InitUtils
-//                             .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, NodeConstants.DEFAULT));
-//             if (!webServerConfig.isEmpty()) {
-//                     // TODO check for other customizers
+               Dictionary<String, Object> webServerConfig = InitUtils
+                               .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT));
+               if (!webServerConfig.isEmpty()) {
+                       // TODO check for other customizers
 //                     webServerConfig.put("customizer.class", "org.argeo.equinox.jetty.CmsJettyCustomizer");
-//                     putFactoryDeployConfig(KernelConstants.JETTY_FACTORY_PID, webServerConfig);
-//             }
-               LdapName defaultHttpServiceDn = serviceDn(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT);
-               if (deployConfigs.containsKey(defaultHttpServiceDn)) {
-                       // remove old default configs since we have now to start Jetty servlet bridge
-                       // indirectly
-                       deployConfigs.remove(defaultHttpServiceDn);
+                       putFactoryDeployConfig(KernelConstants.JETTY_FACTORY_PID, webServerConfig);
                }
+//             LdapName defaultHttpServiceDn = serviceDn(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT);
+//             if (deployConfigs.containsKey(defaultHttpServiceDn)) {
+//                     // remove old default configs since we have now to start Jetty servlet bridge
+//                     // indirectly
+//                     deployConfigs.remove(defaultHttpServiceDn);
+//             }
 
                // SAVE
                save();
                //
 
-               // Explicitly configures Jetty so that the default server is not started by the
-               // activator of the Equinox Jetty bundle.
-               Dictionary<String, Object> webServerConfig = InitUtils
-                               .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT));
-//             if (!webServerConfig.isEmpty()) {
-//                     webServerConfig.put("customizer.class", KernelConstants.CMS_JETTY_CUSTOMIZER_CLASS);
-//
-//                     // TODO centralise with Jetty extender
-//                     Object webSocketEnabled = webServerConfig.get(InternalHttpConstants.WEBSOCKET_ENABLED);
-//                     if (webSocketEnabled != null && webSocketEnabled.toString().equals("true")) {
-//                             bc.registerService(ServerEndpointConfig.Configurator.class, new CmsWebSocketConfigurator(), null);
-//                             webServerConfig.put(InternalHttpConstants.WEBSOCKET_ENABLED, "true");
-//                     }
-//             }
+//             Dictionary<String, Object> webServerConfig = InitUtils
+//                             .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT));
+       }
 
-               int tryCount = 60;
+       public void start() {
                try {
-                       tryGettyJetty: while (tryCount > 0) {
-                               try {
-                                       JettyConfigurator.startServer(KernelConstants.DEFAULT_JETTY_SERVER, webServerConfig);
-                                       // Explicitly starts Jetty OSGi HTTP bundle, so that it gets triggered if OSGi
-                                       // configuration is not cleaned
-                                       FrameworkUtil.getBundle(JettyConfigurator.class).start();
-                                       break tryGettyJetty;
-                               } catch (IllegalStateException e) {
-                                       // Jetty may not be ready
-                                       try {
-                                               Thread.sleep(1000);
-                                       } catch (Exception e1) {
-                                               // silent
-                                       }
-                                       tryCount--;
-                               }
+                       if (!isInitialized()) { // first init
+                               isFirstInit = true;
+                               firstInit();
                        }
-               } catch (Exception e) {
-                       log.error("Cannot start default Jetty server with config " + webServerConfig, e);
-               }
-
-       }
 
-       public void init() throws IOException {
-               if (!isInitialized()) { // first init
-                       isFirstInit = true;
-                       firstInit();
-               }
-
-               boolean isClean;
-               try {
-                       Configuration[] confs = configurationAdmin
-                                       .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
-                       isClean = confs == null || confs.length == 0;
-               } catch (Exception e) {
-                       throw new IllegalStateException("Cannot analyse clean state", e);
-               }
+                       boolean isClean = true;
+                       if (configurationAdmin != null)
+                               try {
+                                       Configuration[] confs = configurationAdmin
+                                                       .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
+                                       isClean = confs == null || confs.length == 0;
+                               } catch (Exception e) {
+                                       throw new IllegalStateException("Cannot analyse clean state", e);
+                               }
 
-               try (InputStream in = Files.newInputStream(deployConfigPath)) {
-                       deployConfigs = new LdifParser().read(in);
-               }
-               if (isClean) {
-                       if (log.isDebugEnabled())
-                               log.debug("Clean state, loading from framework properties...");
-                       setFromFrameworkProperties(isFirstInit);
-                       loadConfigs();
+                       try (InputStream in = Files.newInputStream(deployConfigPath)) {
+                               deployConfigs = new LdifParser().read(in);
+                       }
+                       if (isClean) {
+                               if (log.isDebugEnabled())
+                                       log.debug("Clean state, loading from framework properties...");
+                               setFromFrameworkProperties(isFirstInit);
+                               if (configurationAdmin != null)
+                                       loadConfigs();
+                       }
+                       // TODO check consistency if not clean
+               } catch (IOException e) {
+                       throw new RuntimeException("Cannot load deploy configuration", e);
                }
-               // TODO check consistency if not clean
        }
 
-       public void destroy() {
+       public void stop() {
 
        }
 
@@ -258,6 +220,17 @@ public class DeployConfig implements ConfigurationListener {
 
        }
 
+       public Set<Dictionary<String, Object>> getUserDirectoryConfigs() {
+               Set<Dictionary<String, Object>> res = new HashSet<>();
+               for (LdapName dn : deployConfigs.keySet()) {
+                       if (dn.endsWith(USER_ADMIN_BASE_DN)) {
+                               Attributes attributes = deployConfigs.get(dn);
+                               res.add(new AttributesDictionary(attributes));
+                       }
+               }
+               return res;
+       }
+
        @Override
        public void configurationEvent(ConfigurationEvent event) {
                try {
@@ -350,6 +323,10 @@ public class DeployConfig implements ConfigurationListener {
        }
 
        public boolean hasDomain() {
+               // FIXME lookup deploy configs directly
+               if (configurationAdmin == null)
+                       return false;
+
                Configuration[] configs;
                try {
                        configs = configurationAdmin