浏览代码

improve profile config init process

Jason Rivard 3 年之前
父节点
当前提交
c05948b2d4
共有 1 个文件被更改,包括 23 次插入12 次删除
  1. 23 12
      server/src/main/java/password/pwm/config/DomainConfig.java

+ 23 - 12
server/src/main/java/password/pwm/config/DomainConfig.java

@@ -62,7 +62,6 @@ import java.io.StringWriter;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -85,7 +84,8 @@ public class DomainConfig implements SettingReader
 
     private final ConfigurationSuppliers configurationSuppliers = new ConfigurationSuppliers();
 
-    private final DataCache dataCache = new DataCache();
+    private final Map<String, PwmPasswordPolicy> cachedPasswordPolicy;
+    private final Map<String, Map<Locale, ChallengeProfile>> cachedChallengeProfiles;
     private final StoredSettingReader settingReader;
 
     public DomainConfig( final AppConfig appConfig, final DomainID domainID )
@@ -94,8 +94,25 @@ public class DomainConfig implements SettingReader
         this.storedConfiguration = appConfig.getStoredConfiguration();
         this.domainID = Objects.requireNonNull( domainID );
         this.settingReader = new StoredSettingReader( storedConfiguration, null, domainID );
-    }
 
+        this.cachedPasswordPolicy = Map.copyOf( getPasswordProfileIDs().stream()
+                .map( profile -> PwmPasswordPolicy.createPwmPasswordPolicy( this, profile ) )
+                .collect( Collectors.toMap(
+                        PwmPasswordPolicy::getIdentifier,
+                        pwmPasswordPolicy -> pwmPasswordPolicy
+                ) ) );
+
+        this.cachedChallengeProfiles = Map.copyOf( getChallengeProfileIDs().stream()
+                .collect( Collectors.toMap(
+                        profileId -> profileId,
+                        profileId -> Map.copyOf( appConfig.getKnownLocales().stream()
+                                .collect( Collectors.toMap(
+                                        locale -> locale,
+                                        locale -> ChallengeProfile.readChallengeProfileFromConfig( domainID, profileId, locale, storedConfiguration )
+                                ) ) )
+                ) ) );
+    }
+    
     public AppConfig getAppConfig()
     {
         return appConfig;
@@ -184,7 +201,7 @@ public class DomainConfig implements SettingReader
             throw new IllegalArgumentException( "unknown challenge profileID specified: " + profile );
         }
 
-        return ChallengeProfile.readChallengeProfileFromConfig( getDomainID(), profile, locale, storedConfiguration );
+        return cachedChallengeProfiles.get( profile ).get( locale );
     }
 
     public long readSettingAsLong( final PwmSetting setting )
@@ -194,8 +211,7 @@ public class DomainConfig implements SettingReader
 
     public PwmPasswordPolicy getPasswordPolicy( final String profile )
     {
-        return dataCache.cachedPasswordPolicy
-                .computeIfAbsent( profile, s -> PwmPasswordPolicy.createPwmPasswordPolicy( this, profile ) );
+        return cachedPasswordPolicy.get( profile );
     }
 
     public List<String> getPasswordProfileIDs( )
@@ -263,7 +279,7 @@ public class DomainConfig implements SettingReader
     {
         return appConfig.readAppProperty( property );
     }
-    
+
     public DomainID getDomainID()
     {
         return domainID;
@@ -302,11 +318,6 @@ public class DomainConfig implements SettingReader
         } );
     }
 
-    private static class DataCache
-    {
-        private final Map<String, PwmPasswordPolicy> cachedPasswordPolicy = new LinkedHashMap<>();
-    }
-
     /* generic profile stuff */
     public Map<String, NewUserProfile> getNewUserProfiles( )
     {