/*
* Sun Public License
*
* The contents of this file are subject to the Sun Public License Version
* 1.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is available at http://www.sun.com/
*
* The Original Code is the SLAMD Distributed Load Generation Engine.
* The Initial Developer of the Original Code is Neil A. Wilson.
* Portions created by Neil A. Wilson are Copyright (C) 2004-2010.
* Some preexisting portions Copyright (C) 2002-2006 Sun Microsystems, Inc.
* All Rights Reserved.
*
* Contributor(s): Neil A. Wilson
*/
package com.slamd.dslogplay;
import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.ResultCode;
/**
* This class defines an LDAP add operation parsed from an access log and
* implements the necessary logic to parse the log file line and to replay the
* operation against a Directory Server.
*
*
* @author Neil A. Wilson
*/
public class AddOperation
extends LogOperation
{
// The DN of the entry to add.
private final String dn;
/**
* Creates a new add operation with the provided information.
*
* @param dn The DN of the entry to add.
*/
public AddOperation(String dn)
{
this.dn = dn;
}
/**
* Parses the provided line as appropriate for this type of operation and
* constructs a new log operation based on the information it contains.
*
* @param jobThread The job thread with which this parser is associated.
* @param line The line to be parsed and converted to a log operation.
*
* @return The log operation created from the provided log line, or
* <CODE>null</CODE> if a problem prevented the line from being
* parsed properly.
*/
public static LogOperation parseLogLine(LogPlaybackJobClass jobThread,
String line)
{
try
{
int addPos = line.indexOf("ADD dn=\"");
if (addPos < 0)
{
return null;
}
int closePos = line.indexOf('"', addPos+8);
String dn = line.substring(addPos+8, closePos);
return new AddOperation(dn);
}
catch (Exception e)
{
jobThread.writeVerbose("Unable to parse add line \"" + line + "\": " +
e);
return null;
}
}
/**
* Replays this operation against the directory server using the information
* contained in the provided job thread.
*
* @param jobThread The job thread to use when replaying this operation.
*/
public void replayOperation(LogPlaybackJobClass jobThread)
{
String firstString = jobThread.getRandomString(8);
String lastString = jobThread.getRandomString(8);
Entry entry = new Entry(dn,
new Attribute("objectClass", "top", "person", "organizationalPerson",
"inetOrgPerson", "extensibleObject"),
new Attribute("givenName", firstString),
new Attribute("sn", lastString),
new Attribute("cn", firstString + ' ' + lastString));
String resultCode = DEFAULT_RESULT_CODE;
boolean tryDelete = false;
try
{
jobThread.totalTimer.startTimer();
jobThread.addTimer.startTimer();
jobThread.opConnection.add(entry);
}
catch (LDAPException le)
{
ResultCode rc = le.getResultCode();
if (LogPlaybackJobClass.deleteExistingAdds &&
(rc == ResultCode.ENTRY_ALREADY_EXISTS))
{
tryDelete = true;
}
else
{
resultCode = rc.intValue() + " (" + rc.getName() + ')';
}
}
catch (Exception e)
{
resultCode = ResultCode.OTHER_INT_VALUE + " (" + e + ')';
}
finally
{
jobThread.totalTimer.stopTimer();
jobThread.addTimer.stopTimer();
jobThread.totalReplayed.increment();
jobThread.addsReplayed.increment();
jobThread.opRatios.increment("Add");
jobThread.resultCodes.increment(resultCode);
}
if (tryDelete)
{
try
{
resultCode = DEFAULT_RESULT_CODE;
jobThread.totalTimer.startTimer();
jobThread.deleteTimer.startTimer();
jobThread.opConnection.delete(dn);
}
catch (LDAPException le)
{
ResultCode rc = le.getResultCode();
resultCode = rc.intValue() + " (" + rc.getName() + ')';
}
catch (Exception e)
{
resultCode = ResultCode.OTHER_INT_VALUE + " (" + e + ')';
}
finally
{
jobThread.totalTimer.stopTimer();
jobThread.deleteTimer.stopTimer();
jobThread.totalReplayed.increment();
jobThread.deletesReplayed.increment();
jobThread.opRatios.increment("Delete");
jobThread.resultCodes.increment(resultCode);
}
}
}
/**
* Retrieves a string representation of this log operation.
*
* @return A string representation of this log operation.
*/
public String toString()
{
return "ADD dn=\"" + dn + '"';
}
}