7 Commits

  1. 62
      src/main/java/de/hsfulda/informatik/AccountSource.java
  2. 9
      src/main/java/de/hsfulda/informatik/LdapSync.java

62
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.ldap.sdk.extensions.StartTLSExtendedRequest;
import com.unboundid.util.ssl.SSLUtil; import com.unboundid.util.ssl.SSLUtil;
import com.unboundid.util.ssl.TrustAllTrustManager; import com.unboundid.util.ssl.TrustAllTrustManager;
import sun.nio.ch.ThreadPool;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class AccountSource { public class AccountSource {
@ -24,6 +27,7 @@ public class AccountSource {
final List<SearchResultEntry> searchResultEntryList; final List<SearchResultEntry> searchResultEntryList;
final Set<String> users; final Set<String> users;
final String baseDN; final String baseDN;
final ExecutorService executorService = Executors.newFixedThreadPool(24);
AccountSource(final String baseDN, final List<SearchResultEntry> searchResultEntryList) { AccountSource(final String baseDN, final List<SearchResultEntry> searchResultEntryList) {
this.baseDN = baseDN; this.baseDN = baseDN;
@ -64,38 +68,48 @@ public class AccountSource {
public void add(final List<Entry> usersToBeAdded) { public void add(final List<Entry> usersToBeAdded) {
for (final Entry 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("-> Benutzer cn=" + entry.getDN() + " hinzugefügt...Ok\n");
} else {
System.out.print("Fehler beim Hinzufügen von Benutzer cn=" + entry.getDN() + " (" + ldapResult.getResultCode() + ")\n");
}
} catch (LDAPException e) {
System.out.print("Fehler beim Hinzufügen von Benutzer cn=" + entry.getDN() + " (" + e.getMessage() + ")\n");
}
} }
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
});
} }
} }
public void del(final Set<String> usersToBeDeleted) { public void del(final Set<String> usersToBeDeleted) {
for (final String user : 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("-> Benutzer cn=" + user + "," + AccountSource.this.baseDN + " gelöscht...Ok\n");
} else {
System.out.print("Fehler beim Löschen von Benutzer cn=" + user + " (" + ldapResult.getResultCode() + ")\n");
}
} catch (LDAPException e) {
System.out.print("Fehler beim Löschen von Benutzer cn=" + user + " (" + e.getMessage() + ")\n");
}
} }
} catch (LDAPException e) {
System.out.print("Fehler (" + e.getMessage() + ")\n");
}
});
} }
} }
public void shutdown() {
executorService.shutdown();
}
} }

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

@ -49,7 +49,7 @@ public class LdapSync {
new String[]{"cn", "givenname", "sn", "uid"} new String[]{"cn", "givenname", "sn", "uid"}
); );
System.out.print("Ok\nAbfrage der Benutzer im OpenLDAP...");
System.out.print(remote.getUsers().size() + " entrie(s) received\nAbfrage der Benutzer im OpenLDAP...");
// lade Daten des lokalen Systems // lade Daten des lokalen Systems
final AccountSource local = new AccountSource( final AccountSource local = new AccountSource(
@ -62,7 +62,7 @@ public class LdapSync {
new String[]{} new String[]{}
); );
System.out.print("Ok\n");
System.out.print(local.getUsers().size() + " entrie(s) received\n");
sync(remote, local); sync(remote, local);
} }
@ -141,6 +141,7 @@ public class LdapSync {
local.del(usersToBeDeleted); local.del(usersToBeDeleted);
local.add(entriesToBeAdded); local.add(entriesToBeAdded);
local.shutdown();
} }
static Integer computeUid(final String cn) { static Integer computeUid(final String cn) {
@ -178,6 +179,10 @@ public class LdapSync {
} }
} }
if (m.group(2).isEmpty()) {
return null;
}
final int s = m.group(2).length(); final int s = m.group(2).length();
final int z = Integer.parseInt(m.group(2)); final int z = Integer.parseInt(m.group(2));

Loading…
Cancel
Save