Skip to content
Snippets Groups Projects
Commit 58d5e666 authored by Barbara B. Frosik's avatar Barbara B. Frosik
Browse files

adding AccountSynchronizer to tools dir

parent 0c0482e6
No related branches found
No related tags found
No related merge requests found
Showing
with 637 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/home/phoebus/BFROSIK/trunk/tools/AccountSynchronizer/lib/ojdbc7.jar"/>
<classpathentry kind="lib" path="/home/phoebus/BFROSIK/trunk/tools/AccountSynchronizer/lib/postgresql-9.4-1201.jdbc4.jar"/>
<classpathentry kind="lib" path="/home/phoebus/BFROSIK/trunk/tools/AccountSynchronizer/lib/unboundid-ldapsdk-me.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>accountSynchronizer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
### log congiguration
log.file = accountSynchronizer%g.log
log.limit = 6000
log.count = 2
###
### Oracle database settings
###
oracle.database.connection = jdbc:oracle:thin:@ra.aps.anl.gov:1527:aps1
oracle.database.username = glob_conn
### oracle.database.password =
oracle.database.table = DCC.FL$03_BL_APV_VIEW_V2
###
### Postgresql database settings
###
dm.database.connection = jdbc:postgresql://127.0.0.1:11136/dm
### dm.database.username =
### dm.database.password =
### Connection settings for LDAP
###
#
#ldap.server.address = blacklab.xray.aps.anl.gov
#ldap.server.port = 389
#ldap.server.username = uid=dmadmin,ou=People,o=aps.anl.gov,dc=aps,dc=anl,dc=gov
#ldap.server.password =
###
### LDAP user account settings
###
#ldap.accounts.baseDN = ou=People, o=aps.anl.gov, dc=aps,dc=anl,dc=gov
### General account settings
###
# The user name prefix that gets append to every badge number.
user.userid.prefix = d
File added
File added
File added
File added
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
firstName = FIRST_NAME
lastName = LAST_NAME
middleName = MIDDLE_NAME
email = EMAIL
badge = BADGE_NO
password = PWD_HASH_VALUE
notEmployee = IS_USRNOT_ANL_EMP
inactive = INACTIVE
recordUpdate = LAST_CHANGE_DATE
Main-Class: gov.anl.aps.dm.sync.Synchronizer
Class-Path: ../lib/ojdbc7.jar ../lib/postgresql-9.4-1201.jdbc4.jar ../lib/unboundid-ldapsdk-me.jar resources
### log congiguration
log.file = accountSynchronizer%g.log
log.limit = 6000
log.count = 2
###
### Oracle database settings
###
oracle.database.connection = jdbc:oracle:thin:@ra.aps.anl.gov:1527:aps1
oracle.database.username = glob_conn
### oracle.database.password =
oracle.database.table = DCC.FL$03_BL_APV_VIEW_V2
###
### Postgresql database settings
###
dm.database.connection = jdbc:postgresql://127.0.0.1:11136/dm
### dm.database.username =
### dm.database.password =
### Connection settings for LDAP
###
#
#ldap.server.address = blacklab.xray.aps.anl.gov
#ldap.server.port = 389
#ldap.server.username = uid=dmadmin,ou=People,o=aps.anl.gov,dc=aps,dc=anl,dc=gov
#ldap.server.password =
###
### LDAP user account settings
###
#ldap.accounts.baseDN = ou=People, o=aps.anl.gov, dc=aps,dc=anl,dc=gov
### General account settings
###
# The user name prefix that gets append to every badge number.
user.userid.prefix = d
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
firstName = FIRST_NAME
lastName = LAST_NAME
middleName = MIDDLE_NAME
email = EMAIL
badge = BADGE_NO
password = PWD_HASH_VALUE
notEmployee = IS_USRNOT_ANL_EMP
inactive = INACTIVE
recordUpdate = LAST_CHANGE_DATE
package gov.anl.aps.dm.sync;
public class DmUser {
String username;
String firstName;
String lastName;
String middleName;
String email;
String badge;
String password;
void clear() {
username = null;
firstName = null;
lastName = null;
middleName = null;
email = null;
badge = null;
password = null;
}
}
package gov.anl.aps.dm.sync;
import com.unboundid.ldap.sdk.BindRequest;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SimpleBindRequest;
import com.unboundid.util.ssl.SSLUtil;
import com.unboundid.util.ssl.TrustAllTrustManager;
import java.security.GeneralSecurityException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.SocketFactory;
public class LdapConn {
LDAPConnection connection = null;
private Logger logger;
public LdapConn(Logger logger) {
this.logger = logger;
}
void connect() {
// Use no key manager, and trust all certificates. This would not be used
// in non-trivial code.
SSLUtil sslUtil = new SSLUtil(null,new TrustAllTrustManager());
SocketFactory socketFactory;
LDAPConnection ldapConnection = null;
try {
// Create the socket factory that will be used to make a secure
// connection to the server.
socketFactory = sslUtil.createSSLSocketFactory();
try {
ldapConnection = new LDAPConnection(socketFactory,"blacklab.xray.aps.anl.gov",636);
System.out.println("seems connected "+ ldapConnection.toString());
} catch (LDAPException ex) {
logger.log(Level.SEVERE, null, ex);
}
} catch(GeneralSecurityException exception) {
System.err.println(exception);
System.exit(1);
}
try {
String dn = "uid=dmadmin,ou=People,o=aps.anl.gov,dc=aps,dc=anl,dc=gov";
String password = "pass";
long maxResponseTimeMillis = 1000;
BindRequest bindRequest = new SimpleBindRequest(dn,password);
bindRequest.setResponseTimeoutMillis(maxResponseTimeMillis);
BindResult bindResult = ldapConnection.bind(bindRequest);
System.out.println("connected to ldap " +bindResult.toString());
} catch(LDAPException ldapException) {
ldapConnection.close();
System.err.println(ldapException);
System.exit(ldapException.getResultCode().intValue());
} }
void close() {
connection.close();
}
}
package gov.anl.aps.dm.sync;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OracleConnection {
Connection connection = null;
private Logger logger;
public OracleConnection(Logger logger) {
this.logger = logger;
}
void connect(Properties config) throws SQLException {
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
} catch (SQLException e) {
logger.log(Level.SEVERE, "Can't establish Oracle Driver", e);
throw e;
}
logger.log(Level.INFO, "Oracle JDBC Driver Registered!");
try {
connection = DriverManager.getConnection(
config.getProperty("oracle.database.connection"),
config.getProperty("oracle.database.username"),
config.getProperty("oracle.database.password"));
} catch (SQLException e) {
logger.log(Level.SEVERE, "Can't connect to Oracle data base", e);
throw e;
}
}
ResultSet getUsers(String table) throws SQLException {
try {
Statement statement = connection.createStatement();
return statement.executeQuery("select * from " + table);
} catch (SQLException ex) {
logger.log(Level.SEVERE, "table {0} does not exist", table);
throw ex;
}
}
void close() {
try {
connection.close();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "problem closing Oracle connection ", ex);
}
}
}
package gov.anl.aps.dm.sync;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PsqlConnection {
Connection connection = null;
private final Map<String, String> dmUsers = new HashMap<>(); // <username, username>
private PreparedStatement insertQuery;
private PreparedStatement updateQuery;
private final String userTable = "user_info";
private Date lastUpdate;
private Logger logger;
public PsqlConnection(Logger logger) {
this.logger = logger;
}
void connect(Properties config) throws SQLException {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, "Can't establish PosgreSQL Driver", e);
System.exit(0);
}
logger.log(Level.INFO, "PostgreSQL JDBC Driver Registered!");
try {
connection = DriverManager.getConnection(
config.getProperty("dm.database.connection"),
config.getProperty("dm.database.username"),
config.getProperty("dm.database.password"));
} catch (SQLException e) {
logger.log(Level.SEVERE, "Can't connect to PostgreSQL data base");
throw e;
}
}
void init() throws SQLException {
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "can't create statement for postgreSQL connection");
throw ex;
}
ResultSet results = null;
try {
if (statement != null) {
results = statement.executeQuery("SELECT * FROM " + userTable + ";");
}
} catch (SQLException ex) {
logger.log(Level.SEVERE, "can't execute SELECT query from " + userTable + " table");
throw ex;
}
try {
if (results != null) {
while (results.next()) {
String username = results.getString("username");
dmUsers.put(username, username);
}
results.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException ex) {
logger.log(Level.WARNING, "query results processing error ", ex);
}
// prepare statements
try {
insertQuery = connection.prepareStatement ("INSERT INTO user_info (username, first_name, last_name, middle_name, email, badge) VALUES (?, ?, ?, ?, ?, ?)");
updateQuery = connection.prepareStatement ("UPDATE user_info SET email = ?, last_name = ? WHERE username = ?");
} catch (SQLException ex) {
logger.log(Level.SEVERE, "Can't get peaparedStatement from connection ", ex);
throw ex;
}
// get the last update
String lastUpdateTable = "users_last_update";
try {
statement = connection.createStatement();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "can't create statement for postgreSQL connection");
throw ex;
}
results = null;
try {
if (statement != null) {
results = statement.executeQuery("SELECT * FROM " + lastUpdateTable + " WHERE id = 1;");
}
} catch (SQLException ex) {
logger.log(Level.WARNING, "can't execute query from {0} table", lastUpdateTable);
addLastUpdate();
}
try {
if (results != null) {
results.next();
lastUpdate = results.getDate("last_update");
results.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException ex) {
logger.log(Level.WARNING, "The table {0} is empty", lastUpdateTable);
addLastUpdate();
}
}
boolean isUser(String username) {
return dmUsers.containsKey(username);
}
Date getLastUpdate() {
return lastUpdate;
}
void addLastUpdate() {
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(1990, Calendar.JANUARY, 1);
try {
lastUpdate = new Date(cal.getTimeInMillis());
try (
PreparedStatement setUpdate = connection.prepareStatement("INSERT INTO users_last_update (id, last_update) VALUES (1, ?)")) {
setUpdate.setDate(1, lastUpdate);
setUpdate.executeUpdate();
}
} catch (SQLException ex) {
logger.log(Level.WARNING, "can't update last update table ", ex);
}
}
void setLastUpdate() {
java.util.Calendar cal = java.util.Calendar.getInstance();
try {
lastUpdate = new Date(cal.getTimeInMillis());
try (
PreparedStatement setUpdate = connection.prepareStatement("UPDATE users_last_update SET last_update = ?")) {
setUpdate.setDate(1, lastUpdate);
setUpdate.execute();
}
} catch (SQLException ex) {
logger.log(Level.WARNING, "can't update last update table ", ex);
}
}
void addUser(DmUser dmuser) {
try {
insertQuery.setString(1, dmuser.username);
insertQuery.setString(2, dmuser.firstName);
insertQuery.setString(3, dmuser.lastName);
insertQuery.setString(4, dmuser.middleName);
insertQuery.setString(5, dmuser.email);
insertQuery.setString(6, dmuser.badge);
insertQuery.execute();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "can't execute query to add user ", ex);
}
}
void updateUser(DmUser dmuser) {
try {
updateQuery.setString(1, dmuser.email);
updateQuery.setString(2, dmuser.lastName);
updateQuery.setString(3, dmuser.username);
updateQuery.executeUpdate();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "can't execute query to update user", ex);
}
}
void close() {
try {
insertQuery.close();
updateQuery.close();
connection.close();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "problem closing PostgreSQL connection ", ex);
}
}
}
package gov.anl.aps.dm.sync;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Synchronizer {
// private final LdapConn lConnection = new LdapConn();
private OracleConnection oConnection;
private PsqlConnection pConnection;
private final Properties config = new Properties();
private final String configFile = "config.properties";
private final Properties tableRows = new Properties();
private final String oracleTable = "oracleTable.properties";
private FileHandler fileHandler;
private static final Logger logger = Logger.getLogger("AccountSynchronizerLogger");
public static void main(String[] argv) {
Synchronizer sync = new Synchronizer();
sync.loadProperties(sync.configFile);
sync.initLogger();
sync.oConnection = new OracleConnection(logger);
sync.pConnection = new PsqlConnection(logger);
sync.connectAndInit();
sync.synchronize();
}
private void loadProperties(String configFile) {
InputStream configInputStream = getClass().getClassLoader().getResourceAsStream(configFile);
if (configInputStream != null) {
try {
this.config.load(configInputStream);
} catch (IOException ex) {
initDefaultLogger();
logger.log(Level.SEVERE, "can't load configuration ", ex);
System.exit(-1);
}
} else {
initDefaultLogger();
logger.log(Level.SEVERE, "can't find configuration file");
System.exit(-1);
}
InputStream tableInputStream = getClass().getClassLoader().getResourceAsStream(oracleTable);
if (tableInputStream != null) {
try {
this.tableRows.load(tableInputStream);
} catch (IOException ex) {
logger.log(Level.SEVERE, "can't load Oracle table rows definitions ", ex);
System.exit(-1);
}
}
}
private void initDefaultLogger() {
try {
fileHandler = new FileHandler("accountSynchronizer.log");
logger.addHandler(fileHandler);
logger.log(Level.WARNING, "Using default logger ");
} catch (SecurityException | IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
private void initLogger() {
String pattern = config.getProperty("log.file");
int limit = 0;
int count = 0;
try {
String limitProperty = config.getProperty("log.limit");
limit = Integer.decode(limitProperty);
count = Integer.decode(config.getProperty("log.count"));
} catch (NumberFormatException e) {
initDefaultLogger();
logger.log(Level.WARNING, "The logger limit or count are not configured properly. ", e);
System.exit(-1);
}
try {
fileHandler = new FileHandler(pattern, limit, count);
logger.addHandler(fileHandler);
} catch (SecurityException | IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
private void connectAndInit() {
// lConnection.connect();
try {
pConnection.connect(config);
}
catch (SQLException e) {
System.exit(-1);
}
try {
oConnection.connect(config);
}
catch (SQLException e) {
pConnection.close();
System.exit(-1);
}
try {
pConnection.init();
}
catch (SQLException e) {
pConnection.close();
oConnection.close();
System.exit(-1);
}
}
private void synchronize() {
Date lastUpdate = pConnection.getLastUpdate();
DmUser dmuser = new DmUser();
try {
ResultSet results = oConnection.getUsers(config.getProperty("oracle.database.table"));
while (results.next()) {
String badge = results.getString("BADGE_NO");
String username = config.getProperty("user.userid.prefix") + badge;
if (pConnection.isUser(username)) {
if ((results.getDate("LAST_CHANGE_DATE") == null) || (results.getDate("LAST_CHANGE_DATE").compareTo(lastUpdate) >= 0)) {
// update user
dmuser.email = results.getString("EMAIL");
dmuser.username = username;
dmuser.lastName = results.getString("LAST_NAME");
pConnection.updateUser(dmuser);
dmuser.clear();
}
} else {
// add user
dmuser.badge = badge;
dmuser.email = results.getString("EMAIL");
dmuser.firstName = results.getString("FIRST_NAME");
dmuser.lastName = results.getString("LAST_NAME");
dmuser.middleName = results.getString("MIDDLE_NAME");
dmuser.username = username;
pConnection.addUser(dmuser);
dmuser.clear();
}
}
pConnection.setLastUpdate();
} catch (SQLException ex) {
logger.log(Level.SEVERE, "table processing error ", ex);
}
finally {
pConnection.close();
oConnection.close();
}
}
}
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment