]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/osgi/DeployConfig.java
Adapt build to changes in Argeo TP structure
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / osgi / DeployConfig.java
index c575b1da31cefa66f32140c823ad7c836ca58080..7f8db5a062cd3fca9bd110cf7ff5e250a91a6505 100644 (file)
@@ -6,6 +6,7 @@ import java.io.Writer;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.List;
@@ -22,35 +23,23 @@ import javax.naming.ldap.Rdn;
 
 import org.argeo.api.cms.CmsConstants;
 import org.argeo.api.cms.CmsLog;
-import org.argeo.cms.internal.runtime.InitUtils;
-import org.argeo.cms.internal.runtime.KernelConstants;
-import org.argeo.cms.internal.runtime.KernelUtils;
-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.argeo.util.directory.DirectoryConf;
+import org.argeo.util.directory.ldap.AttributesDictionary;
+import org.argeo.util.directory.ldap.LdifParser;
+import org.argeo.util.directory.ldap.LdifWriter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.cm.ConfigurationEvent;
-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);
-               }
-       }
+@Deprecated
+public class DeployConfig {
 
        private final CmsLog log = CmsLog.getLog(getClass());
 //     private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
 
-       private static Path deployConfigPath = KernelUtils.getOsgiInstancePath(KernelConstants.DEPLOY_CONFIG_PATH);
+       private static Path deployConfigPath;// = KernelUtils.getOsgiInstancePath(KernelConstants.DEPLOY_CONFIG_PATH);
        private SortedMap<LdapName, Attributes> deployConfigs = new TreeMap<>();
 //     private final DataModels dataModels;
 
@@ -65,7 +54,7 @@ public class DeployConfig implements ConfigurationListener {
                Files.createDirectories(deployConfigPath.getParent());
 
                // FirstInit firstInit = new FirstInit();
-               InitUtils.prepareFirstInitInstanceArea();
+               //InitUtils.prepareFirstInitInstanceArea();
 
                if (!Files.exists(deployConfigPath))
                        deployConfigs = new TreeMap<>();
@@ -79,59 +68,50 @@ public class DeployConfig implements ConfigurationListener {
        private void setFromFrameworkProperties(boolean isFirstInit) {
 
                // user admin
-               List<Dictionary<String, Object>> userDirectoryConfigs = InitUtils.getUserDirectoryConfigs();
-               if (userDirectoryConfigs.size() != 0) {
-                       List<String> activeCns = new ArrayList<>();
-                       for (int i = 0; i < userDirectoryConfigs.size(); i++) {
-                               Dictionary<String, Object> userDirectoryConfig = userDirectoryConfigs.get(i);
-                               String baseDn = (String) userDirectoryConfig.get(UserAdminConf.baseDn.name());
-                               String cn;
-                               if (CmsConstants.ROLES_BASEDN.equals(baseDn))
-                                       cn = ROLES;
-                               else
-                                       cn = UserAdminConf.baseDnHash(userDirectoryConfig);
-                               activeCns.add(cn);
-                               userDirectoryConfig.put(CmsConstants.CN, cn);
-                               putFactoryDeployConfig(CmsConstants.NODE_USER_ADMIN_PID, userDirectoryConfig);
-                       }
-                       // disable others
-                       LdapName userAdminFactoryName = serviceFactoryDn(CmsConstants.NODE_USER_ADMIN_PID);
-                       for (LdapName name : deployConfigs.keySet()) {
-                               if (name.startsWith(userAdminFactoryName) && !name.equals(userAdminFactoryName)) {
-//                                     try {
-                                       Attributes attrs = deployConfigs.get(name);
-                                       String cn = name.getRdn(name.size() - 1).getValue().toString();
-                                       if (!activeCns.contains(cn)) {
-                                               attrs.put(UserAdminConf.disabled.name(), "true");
-                                       }
-//                                     } catch (Exception e) {
-//                                             throw new CmsException("Cannot disable user directory " + name, e);
+//             List<Dictionary<String, Object>> userDirectoryConfigs = InitUtils.getUserDirectoryConfigs();
+//             if (userDirectoryConfigs.size() != 0) {
+//                     List<String> activeCns = new ArrayList<>();
+//                     for (int i = 0; i < userDirectoryConfigs.size(); i++) {
+//                             Dictionary<String, Object> userDirectoryConfig = userDirectoryConfigs.get(i);
+//                             String baseDn = (String) userDirectoryConfig.get(DirectoryConf.baseDn.name());
+//                             String cn;
+//                             if (CmsConstants.ROLES_BASEDN.equals(baseDn))
+//                                     cn = ROLES;
+//                             else
+//                                     cn = DirectoryConf.baseDnHash(userDirectoryConfig);
+//                             activeCns.add(cn);
+//                             userDirectoryConfig.put(CmsConstants.CN, cn);
+//                             putFactoryDeployConfig(CmsConstants.NODE_USER_ADMIN_PID, userDirectoryConfig);
+//                     }
+//                     // disable others
+//                     LdapName userAdminFactoryName = serviceFactoryDn(CmsConstants.NODE_USER_ADMIN_PID);
+//                     for (LdapName name : deployConfigs.keySet()) {
+//                             if (name.startsWith(userAdminFactoryName) && !name.equals(userAdminFactoryName)) {
+////                                   try {
+//                                     Attributes attrs = deployConfigs.get(name);
+//                                     String cn = name.getRdn(name.size() - 1).getValue().toString();
+//                                     if (!activeCns.contains(cn)) {
+//                                             attrs.put(DirectoryConf.disabled.name(), "true");
 //                                     }
-                               }
-                       }
-               }
+////                                   } catch (Exception e) {
+////                                           throw new CmsException("Cannot disable user directory " + name, e);
+////                                   }
+//                             }
+//                     }
+//             }
 
                // http server
-               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);
+//             Dictionary<String, Object> webServerConfig = InitUtils
+//                             .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT));
+//             if (!webServerConfig.isEmpty()) {
+//                     // TODO check for other customizers
+//                     putFactoryDeployConfig(KernelConstants.JETTY_FACTORY_PID, webServerConfig);
 //             }
 
                // SAVE
                save();
                //
 
-//             Dictionary<String, Object> webServerConfig = InitUtils
-//                             .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, CmsConstants.DEFAULT));
        }
 
        public void start() {
@@ -171,6 +151,25 @@ public class DeployConfig implements ConfigurationListener {
 
        }
 
+       protected void logAllConfigurations() {
+               if (!log.isDebugEnabled())
+                       return;
+               try {
+                       Configuration[] configurations = configurationAdmin.listConfigurations(null);
+                       if (configurations == null) {
+                               log.debug("No configuration available");
+                               return;
+                       }
+                       Arrays.sort(configurations, (o1, o2) -> o1.getPid().compareTo(o2.getPid()));
+                       for (Configuration configuration : configurations) {
+                               log.debug(configuration.getFactoryPid() + " - " + configuration.getPid() + " - "
+                                               + configuration.getProperties());
+                       }
+               } catch (IOException | InvalidSyntaxException e) {
+                       throw new IllegalStateException("Cannot log configurations", e);
+               }
+       }
+
        public void loadConfigs() throws IOException {
                // FIXME make it more robust
                Configuration systemRolesConf = null;
@@ -182,6 +181,7 @@ public class DeployConfig implements ConfigurationListener {
                        throw new IllegalArgumentException(e);
                }
                deployConfigs: for (LdapName dn : deployConfigs.keySet()) {
+                       Attributes deployConfig = deployConfigs.get(dn);
                        Rdn lastRdn = dn.getRdn(dn.size() - 1);
                        LdapName prefix = (LdapName) dn.getPrefix(dn.size() - 1);
                        if (prefix.toString().equals(CmsConstants.DEPLOY_BASEDN)) {
@@ -189,26 +189,53 @@ public class DeployConfig implements ConfigurationListener {
                                        // service
                                        String pid = lastRdn.getValue().toString();
                                        Configuration conf = configurationAdmin.getConfiguration(pid);
-                                       AttributesDictionary dico = new AttributesDictionary(deployConfigs.get(dn));
+                                       AttributesDictionary dico = new AttributesDictionary(deployConfig);
                                        conf.update(dico);
                                } else {
                                        // service factory definition
                                }
                        } else {
-                               Attributes config = deployConfigs.get(dn);
-                               Attribute disabled = config.get(UserAdminConf.disabled.name());
+                               Attribute disabled = deployConfig.get(DirectoryConf.disabled.name());
                                if (disabled != null)
                                        continue deployConfigs;
                                // service factory service
+                               if (!lastRdn.getType().equals(CmsConstants.CN))
+                                       throw new IllegalStateException("Only " + CmsConstants.CN + "= is supported: " + dn);
                                Rdn beforeLastRdn = dn.getRdn(dn.size() - 2);
                                assert beforeLastRdn.getType().equals(CmsConstants.OU);
                                String factoryPid = beforeLastRdn.getValue().toString();
-                               Configuration conf = configurationAdmin.createFactoryConfiguration(factoryPid.toString(), null);
-                               if (systemRolesDn.equals(dn)) {
-                                       systemRolesConf = configurationAdmin.createFactoryConfiguration(factoryPid.toString(), null);
+
+                               String cn = lastRdn.getValue().toString();
+                               Configuration conf = getSingleServiceConfiguration(factoryPid, cn);
+                               if (conf != null) {
+                                       if (systemRolesDn.equals(dn))
+                                               systemRolesConf = conf;
+                                       // TODO deal with modifications
+//                                     boolean modified = false;
+//                                     Dictionary<String, Object> currentProperties = conf.getProperties();
+//
+//                                     attrs: for (NamingEnumeration<? extends Attribute> it = deployConfig.getAll(); it
+//                                                     .hasMoreElements();) {
+//                                             Attribute attr = (Attribute) it.next();
+//                                             String key = attr.getID();
+//                                             Object currentValue = currentProperties.get(key);
+//                                             if (currentValue == null) {
+//                                                     modified = true;
+//                                                     break attrs;
+//                                             }
+//                                     }
+
+//                                     AttributesDictionary dico = new AttributesDictionary(deployConfig);
+//                                     conf.update(dico);
                                } else {
-                                       AttributesDictionary dico = new AttributesDictionary(config);
-                                       conf.update(dico);
+
+                                       conf = configurationAdmin.createFactoryConfiguration(factoryPid.toString(), null);
+                                       if (systemRolesDn.equals(dn)) {
+                                               systemRolesConf = configurationAdmin.createFactoryConfiguration(factoryPid.toString(), null);
+                                       } else {
+                                               AttributesDictionary dico = new AttributesDictionary(deployConfig);
+                                               conf.update(dico);
+                                       }
                                }
                        }
                }
@@ -218,9 +245,18 @@ public class DeployConfig implements ConfigurationListener {
                        throw new IllegalStateException("System roles are not configured.");
                systemRolesConf.update(new AttributesDictionary(deployConfigs.get(systemRolesDn)));
 
+//             logAllConfigurations();
        }
 
        public Set<Dictionary<String, Object>> getUserDirectoryConfigs() {
+               // not static because class is not supported by Android
+               final LdapName USER_ADMIN_BASE_DN;
+               try {
+                       USER_ADMIN_BASE_DN = new LdapName(
+                                       CmsConstants.OU + "=" + CmsConstants.NODE_USER_ADMIN_PID + "," + CmsConstants.DEPLOY_BASEDN);
+               } catch (InvalidNameException e) {
+                       throw new IllegalArgumentException(e);
+               }
                Set<Dictionary<String, Object>> res = new HashSet<>();
                for (LdapName dn : deployConfigs.keySet()) {
                        if (dn.endsWith(USER_ADMIN_BASE_DN)) {
@@ -231,7 +267,7 @@ public class DeployConfig implements ConfigurationListener {
                return res;
        }
 
-       @Override
+//     @Override
        public void configurationEvent(ConfigurationEvent event) {
                try {
                        if (ConfigurationEvent.CM_UPDATED == event.getType()) {
@@ -327,17 +363,11 @@ public class DeployConfig implements ConfigurationListener {
                if (configurationAdmin == null)
                        return false;
 
-               Configuration[] configs;
-               try {
-                       configs = configurationAdmin
-                                       .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
-               } catch (IOException | InvalidSyntaxException e) {
-                       throw new IllegalStateException("Cannot list user directories", e);
-               }
+               Configuration[] configs = listConfigurationsByFactory(CmsConstants.NODE_USER_ADMIN_PID);
 
                boolean hasDomain = false;
                for (Configuration config : configs) {
-                       Object realm = config.getProperties().get(UserAdminConf.realm.name());
+                       Object realm = config.getProperties().get(DirectoryConf.realm.name());
                        if (realm != null) {
                                log.debug("Found realm: " + realm);
                                hasDomain = true;
@@ -346,6 +376,34 @@ public class DeployConfig implements ConfigurationListener {
                return hasDomain;
        }
 
+       private Configuration[] listConfigurationsByFactory(String factoryPid) {
+               try {
+                       Configuration[] configs = configurationAdmin.listConfigurations("(service.factoryPid=" + factoryPid + ")");
+                       if (configs == null)
+                               configs = new Configuration[0];
+                       return configs;
+               } catch (IOException | InvalidSyntaxException e) {
+                       throw new IllegalStateException("Cannot list configurations with factoryPid " + factoryPid, e);
+               }
+
+       }
+
+       private Configuration getSingleServiceConfiguration(String factoryPid, String cn) {
+               Configuration[] configs = listConfigurationsByFactory(factoryPid);
+               List<Configuration> res = new ArrayList<>();
+               for (Configuration config : configs) {
+                       Object currentCn = config.getProperties().get(CmsConstants.CN);
+                       if (currentCn != null && cn.equals(currentCn.toString()))
+                               res.add(config);
+               }
+               if (res.size() == 0)
+                       return null;
+               if (res.size() > 1)
+                       throw new IllegalStateException(
+                                       "More than one " + factoryPid + " configuration returned for " + CmsConstants.CN + "=" + cn);
+               return res.get(0);
+       }
+
        /*
         * UTILITIES
         */