Browse Source

Parallel add and delete

main
Christian Pape 2 days ago
committed by Dustin Frisch
parent
commit
a87e8c6384
No known key found for this signature in database GPG Key ID: B4C3BF012D9B26BE
  1. 56
      src/main/java/de/hsfulda/informatik/AccountSource.java
  2. 4
      src/main/java/de/hsfulda/informatik/LdapSync.java

56
src/main/java/de/hsfulda/informatik/AccountSource.java

@ -12,11 +12,14 @@ import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest;
import com.unboundid.util.ssl.SSLUtil;
import com.unboundid.util.ssl.TrustAllTrustManager;
import sun.nio.ch.ThreadPool;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class AccountSource {
@ -24,6 +27,7 @@ public class AccountSource {
final List<SearchResultEntry> searchResultEntryList;
final Set<String> users;
final String baseDN;
final ExecutorService executorService = Executors.newFixedThreadPool(24);
AccountSource(final String baseDN, final List<SearchResultEntry> searchResultEntryList) {
this.baseDN = baseDN;
@ -65,37 +69,45 @@ public class AccountSource {
public void add(final List<Entry> usersToBeAdded) {
for (final Entry entry : usersToBeAdded) {
System.out.print("-> Füge Benutzer " + entry.getDN() + "...");
final LDAPResult ldapResult;
try {
ldapResult = connection.add(entry);
if (ldapResult.getResultCode().intValue() == 0) {
System.out.print("Ok\n");
} else {
System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n");
executorService.submit(new Runnable() {
@Override
public void run() {
final LDAPResult ldapResult;
try {
ldapResult = connection.add(entry);
if (ldapResult.getResultCode().intValue() == 0) {
System.out.print("Ok\n");
} else {
System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n");
}
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
}
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
});
}
}
public void del(final Set<String> usersToBeDeleted) {
for (final String user : usersToBeDeleted) {
System.out.print("-> Lösche Benutzer cn=" + user + "," + this.baseDN + "...");
final LDAPResult ldapResult;
try {
ldapResult = connection.delete("cn=" + user + "," + this.baseDN);
executorService.submit(new Runnable() {
@Override
public void run() {
final LDAPResult ldapResult;
try {
ldapResult = connection.delete("cn=" + user + "," + AccountSource.this.baseDN);
if (ldapResult.getResultCode().intValue() == 0) {
System.out.print("Ok\n");
} else {
System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n");
if (ldapResult.getResultCode().intValue() == 0) {
System.out.print("Ok\n");
} else {
System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n");
}
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
}
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
});
}
}
}

4
src/main/java/de/hsfulda/informatik/LdapSync.java

@ -49,7 +49,7 @@ public class LdapSync {
new String[]{"cn", "givenname", "sn", "uid"}
);
System.out.print(remote.getUsers().size() + "entrie(s) received\nAbfrage der Benutzer im OpenLDAP...");
System.out.print(remote.getUsers().size() + " entrie(s) received\nAbfrage der Benutzer im OpenLDAP...");
// lade Daten des lokalen Systems
final AccountSource local = new AccountSource(
@ -62,7 +62,7 @@ public class LdapSync {
new String[]{}
);
System.out.print(remote.getUsers().size() + "entrie(s) received\n");
System.out.print(remote.getUsers().size() + " entrie(s) received\n");
sync(remote, local);
}

Loading…
Cancel
Save