Search in sources :

Example 1 with PerunUser

use of cz.metacentrum.perun.ldapc.model.PerunUser in project perun by CESNET.

the class UserSynchronizer method synchronizeUsers.

public void synchronizeUsers() {
    PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
    ThreadPoolTaskExecutor syncExecutor = new ThreadPoolTaskExecutor();
    int poolIndex;
    boolean shouldWriteExceptionLog = true;
    for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
        perunUser[poolIndex] = context.getBean("perunUser", PerunUser.class);
    }
    try {
        log.debug("Getting list of users");
        List<User> users = perun.getUsersManagerBl().getUsers(ldapcManager.getPerunSession());
        Set<Name> presentUsers = new HashSet<Name>(users.size());
        syncExecutor.setCorePoolSize(5);
        syncExecutor.setMaxPoolSize(8);
        // syncExecutor.setQueueCapacity(30);
        syncExecutor.initialize();
        poolIndex = 0;
        taskCount = new AtomicInteger(0);
        for (User user : users) {
            presentUsers.add(perunUser[0].getEntryDN(String.valueOf(user.getId())));
            log.debug("Getting list of attributes for user {}", user.getId());
            List<Attribute> attrs = new ArrayList<Attribute>();
            List<String> attrNames = fillPerunAttributeNames(perunUser[poolIndex].getPerunAttributeNames());
            try {
                // log.debug("Getting attribute {} for user {}", attrName, user.getId());
                attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), user, attrNames));
            /* very chatty
						if(attr == null) {
							log.debug("Got null for attribute {}", attrName);
						} else if (attr.getValue() == null) {
							log.debug("Got attribute {} with null value", attrName);
						} else {
							log.debug("Got attribute {} with value {}", attrName, attr.getValue().toString());
						}
						*/
            } catch (PerunRuntimeException e) {
                log.warn("Couldn't get attributes {} for user {}: {}", attrNames, user.getId(), e.getMessage());
                shouldWriteExceptionLog = false;
                throw new InternalErrorException(e);
            }
            log.debug("Got attributes {}", attrNames.toString());
            try {
                // log.debug("Synchronizing user {} with {} attrs", user, attrs.size());
                // perunUser.synchronizeEntry(user, attrs);
                log.debug("Getting list of member groups for user {}", user.getId());
                Set<Integer> voIds = new HashSet<>();
                List<Member> members = perun.getMembersManagerBl().getMembersByUser(ldapcManager.getPerunSession(), user);
                List<Group> groups = new ArrayList<Group>();
                for (Member member : members) {
                    if (member.getStatus().equals(Status.VALID)) {
                        voIds.add(member.getVoId());
                        groups.addAll(perun.getGroupsManagerBl().getAllGroupsWhereMemberIsActive(ldapcManager.getPerunSession(), member));
                    }
                }
                // log.debug("Synchronizing user {} with {} VOs and {} groups", user.getId(), voIds.size(), groups.size());
                // perunUser.synchronizeMembership(user, voIds, groups);
                log.debug("Getting list of extSources for user {}", user.getId());
                List<UserExtSource> userExtSources = perun.getUsersManagerBl().getUserExtSources(ldapcManager.getPerunSession(), user);
                List<Group> admin_groups = perun.getUsersManagerBl().getGroupsWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                List<Vo> admin_vos = perun.getUsersManagerBl().getVosWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                List<Facility> admin_facilities = perun.getFacilitiesManagerBl().getFacilitiesWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                // log.debug("Synchronizing user {} with {} extSources", user.getId(), userExtSources.size());
                // perunUser.synchronizePrincipals(user, userExtSources);
                syncExecutor.execute(new SyncUsersWorker(poolIndex, user, attrs, voIds, groups, userExtSources, admin_groups, admin_vos, admin_facilities));
                taskCount.incrementAndGet();
            } catch (PerunRuntimeException e) {
                log.error("Error synchronizing user", e);
                shouldWriteExceptionLog = false;
                throw new InternalErrorException(e);
            }
            poolIndex = (poolIndex + 1) % perunUser.length;
        }
        try {
            removeOldEntries(perunUser[0], presentUsers, log);
        } catch (InternalErrorException e) {
            log.error("Error removing old user entries", e);
            shouldWriteExceptionLog = false;
            throw new InternalErrorException(e);
        }
    } catch (PerunRuntimeException e) {
        if (shouldWriteExceptionLog) {
            log.error("Error synchronizing users", e);
        }
        throw new InternalErrorException(e);
    } finally {
        // wait for all the tasks to get executed
        while (!syncExecutor.getThreadPoolExecutor().getQueue().isEmpty()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        // wait for all the tasks to complete (for at most 10 seconds)
        for (int i = 0; i < 10 && taskCount.get() > 0; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        syncExecutor.shutdown();
        for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
            perunUser[poolIndex] = null;
        }
    }
    if (wasThreadException) {
        throw new InternalErrorException("Error synchronizing user in executed thread");
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunUser(cz.metacentrum.perun.ldapc.model.PerunUser) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Name(javax.naming.Name) PerunRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException) Vo(cz.metacentrum.perun.core.api.Vo) PerunUser(cz.metacentrum.perun.ldapc.model.PerunUser) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Facility(cz.metacentrum.perun.core.api.Facility)

Aggregations

Attribute (cz.metacentrum.perun.core.api.Attribute)1 Facility (cz.metacentrum.perun.core.api.Facility)1 Group (cz.metacentrum.perun.core.api.Group)1 Member (cz.metacentrum.perun.core.api.Member)1 User (cz.metacentrum.perun.core.api.User)1 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)1 Vo (cz.metacentrum.perun.core.api.Vo)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 PerunRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException)1 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)1 PerunUser (cz.metacentrum.perun.ldapc.model.PerunUser)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Name (javax.naming.Name)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1