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");
}
}
Aggregations