mschaefers
2012-12-03 296ace81ee4f6d7d405071a91706511d0aa1bc6d
src/com/gitblit/LdapUserService.java
@@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -55,9 +56,25 @@
    public static final String LDAP_PASSWORD_KEY = "StoredInLDAP";
    private IStoredSettings settings;
    private long lastLdapUserSyncTs = 0L;
    private long ldapSyncCachePeriod;
   public LdapUserService() {
      super();
   }
    private void initializeLdapCaches() {
        final String cacheDuration = settings.getString(Keys.realm.ldap.ldapCachePeriod, "2 MINUTES");
        final long duration;
        final TimeUnit timeUnit;
        try {
            final String[] s = cacheDuration.split(" ", 2);
            duration = Long.parseLong(s[0]);
            timeUnit = TimeUnit.valueOf(s[1]);
            ldapSyncCachePeriod = timeUnit.toMillis(duration);
        } catch (RuntimeException ex) {
            throw new IllegalArgumentException(Keys.realm.ldap.ldapCachePeriod + " must have format '<long> <TimeUnit>' where <TimeUnit> is one of 'MILLISECONDS', 'SECONDS', 'MINUTES', 'HOURS', 'DAYS'");
        }
   }
   @Override
@@ -66,17 +83,18 @@
      String file = settings.getString(Keys.realm.ldap.backingUserService, "users.conf");
      File realmFile = GitBlit.getFileOrFolder(file);
        initializeLdapCaches();
      serviceImpl = createUserService(realmFile);
      logger.info("LDAP User Service backed by " + serviceImpl.toString());
        synchronizeLdapUsers();
    }
    protected void synchronizeLdapUsers() {
    protected synchronized void synchronizeLdapUsers() {
        final boolean enabled = settings.getBoolean(Keys.realm.ldap.synchronizeUsers.enable, false);
        if (!enabled) {
            return;
        }
        if (enabled) {
            if (lastLdapUserSyncTs + ldapSyncCachePeriod < System.currentTimeMillis()) {
        final boolean deleteRemovedLdapUsers = settings.getBoolean(Keys.realm.ldap.synchronizeUsers.removeDeleted, true);
        LDAPConnection ldapConnection = getLdapConnection();
        if (ldapConnection != null) {
@@ -135,11 +153,14 @@
                        updateTeamModels(userTeams.values());
                    }
                }
                        lastLdapUserSyncTs = System.currentTimeMillis();
            } finally {
                ldapConnection.close();
            }
        }
    }
        }
    }
    private LDAPConnection getLdapConnection() {
        try {