Example usage for javax.naming.ldap LdapContext close

List of usage examples for javax.naming.ldap LdapContext close

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext close.

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this context.

Usage

From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java

/**
 * Execute Batch Sync. Will update all Attributes of LDAP users in OLAt, create new users and delete users in OLAT.
 * Can be configured in ldapContext.xml/*from w  w  w .j  a v  a2s .  co  m*/
 * 
 * @param LDAPError
 * 
 */
@Override
public boolean doBatchSync(LDAPError errors, boolean full) {
    //fxdiff: also run on nodes != 1 as nodeid = tomcat-id in fx-environment
    //      if(WebappHelper.getNodeId() != 1) {
    //         log.warn("Sync happens only on node 1", null);
    //         return false;
    //      }

    // o_clusterNOK
    // Synchronize on class so that only one thread can read the
    // batchSyncIsRunning flag Only this read operation is synchronized to not
    // block the whole execution of the do BatchSync method. The method is used
    // in automatic cron scheduler job and also in GUI controllers that can't
    // wait for the concurrent running request to finish first, an immediate
    // feedback about the concurrent job is needed. -> only synchronize on the
    // property read.
    synchronized (LDAPLoginManagerImpl.class) {
        if (batchSyncIsRunning) {
            // don't run twice, skip this execution
            log.info("LDAP user doBatchSync started, but another job is still running - skipping this sync");
            errors.insert("BatchSync already running by concurrent process");
            return false;
        }
    }

    WorkThreadInformations.setLongRunningTask("ldapSync");

    coordinator.getEventBus().fireEventToListenersOf(new LDAPEvent(LDAPEvent.SYNCHING), ldapSyncLockOres);

    if (full) {
        lastSyncDate = null;
    }

    LdapContext ctx = null;
    boolean success = false;
    try {
        acquireSyncLock();
        ctx = bindSystem();
        if (ctx == null) {
            errors.insert("LDAP connection ERROR");
            log.error("Error in LDAP batch sync: LDAP connection empty", null);
            freeSyncLock();
            success = false;
            return success;
        }
        Date timeBeforeSync = new Date();

        //check server capabilities
        // Get time before sync to have a save sync time when sync is successful
        String sinceSentence = (lastSyncDate == null ? " (full sync)"
                : " since last sync from " + lastSyncDate);
        doBatchSyncDeletedUsers(ctx, sinceSentence);
        // bind again to use an initial unmodified context. lookup of server-properties might fail otherwise!
        ctx.close();
        ctx = bindSystem();
        Map<String, LDAPUser> dnToIdentityKeyMap = new HashMap<>();
        List<LDAPUser> ldapUsers = doBatchSyncNewAndModifiedUsers(ctx, sinceSentence, dnToIdentityKeyMap,
                errors);
        ctx.close();
        ctx = bindSystem();
        //sync groups by LDAP groups or attributes
        doBatchSyncGroups(ctx, ldapUsers, dnToIdentityKeyMap, errors);
        //sync roles
        doBatchSyncRoles(ctx, ldapUsers, dnToIdentityKeyMap, errors);

        // update sync time and set running flag
        lastSyncDate = timeBeforeSync;

        ctx.close();
        success = true;
        return success;
    } catch (Exception e) {

        errors.insert("Unknown error");
        log.error("Error in LDAP batch sync, unknown reason", e);
        success = false;
        return success;
    } finally {
        WorkThreadInformations.unsetLongRunningTask("ldapSync");
        freeSyncLock();
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                //try but failed silently
            }
        }
        LDAPEvent endEvent = new LDAPEvent(LDAPEvent.SYNCHING_ENDED);
        endEvent.setTimestamp(new Date());
        endEvent.setSuccess(success);
        endEvent.setErrors(errors);
        coordinator.getEventBus().fireEventToListenersOf(endEvent, ldapSyncLockOres);
    }
}

From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java

private void doBatchSyncRoles(LdapContext ctx, List<LDAPUser> ldapUsers,
        Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) throws NamingException {
    ctx.close();
    ctx = bindSystem();/*from w  ww  .j  a  va 2 s  .  c  om*/

    //authors
    if (syncConfiguration.getAuthorsGroupBase() != null && syncConfiguration.getAuthorsGroupBase().size() > 0) {
        List<LDAPGroup> authorGroups = ldapDao.searchGroups(ctx, syncConfiguration.getAuthorsGroupBase());
        syncRole(ctx, authorGroups, Constants.GROUP_AUTHORS, dnToIdentityKeyMap, errors);
    }
    //user managers
    if (syncConfiguration.getUserManagersGroupBase() != null
            && syncConfiguration.getUserManagersGroupBase().size() > 0) {
        List<LDAPGroup> userManagerGroups = ldapDao.searchGroups(ctx,
                syncConfiguration.getUserManagersGroupBase());
        syncRole(ctx, userManagerGroups, Constants.GROUP_USERMANAGERS, dnToIdentityKeyMap, errors);
    }
    //group managers
    if (syncConfiguration.getGroupManagersGroupBase() != null
            && syncConfiguration.getGroupManagersGroupBase().size() > 0) {
        List<LDAPGroup> groupManagerGroups = ldapDao.searchGroups(ctx,
                syncConfiguration.getGroupManagersGroupBase());
        syncRole(ctx, groupManagerGroups, Constants.GROUP_GROUPMANAGERS, dnToIdentityKeyMap, errors);
    }
    //question pool managers
    if (syncConfiguration.getQpoolManagersGroupBase() != null
            && syncConfiguration.getQpoolManagersGroupBase().size() > 0) {
        List<LDAPGroup> qpoolManagerGroups = ldapDao.searchGroups(ctx,
                syncConfiguration.getQpoolManagersGroupBase());
        syncRole(ctx, qpoolManagerGroups, Constants.GROUP_POOL_MANAGER, dnToIdentityKeyMap, errors);
    }
    //learning resource manager
    if (syncConfiguration.getLearningResourceManagersGroupBase() != null
            && syncConfiguration.getLearningResourceManagersGroupBase().size() > 0) {
        List<LDAPGroup> resourceManagerGroups = ldapDao.searchGroups(ctx,
                syncConfiguration.getLearningResourceManagersGroupBase());
        syncRole(ctx, resourceManagerGroups, Constants.GROUP_INST_ORES_MANAGER, dnToIdentityKeyMap, errors);
    }

    int count = 0;
    boolean syncAuthor = StringHelper.containsNonWhitespace(syncConfiguration.getAuthorRoleAttribute())
            && StringHelper.containsNonWhitespace(syncConfiguration.getAuthorRoleValue());
    boolean syncUserManager = StringHelper
            .containsNonWhitespace(syncConfiguration.getUserManagerRoleAttribute())
            && StringHelper.containsNonWhitespace(syncConfiguration.getUserManagerRoleValue());
    boolean syncGroupManager = StringHelper
            .containsNonWhitespace(syncConfiguration.getGroupManagerRoleAttribute())
            && StringHelper.containsNonWhitespace(syncConfiguration.getGroupManagerRoleValue());
    boolean syncQpoolManager = StringHelper
            .containsNonWhitespace(syncConfiguration.getQpoolManagerRoleAttribute())
            && StringHelper.containsNonWhitespace(syncConfiguration.getQpoolManagerRoleValue());
    boolean syncLearningResourceManager = StringHelper
            .containsNonWhitespace(syncConfiguration.getLearningResourceManagerRoleAttribute())
            && StringHelper.containsNonWhitespace(syncConfiguration.getLearningResourceManagerRoleValue());

    for (LDAPUser ldapUser : ldapUsers) {
        if (syncAuthor && ldapUser.isAuthor()) {
            syncRole(ldapUser, Constants.GROUP_AUTHORS);
            count++;
        }
        if (syncUserManager && ldapUser.isUserManager()) {
            syncRole(ldapUser, Constants.GROUP_USERMANAGERS);
            count++;
        }
        if (syncGroupManager && ldapUser.isGroupManager()) {
            syncRole(ldapUser, Constants.GROUP_GROUPMANAGERS);
            count++;
        }
        if (syncQpoolManager && ldapUser.isQpoolManager()) {
            syncRole(ldapUser, Constants.GROUP_POOL_MANAGER);
            count++;
        }
        if (syncLearningResourceManager && ldapUser.isLearningResourceManager()) {
            syncRole(ldapUser, Constants.GROUP_INST_ORES_MANAGER);
            count++;
        }
        if (count > 20) {
            dbInstance.commitAndCloseSession();
            count = 0;
        }
    }

    dbInstance.commitAndCloseSession();
}

From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java

private void doBatchSyncGroups(LdapContext ctx, List<LDAPUser> ldapUsers,
        Map<String, LDAPUser> dnToIdentityKeyMap, LDAPError errors) throws NamingException {
    ctx.close();

    log.info("LDAP batch sync LDAP user to OO groups");

    ctx = bindSystem();//from w w w  . j  av a 2s  .co  m
    //sync groups by LDAP groups or attributes
    Map<String, LDAPGroup> cnToGroupMap = new HashMap<>();

    // retrieve all ldap group's with their list of members
    if (syncConfiguration.syncGroupWithLDAPGroup()) {
        List<String> groupDNs = syncConfiguration.getLdapGroupBases();
        List<LDAPGroup> ldapGroups = ldapDao.searchGroups(ctx, groupDNs);
        for (LDAPGroup ldapGroup : ldapGroups) {
            cnToGroupMap.put(ldapGroup.getCommonName(), ldapGroup);
        }
    }
    if (syncConfiguration.syncGroupWithAttribute()) {
        doSyncGroupByAttribute(ldapUsers, cnToGroupMap);
    }

    for (LDAPGroup group : cnToGroupMap.values()) {
        BusinessGroup managedGroup = getManagerBusinessGroup(group.getCommonName());
        if (managedGroup != null) {
            syncBusinessGroup(ctx, managedGroup, group, dnToIdentityKeyMap, errors);
        }
        dbInstance.commitAndCloseSession();
    }
}

From source file:org.sonatype.nexus.ldap.internal.connector.DefaultLdapConnector.java

private void closeContext(LdapContext context) {
    try {//  w  w  w .java  2  s  .  c om
        if (context != null) {
            context.close();
        }
    } catch (NamingException e) {
        this.log.debug("Error closing connection: " + e.getMessage(), e);
    }
}

From source file:org.tolven.gatekeeper.bean.LdapBean.java

private void close(LdapContext ctx, String realm) {
    try {/*ww w.ja v a 2s. co m*/
        if (ctx != null) {
            ctx.close();
        }
    } catch (Exception ex) {
        throw new RuntimeException("Could not close ldap context for realm: " + realm, ex);
    }
}