mschaefers
2012-12-03 296ace81ee4f6d7d405071a91706511d0aa1bc6d
cache results of LDAP user synchronization for a configurable amount of time (realm.ldap.ldapCachePeriod = 2 MINUTES)
2 files modified
38 ■■■■ changed files
distrib/gitblit.properties 9 ●●●●● patch | view | raw | blame | history
src/com/gitblit/LdapUserService.java 29 ●●●● patch | view | raw | blame | history
distrib/gitblit.properties
@@ -1057,6 +1057,15 @@
# SINCE 1.0.0
realm.ldap.email = email
# Defines the cache period to be used when caching LDAP queries. This is currently
# only used for LDAP user synchronization.
#
# Must be of the form '<long> <TimeUnit>' where <TimeUnit> is one of 'MILLISECONDS', 'SECONDS', 'MINUTES', 'HOURS', 'DAYS'
# default: 2 MINUTES
#
# RESTART REQUIRED
realm.ldap.ldapCachePeriod = 2 MINUTES
# Defines whether to synchronize all LDAP users into the backing user service
#
# Valid values: true, false
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 {