From 4e75fdc85ff2a9e04d1820a3a2f8dc62e431295b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 15 Jan 2013 13:36:03 +0100 Subject: o Renaming PersonGenerator to LdapImporter. Taking arguments from args instead of system properties. --- pom.xml | 4 + .../io/trygvis/esper/testing/LdapImporter.java | 120 ++++++++++++++++++++ .../io/trygvis/esper/testing/PersonGenerator.java | 121 --------------------- 3 files changed, 124 insertions(+), 121 deletions(-) create mode 100755 src/main/java/io/trygvis/esper/testing/LdapImporter.java delete mode 100755 src/main/java/io/trygvis/esper/testing/PersonGenerator.java diff --git a/pom.xml b/pom.xml index 5fe5c03..eba5b45 100755 --- a/pom.xml +++ b/pom.xml @@ -203,6 +203,10 @@ + + ldap-importer + io.trygvis.esper.testing.LdapImporter + nexus-importer io.trygvis.esper.testing.nexus.NexusImporter diff --git a/src/main/java/io/trygvis/esper/testing/LdapImporter.java b/src/main/java/io/trygvis/esper/testing/LdapImporter.java new file mode 100755 index 0000000..b29b325 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/LdapImporter.java @@ -0,0 +1,120 @@ +package io.trygvis.esper.testing; + +import com.jolbox.bonecp.*; +import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.util.sql.*; +import org.slf4j.*; + +import java.sql.*; +import java.util.*; +import javax.naming.*; +import javax.naming.directory.*; + +public class LdapImporter { + public static void main(String[] args) throws Exception { + Config config = Config.loadFromDisk("person-generator"); + + final Logger logger = LoggerFactory.getLogger(config.appName); + + if(args.length != 3) { + System.err.println("Usage: ldap-importer [ldap host] [ldap port] [base dn]"); + System.exit(1); + } + + String ldapHost = args[0]; + int ldapPort = Integer.parseInt(args[1]); + String baseDn = args[2]; + + Properties properties = new Properties(); + properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); + properties.put(Context.PROVIDER_URL, "ldap://" + ldapHost + ":" + ldapPort); + properties.put(Context.REFERRAL, "ignore"); + properties.put(Context.SECURITY_AUTHENTICATION, "none"); + + InitialDirContext context = new InitialDirContext(properties); + SearchControls searchCtls = new SearchControls(); + searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); + searchCtls.setReturningAttributes(new String[]{"uid", "mail", "displayName"}); + NamingEnumeration answer = context.search(baseDn, "(&(uid=*)(mail=*)(displayName=*))", searchCtls); + + BoneCPDataSource dataSource = config.createBoneCp(); + final Daos daos = new Daos(dataSource.getConnection()); + PersonDao personDao = daos.personDao; + List servers = daos.jenkinsDao.selectServers(false); + + while (answer.hasMore()) { + SearchResult next = (SearchResult) answer.next(); + String uid = next.getAttributes().get("uid").get().toString(); + String mail = next.getAttributes().get("mail").get().toString(); + String displayName = next.getAttributes().get("displayName").get().toString(); + + logger.info("LDAP: uid = {}, mail = {}, displayName = {}", uid, mail, displayName); + + SqlOption personO = personDao.selectPersonByMail(mail); + + Uuid personUuid; + if(personO.isNone()) { + personUuid = personDao.insertPerson(mail, displayName); + logger.info("New person: uuid={}", personUuid); + } + else { + personUuid = personO.get().uuid; + logger.info("Existing person: uuid={}", personUuid); + } + + claimUid(logger, daos, servers, uid, personUuid); + + // Add the username from their email addresses as aliases too + int i = mail.indexOf('@'); + + if (i == -1) { + continue; + } + + String alternate = mail.substring(0, i); + + if(alternate.equals(uid)) { + continue; + } + + claimUid(logger, daos, servers, alternate, personUuid); + } + +// System.out.println("ROLLBACK"); +// daos.rollback(); + logger.info("COMMIT"); + daos.commit(); + logger.info("Closing SQL connection"); + daos.close(); + + logger.info("Closing pool"); + dataSource.close(); + System.exit(0); + } + + private static void claimUid(final Logger logger, final Daos daos, List servers, String uid, Uuid personUuid) throws SQLException { + for (final JenkinsServerDto server : servers) { + final String url = server.userUrl(uid).toASCIIString(); + + UUID jenkinsUserUuid = daos.jenkinsDao.selectUserByAbsoluteUrl(server.uuid, url).map(new SqlF() { + public UUID apply(JenkinsUserDto jenkinsUserDto) throws SQLException { + return jenkinsUserDto.uuid; + } + }).getOrElse(new SqlP0() { + public UUID apply() throws SQLException { + logger.info("New Jenkins user for server {}", server.url); + return daos.jenkinsDao.insertUser(server.uuid, url); + } + }); + + if(!daos.personDao.hasPersonJenkinsUser(personUuid, jenkinsUserUuid)) { + logger.info("Person claims jenkins user: " + url); + daos.personDao.insertPersonJenkinsUser(personUuid, jenkinsUserUuid); + } + else { + logger.info("Person already had a claim for: " + url); + } + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/PersonGenerator.java b/src/main/java/io/trygvis/esper/testing/PersonGenerator.java deleted file mode 100755 index 28d8438..0000000 --- a/src/main/java/io/trygvis/esper/testing/PersonGenerator.java +++ /dev/null @@ -1,121 +0,0 @@ -package io.trygvis.esper.testing; - -import com.jolbox.bonecp.*; -import io.trygvis.esper.testing.core.db.*; -import io.trygvis.esper.testing.jenkins.*; -import io.trygvis.esper.testing.util.sql.*; -import org.slf4j.*; - -import java.sql.*; -import java.util.*; -import javax.naming.*; -import javax.naming.directory.*; - -public class PersonGenerator { - public static void main(String[] args) throws Exception { - Config config = Config.loadFromDisk("person-generator"); - - final Logger logger = LoggerFactory.getLogger(config.appName); - - String ldapHost = System.getProperty("ldap.host", "localhost"); - int ldapPort = Integer.parseInt(System.getProperty("ldap.port", "386")); - String baseDn = System.getProperty("ldap.base"); - - if(baseDn == null) { - System.err.println("Missing system property: ldap.base"); - return; - } - - Properties properties = new Properties(); - properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); - properties.put(Context.PROVIDER_URL, "ldap://" + ldapHost + ":" + ldapPort); - properties.put(Context.REFERRAL, "ignore"); - properties.put(Context.SECURITY_AUTHENTICATION, "none"); - - InitialDirContext context = new InitialDirContext(properties); - SearchControls searchCtls = new SearchControls(); - searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); - String returnedAtts[] = {"uid", "mail", "displayName"}; - searchCtls.setReturningAttributes(returnedAtts); - NamingEnumeration answer = context.search(baseDn, "(&(uid=*)(mail=*)(displayName=*))", searchCtls); - - BoneCPDataSource dataSource = config.createBoneCp(); - final Daos daos = new Daos(dataSource.getConnection()); - PersonDao personDao = daos.personDao; - List servers = daos.jenkinsDao.selectServers(false); - - while (answer.hasMore()) { - SearchResult next = (SearchResult) answer.next(); - String uid = next.getAttributes().get("uid").get().toString(); - String mail = next.getAttributes().get("mail").get().toString(); - String displayName = next.getAttributes().get("displayName").get().toString(); - - logger.info("LDAP: uid = {}, mail = {}, displayName = {}", uid, mail, displayName); - - SqlOption personO = personDao.selectPersonByMail(mail); - - Uuid personUuid; - if(personO.isNone()) { - personUuid = personDao.insertPerson(mail, displayName); - logger.info("New person: uuid={}", personUuid); - } - else { - personUuid = personO.get().uuid; - logger.info("Existing person: uuid={}", personUuid); - } - - claimUid(logger, daos, servers, uid, personUuid); - - // Add the username from their email addresses as aliases too - int i = mail.indexOf('@'); - - if (i == -1) { - continue; - } - - String alternate = mail.substring(0, i); - - if(alternate.equals(uid)) { - continue; - } - - claimUid(logger, daos, servers, alternate, personUuid); - } - -// System.out.println("ROLLBACK"); -// daos.rollback(); - logger.info("COMMIT"); - daos.commit(); - logger.info("Closing SQL connection"); - daos.close(); - - logger.info("Closing pool"); - dataSource.close(); - System.exit(0); - } - - private static void claimUid(final Logger logger, final Daos daos, List servers, String uid, Uuid personUuid) throws SQLException { - for (final JenkinsServerDto server : servers) { - final String url = server.userUrl(uid).toASCIIString(); - - UUID jenkinsUserUuid = daos.jenkinsDao.selectUserByAbsoluteUrl(server.uuid, url).map(new SqlF() { - public UUID apply(JenkinsUserDto jenkinsUserDto) throws SQLException { - return jenkinsUserDto.uuid; - } - }).getOrElse(new SqlP0() { - public UUID apply() throws SQLException { - logger.info("New Jenkins user for server {}", server.url); - return daos.jenkinsDao.insertUser(server.uuid, url); - } - }); - - if(!daos.personDao.hasPersonJenkinsUser(personUuid, jenkinsUserUuid)) { - logger.info("Person claims jenkins user: " + url); - daos.personDao.insertPersonJenkinsUser(personUuid, jenkinsUserUuid); - } - else { - logger.info("Person already had a claim for: " + url); - } - } - } -} -- cgit v1.2.3