import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.datamirror.ea.api.AccessServerEvent;
import com.datamirror.ea.api.ApiException;
import com.datamirror.ea.api.DataSource;
import com.datamirror.ea.api.DefaultContext;
import com.datamirror.ea.api.Toolkit;
import com.datamirror.ea.api.messaging.DefaultDataSource;
/**
* Connects to Access Server and requests Access Server audit trail events
* for the specified time range, then writes the events to the specified file.
*/
public class ExportAccessServerAuditLogSample
{
public static String ACCESS_SERVER_HOST_TOKEN = "-host"; //$NON-NLS-1$
public static String ACCESS_SERVER_PORT_TOKEN = "-port"; //$NON-NLS-1$
public static String ACCESS_SERVER_USER_TOKEN = "-user"; //$NON-NLS-1$
public static String ACCESS_SERVER_PASSWORD_TOKEN = "-password"; //$NON-NLS-1$
public static String FILENAME_TOKEN = "-filename"; //$NON-NLS-1$
public static String START_TIME_TOKEN = "-starttime"; //$NON-NLS-1$
public static String END_TIME_TOKEN = "-endtime"; //$NON-NLS-1$
private DataSource accessServer;
/**
* Runs the sample.
*
* @param host
* the host name for access server.
* @param port
* the port number of access server in the host machine.
* @param user
* the login user name.
* @param password
* the login password.
* @param startTime
* timestamp of the start time of the events
* @param endTime
* timestamp of the end time of the events
* @param fileName
* the name of the file for saving audit trail
* @throws ApiException
* if an error occurred.
*/
public void run(String host, String port, String user, String password, long startTime, long endTime,
String fileName) throws ApiException
{
try
{
connectAccessServer(host, port, user, password);
System.out.println("Starting Access Server audit log exporting, please wait..."); //$NON-NLS-1$
DefaultDataSource dataSource = (DefaultDataSource) accessServer;
AccessServerEvent[] events = dataSource.getAccessServerEvents(startTime, endTime,
AccessServerEvent.TYPE_AUDIT_TRAIL);
boolean append = false;
File file = dataSource.exportAccessAuditLog(events, fileName, append);
System.out.println("Audit trail saved to " + file.getAbsolutePath()); //$NON-NLS-1$
}
finally
{
disconnectAccessServer();
}
}
/**
* Connects to Access Server.
*
* @param host
* the host name for access server.
* @param port
* the port number of access server in the host machine.
* @param user
* the login user name.
* @param password
* the login password.
* @throws ApiException
* if an error occurred.
*/
public void connectAccessServer(String host, String port, String user, String password)
throws ApiException
{
try
{
System.out.println("Connecting to Access Server..."); //$NON-NLS-1$
accessServer = Toolkit.getDefaultToolkit().createDataSource();
DefaultContext eaAccessContext = new DefaultContext();
eaAccessContext.setString(DataSource.User, user);
eaAccessContext.setString(DataSource.Password, password);
eaAccessContext.setString(DataSource.Hostname, host);
eaAccessContext.setInt(DataSource.Port, Integer.parseInt(port));
accessServer.connect(eaAccessContext);
System.out.println("Connected."); //$NON-NLS-1$
}
catch (ApiException e)
{
throw new ApiException("Failed to connect to " + host + "@" + port + " as " + user); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
/**
* Disconnects from Access Server.
*
* @throws ApiException
* if an error occurred.
*/
public void disconnectAccessServer() throws ApiException
{
boolean disconnecting = false;
if (accessServer != null && accessServer.isOpen())
{
System.out.println("Disconnecting from Access Server..."); //$NON-NLS-1$
disconnecting = true;
accessServer.close();
}
if (accessServer != null && accessServer.isOpen())
{
throw new ApiException("Failed to disconnect from AccessServer"); //$NON-NLS-1$
}
else if (disconnecting)
{
System.out.println("Disconnected."); //$NON-NLS-1$
}
}
/**
* Returns an example of arguments for the class.
*
* @return the help string.
*/
public static String getHelp()
{
String help = "java -classpath \"{pathToSample and api.jar}\" ExportAccessServerAuditLogSample\n" //$NON-NLS-1$
+ " [-host accessServerHost -port accessServerPort]\n" //$NON-NLS-1$
+ " -user accessServerUser -password accessServerPassword\n" //$NON-NLS-1$
+ " -startTime event start time in yyyyMMddHHmm\n" //$NON-NLS-1$
+ " -endTime event end time in yyyyMMddHHmm\n" //$NON-NLS-1$
+ " -fileName file name for storing the audit log events\n\n" //$NON-NLS-1$
+ "If -host or -port are not specified, \"localhost\" and \"10101\" will be used.\n" //$NON-NLS-1$
+ "If -endTime is not specified, current date time will be used.\n"; //$NON-NLS-1$
return help;
}
/**
* Returns the named argument's value.
*
* @param args
* the command line arguments.
* @param name
* the name of the command line argument.
* @param defaultValue
* the default value. If null is specified, then an exception is
* thrown if the parameter does not exist.
* @return value for the argument.
* @throws IllegalArgumentException
* if the argument is missing.
*/
private String getParameter(String[] args, String name, String defaultValue)
throws IllegalArgumentException
{
String parameter = null;
for (int i = 0; i < args.length - 1; i++)
{
if (args[i].equalsIgnoreCase(name))
{
parameter = args[i + 1];
break;
}
}
if (parameter == null)
{
if (defaultValue == null)
{
throw new IllegalArgumentException("Parameter " + name + " is missing."); //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
parameter = defaultValue;
}
}
return parameter;
}
public static void main(String[] args)
{
try
{
ExportAccessServerAuditLogSample sample = new ExportAccessServerAuditLogSample();
// Access server connection information.
String host = sample.getParameter(args, ACCESS_SERVER_HOST_TOKEN, "localhost"); //$NON-NLS-1$
String port = sample.getParameter(args, ACCESS_SERVER_PORT_TOKEN, "10101"); //$NON-NLS-1$
String user = sample.getParameter(args, ACCESS_SERVER_USER_TOKEN, null);
String password = sample.getParameter(args, ACCESS_SERVER_PASSWORD_TOKEN, null);
String startTimeString = null;
startTimeString = sample.getParameter(args, START_TIME_TOKEN, ""); //$NON-NLS-1$
if (startTimeString == null || startTimeString.length() == 0)
{
throw new IllegalArgumentException("Parameter " + START_TIME_TOKEN + " is required."); //$NON-NLS-1$ //$NON-NLS-2$
}
if (startTimeString.length() != 12) // yyyyMMddHHmm
{
throw new IllegalArgumentException("Value is invalid for parameter " + START_TIME_TOKEN); //$NON-NLS-1$
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm"); //$NON-NLS-1$
dateFormat.setLenient(false);
Date date = dateFormat.parse(startTimeString);
long startTime = date.getTime();
String endTimeString = null;
long endTime = 0;
endTimeString = sample.getParameter(args, END_TIME_TOKEN, "");//$NON-NLS-1$
if (endTimeString == null || endTimeString.length() == 0)
{
endTime = new Date().getTime();
}
else
{
if (endTimeString.length() != 12) // yyyyMMddHHmm
{
throw new IllegalArgumentException("Value is invalid for parameter " + END_TIME_TOKEN); //$NON-NLS-1$
}
date = dateFormat.parse(endTimeString);
endTime = date.getTime();
}
String fileNameString = sample.getParameter(args, FILENAME_TOKEN, null);
if (fileNameString == null || fileNameString.length() == 0)
{
throw new IllegalArgumentException("Parameter " + FILENAME_TOKEN //$NON-NLS-1$
+ " is required."); //$NON-NLS-1$
}
sample.run(host, port, user, password, startTime, endTime, fileNameString);
}
catch (IllegalArgumentException e)
{
System.out.println(ExportAccessServerAuditLogSample.getHelp());
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.out.println();
e.printStackTrace();
}
}
}