rmProps.put(RecordManagerOptions.DISABLE_TRANSACTIONS, "true");
String dbname = new File(System.getProperty("java.io.tmpdir"), "lastlogindb").getCanonicalPath();
if (VERBOSE)
System.out.println("dbname: " + dbname);
RecordManager stamps = RecordManagerFactory.createRecordManager(dbname, rmProps);
BTree stampDb = BTree.createInstance(stamps, new StringComparator());
// Scan log files looking for login records
final Pattern loginCracker = Pattern.compile(loginRE);
final SimpleDateFormat dateEncoder = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (String logName : args)
{
BufferedReader logReader = new BufferedReader(new FileReader(logName));
while(true)
{
String line = logReader.readLine();
// End of file?
if (null == line)
break;
// Skip if definitely not a login record
if (!line.contains(":login:"))
continue;
// Try to recognize the interesting fields
Matcher loginMatcher = loginCracker.matcher(line);
if (!loginMatcher.matches())
continue;
// Pretty sure we have a login
String date = loginMatcher.group(1);
String time = loginMatcher.group(2);
String user = loginMatcher.group(3);
String logDateTime = date + ' ' + time;
Date stamp;
try {
stamp = dateEncoder.parse(logDateTime);
} catch (ParseException ex) {
System.err.println("Skipping log record: " + ex.getMessage());
continue;
}
Date previous = (Date) stampDb.find(user);
if (null == previous || stamp.after(previous))
{
stampDb.insert(user, stamp, true); // Record this user's newest login so far
}
}
logReader.close();
}
// Now walk the cache and update EPersons
TupleBrowser walker = stampDb.browse();
Tuple stamp = new Tuple();
Context ctx = new Context();
ctx.turnOffAuthorisationSystem();
while(walker.getNext(stamp))
{
// Update an EPerson's last login
String name = (String) stamp.getKey();
Date date = (Date) stamp.getValue();
EPerson ePerson;
ePerson = EPerson.findByEmail(ctx, name);
if (null == ePerson)
ePerson = EPerson.findByNetid(ctx, name);
if (null == ePerson)
{
System.err.println("Skipping unknown user: " + name);
continue;
}
Date previous = ePerson.getLastActive();
if ((null == previous) || date.after(previous))
{
if (PRETEND)
{
System.out.printf("%d\t%s\t%s\t%s\t%s\n",
ePerson.getID(),
date,
ePerson.getEmail(),
ePerson.getNetid(),
ePerson.getFullName());
}
else
{
ePerson.setLastActive(date);
ePerson.update();
ctx.commit();
}
}
}
ctx.complete();
stamps.close();
// Clean up external data and index files, if any
File target;
target = new File(dbname + ".db");