use of com.sun.identity.log.util.LogRecordSorter in project OpenAM by OpenRock.
the class LogReadDBHandler method logRecRead.
/**
* LogReader calls this method. It collects header, records,
* applies query (if any), sorts (if asked) the records on field, checks
* the max records to return, collects all the recods and returns.
*
* @param tableNames db table names
* @param logQuery is user specified qury chriteria with sorting requirement
* @param logMgr the log manager associated with this handler
* @param sourceData it specifies whether return data should be original
* data received by logger (source) or formatted data as in file.
* @return all the matched records with query
* @throws IOException if it fails to read log records.
* @throws NoSuchFieldException if it fails to retrieve the name of field.
* @throws IllegalArgumentException if query has wrong value.
* @throws RuntimeException if it fails to retrieve log record.
* @throws SQLException if it fails to process sql query.
* @throws Exception if it fails any of operation.
*/
public String[][] logRecRead(Set tableNames, LogQuery logQuery, java.util.logging.LogManager logMgr, boolean sourceData) throws IOException, NoSuchFieldException, IllegalArgumentException, RuntimeException, SQLException, Exception {
String sortField = null;
// if the object is persistence use it otherwise don't
this.cleaner();
//
// tblNames is needed for the guaranteed "underscore" form
// (e.g., "amAuthentication_access") of the table names
//
Set tblNames = new HashSet();
StringBuilder allTablesSB = new StringBuilder("");
for (Iterator it = tableNames.iterator(); it.hasNext(); ) {
String ss = (String) it.next();
String ss2 = ss.replace('.', '_');
tblNames.add(ss2);
allTablesSB.append(ss2);
}
//
try {
this.databaseURL = logMgr.getProperty(LogConstants.LOG_LOCATION);
this.dbDriver = logMgr.getProperty(LogConstants.DB_DRIVER);
this.dbUserName = logMgr.getProperty(LogConstants.DB_USER);
this.dbPassWord = logMgr.getProperty(LogConstants.DB_PASSWORD);
this.maxRecordsStr = logMgr.getProperty(LogConstants.MAX_RECORDS);
} catch (Exception e) {
Debug.error("DBLogRecReadSet:config: ", e);
// rethrow the exception
throw e;
}
if (this.dbDriver.toLowerCase().indexOf("oracle") != -1) {
isMySQL = false;
} else if (this.dbDriver.toLowerCase().indexOf("mysql") != -1) {
isMySQL = true;
} else {
isMySQL = false;
Debug.warning("DBlogRecRead:assuming driver: '" + this.dbDriver + "' is Oracle-compatible.");
}
try {
this.maxRecords = Integer.parseInt(maxRecordsStr);
} catch (NumberFormatException nfe) {
if (Debug.warningEnabled()) {
Debug.warning("DBlogRecRead(s): maxRecords error (" + maxRecordsStr + "), set to MAX");
}
this.maxRecords = LogConstants.MAX_RECORDS_DEFAULT_INT;
}
//
// kind of a bad situation here between Oracle and MySQL.
// pre-v4 MySQL doesn't support the "union" operator, so multi-
// table selects has to be affected as multiple single-table
// selects and combining their results.
//
// the Oracle case is more straightforward, and if we decide
// to only support >= V4 MySQL, can just use the !isMySQL
// branch.
//
String selectStr;
if (!isMySQL) {
if (sourceData == true) {
String temps = logQuery.getSortingField();
if (temps != null) {
sortField = temps.trim();
}
// default all
String columns = "*";
ArrayList sCol = logQuery.getColumns();
if (sCol != null) {
StringBuilder colSB = new StringBuilder();
int sSize = sCol.size();
for (int i = 0; i < sSize; i++) {
colSB.append((String) sCol.get(i));
if ((i + 1) < sSize) {
colSB.append(", ");
}
}
columns = colSB.toString();
}
selectStr = lq2Select(tblNames, columns, logQuery);
if (Debug.messageEnabled()) {
Debug.message("logRecRead/4:selectStr = " + selectStr);
}
} else {
// default all
String columns = "*";
selectStr = lq2Select(tblNames, columns, null);
if (Debug.messageEnabled()) {
Debug.message("logRecRead/4.2:selectStr = " + selectStr);
}
}
String[][] tableResults;
try {
connectToDatabase(dbUserName, dbPassWord);
} catch (SQLException sqe) {
Debug.error("DBlogRecRead:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
// rethrow for LogReader to catch
throw sqe;
} catch (ClassNotFoundException cnfe) {
// rethrow for LogReader to catch
throw cnfe;
}
String selStr = selectStr;
Statement stmt = null;
int numberOfRows = 0;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:about to execute: " + selStr);
}
ResultSet rs = stmt.executeQuery(selStr);
//
// fetchsize appears to be 10 from rs.getFetchSize();
//
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:#columns = " + numberOfColumns);
}
//
// these are the column (field) names
//
String[] spltHdrStr = new String[numberOfColumns];
for (int i = 1; i <= numberOfColumns; i++) {
String tempstr = rsmd.getColumnName(i);
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:col #" + i + " name = " + tempstr);
}
spltHdrStr[i - 1] = tempstr;
}
listOfValidRecords.add(spltHdrStr);
// have to figure out #rows
while (rs.next()) {
numberOfRows++;
}
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:#rows = " + numberOfRows);
}
if (numberOfRows == 0) {
stmt.close();
try {
conn.close();
} catch (SQLException ex) {
//
// might not care about this too much...?
//
Debug.error("DBlogRecRead:rows=0:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
//
// should be at least the column names
//
int recSize = listOfValidRecords.size();
if (recSize <= 0) {
return null;
}
queryResult = new String[recSize][];
for (int i = 0; i < recSize; i++) {
queryResult[i] = (String[]) listOfValidRecords.get(i);
}
return queryResult;
}
if (numberOfRows > this.maxRecords) {
stmt.close();
try {
conn.close();
} catch (SQLException ex) {
//
// don't care about this too much...
//
Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
throw new AMLogException(AMLogException.LOG_DB_TOOMANYRECORDS);
}
//
// reset to the beginning
//
boolean isFirst = rs.first();
if (isFirst == false) {
Debug.error("DBlogRecRead:first() is false!");
}
//
// think we're not going to allow MOST_RECENT_MAX_RECORDS
// with multi-table query...
//
int rowsToAlloc = numberOfRows;
tableResults = new String[rowsToAlloc][numberOfColumns];
String result = null;
int rowCount = 0;
for (int i = 0; i < numberOfColumns; i++) {
result = rs.getString(i + 1);
tableResults[0][i] = result;
}
rowCount = 1;
while (rs.next()) {
for (int i = 0; i < numberOfColumns; i++) {
result = rs.getString(i + 1);
tableResults[rowCount][i] = result;
}
rowCount++;
}
stmt.close();
} catch (SQLException se) {
Debug.error("DBlogRecRead:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
// rethrow for LogReader to catch
throw se;
}
try {
this.getRecords(tableResults, sourceData);
} catch (IOException e) {
// catch & rethrow it
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow it
throw e;
} catch (RuntimeException e) {
// catch & rethrow it
throw e;
} catch (Exception e) {
// catch & rethrow it
throw e;
}
int recSize = listOfValidRecords.size();
// checks whether it has got any record or not
if (recSize <= 0) {
// if no record found return null
return null;
}
try {
conn.close();
} catch (SQLException ex) {
//
// might not care about this too much...?
//
Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
queryResult = new String[recSize][];
for (int i = 0; i < recSize; i++) {
queryResult[i] = (String[]) listOfValidRecords.get(i);
}
} else {
// else (isMySQL case)
//
// Multi-table select for <V4 MySQL is essentially
// looping through the tables and combining the results.
//
String columns = null;
if (sourceData == true) {
String temps = logQuery.getSortingField();
if (temps != null) {
sortField = temps.trim();
}
// default all
columns = "*";
ArrayList sCol = logQuery.getColumns();
if (sCol != null) {
StringBuilder colSB = new StringBuilder();
int sSize = sCol.size();
for (int i = 0; i < sSize; i++) {
colSB.append((String) sCol.get(i));
if ((i + 1) < sSize) {
colSB.append(", ");
}
}
columns = colSB.toString();
}
} else {
// default all
columns = "*";
}
//
// do same select on each table
//
boolean isFirstTable = true;
int totalNumberOfRows = 0;
int recSize = 0;
for (Iterator it = tblNames.iterator(); it.hasNext(); ) {
String thisTable = (String) it.next();
if (sourceData == true) {
selectStr = lq2Select(thisTable, columns, logQuery);
if (Debug.messageEnabled()) {
Debug.message("logRecRead/5:selectStr = " + selectStr);
}
} else {
selectStr = lq2Select(thisTable, columns, null);
if (Debug.messageEnabled()) {
Debug.message("logRecRead/5.2:selectStr = " + selectStr);
}
}
String[][] tableResults = null;
try {
connectToDatabase(dbUserName, dbPassWord);
} catch (SQLException sqe) {
Debug.error("DBlogRecRead:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
// rethrow for LogReader to catch
throw sqe;
} catch (ClassNotFoundException cnfe) {
// rethrow for LogReader to catch
throw cnfe;
}
String selStr = selectStr;
Statement stmt = null;
int numberOfRows = 0;
//
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:about to execute: " + selStr);
}
ResultSet rs = stmt.executeQuery(selStr);
//
// fetchsize appears to be 10 from rs.getFetchSize();
//
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:#columns = " + numberOfColumns);
}
if (isFirstTable) {
String[] spltHdrStr = new String[numberOfColumns];
for (int i = 1; i <= numberOfColumns; i++) {
String tempstr = rsmd.getColumnName(i);
if (Debug.messageEnabled()) {
Debug.message("DBlogRecRead:col #" + i + " name = " + tempstr);
}
spltHdrStr[i - 1] = tempstr;
}
listOfValidRecords.add(spltHdrStr);
}
// have to figure out #rows
numberOfRows = 0;
while (rs.next()) {
numberOfRows++;
}
totalNumberOfRows += numberOfRows;
if (totalNumberOfRows > this.maxRecords) {
stmt.close();
try {
conn.close();
} catch (SQLException ex) {
//
// don't care about this too much...
//
Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
throw new AMLogException(AMLogException.LOG_DB_TOOMANYRECORDS);
}
if (numberOfRows > 0) {
//
// reset to the "beginning"
//
boolean isFirst = rs.first();
if (isFirst == false) {
Debug.error("DBlogRecRead:first() is false!");
}
//
// think we're not going to allow
// MOST_RECENT_MAX_RECORDS with multi-table query...
//
tableResults = new String[numberOfRows][numberOfColumns];
String result = null;
int rowCount = 0;
do {
for (int i = 0; i < numberOfColumns; i++) {
result = rs.getString(i + 1);
tableResults[rowCount][i] = result;
}
rowCount++;
} while (rs.next());
}
//
// print the actual results in the debug log
//
stmt.close();
} catch (SQLException se) {
Debug.error("DBlogRecRead:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
// rethrow for LogReader to catch
throw se;
}
if (numberOfRows > 0) {
try {
this.getRecords(tableResults, sourceData);
} catch (IOException e) {
// catch & rethrow it
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow it
throw e;
} catch (RuntimeException e) {
// catch & rethrow it
throw e;
} catch (Exception e) {
// catch & rethrow it
throw e;
}
}
if (isFirstTable) {
isFirstTable = false;
}
}
try {
conn.close();
} catch (SQLException ex) {
//
// might not care about this too much...?
//
Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
if (logQuery != null) {
String sortByField = logQuery.getSortingField();
if (sortByField != null) {
try {
this.sorter = new LogRecordSorter(sortByField, listOfValidRecords);
queryResult = this.sorter.getSortedRecords();
} catch (NoSuchFieldException e) {
Debug.error("DBlogRecRead/5:sort:nsfe: " + e.getMessage());
throw e;
} catch (IllegalArgumentException e) {
Debug.error("DBlogRecRead/5:sort:iae: " + e.getMessage());
throw e;
} catch (RuntimeException e) {
Debug.error("DBlogRecRead/5:sort:rte: " + e.getMessage());
throw e;
} catch (Exception e) {
Debug.error("DBlogRecRead/5:sort:ex: " + e.getMessage());
throw e;
}
return (queryResult);
}
}
recSize = listOfValidRecords.size();
// checks whether it has got any record or not
if (recSize <= 0) {
// if no record found return null
return null;
}
//
// can we use the toArray() converter?
//
queryResult = new String[recSize][];
for (int i = 0; i < recSize; i++) {
queryResult[i] = (String[]) listOfValidRecords.get(i);
}
}
return queryResult;
}
use of com.sun.identity.log.util.LogRecordSorter in project OpenAM by OpenRock.
the class LogReadFileHandler method logRecRead.
/**
* LogReader calls this method method. It collects header, records,
* applies query (if any), sorts (if asked) the records on field, checks
* the max records to return, collects all the recods and returns.
*
* @param fileName is complete filename with path
* @param logQuery is user specified qury chriteria with sorting requirement
* @param sourceData it specifies whether return data should be original
* data received by logger (source) or formatted data as in file.
* @return all the matched records with query
* @throws IOException if it fails to read log records.
* @throws NoSuchFieldException if it fails to retrieve the name of field.
* @throws IllegalArgumentException if query has wrong value.
* @throws RuntimeException if it fails to retrieve log record.
* @throws Exception if it fails any of operation.
*/
public String[][] logRecRead(String fileName, LogQuery logQuery, boolean sourceData) throws IOException, NoSuchFieldException, IllegalArgumentException, RuntimeException, Exception {
// if the object is persistence use it otherwise don't
this.cleaner();
this.logFileName = fileName;
boolean hdrExist = false;
if (sourceData == true) {
queryChriteria = logQuery;
}
// to collect field names
try {
hdrExist = this.getFieldNames(true, logQuery);
} catch (IOException e) {
// catch & rethrow it else it cud be caught in other place
throw e;
} catch (RuntimeException e) {
// catch & rethrow it else it cud be caught in other place
throw e;
} catch (Exception e) {
// catch & rethrow it else it cud be caught in other place
throw e;
}
if (hdrExist == false) {
return queryResult;
}
if (logQuery != null) {
// this class has no idea about the value
if ((logQuery.getNumRecordsWanted() == LogQuery.MOST_RECENT_MAX_RECORDS) || (logQuery.getNumRecordsWanted() < LogQuery.ALL_RECORDS)) {
this.maxNoOfRecs = 1;
} else {
this.maxNoOfRecs = logQuery.getNumRecordsWanted();
}
} else {
// can't be 0, headers will be returned
this.maxNoOfRecs = 1;
}
try {
if (sourceData == true) {
this.getRecords(true);
} else {
this.getRecords(false);
}
} catch (IOException e) {
// catch & rethrow it else it cud be caught in other place
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow it else it cud be caught in other place
throw e;
} catch (RuntimeException e) {
// catch & rethrow it else it cud be caught in other place
throw e;
} catch (Exception e) {
// catch & rethrow it else it cud be caught in other place
throw e;
}
int recSize = listOfValidRecords.size();
// checks whether it has got any record or not
if (recSize <= 0) {
// if no record found return null
return null;
}
// if Sorting is specified
if (queryChriteria != null) {
String sortByField = queryChriteria.getSortingField();
if (sortByField != null) {
try {
this.sorter = new LogRecordSorter(sortByField, listOfValidRecords);
queryResult = this.sorter.getSortedRecords();
return (queryResult);
} catch (NoSuchFieldException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (RuntimeException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (Exception e) {
// catch & rethrow else cud be caught in other place
throw e;
// don't do any processing
}
}
}
queryResult = new String[recSize][];
for (int i = 0; i < recSize; i++) {
queryResult[i] = (String[]) listOfValidRecords.get(i);
}
return queryResult;
}
use of com.sun.identity.log.util.LogRecordSorter in project OpenAM by OpenRock.
the class LogReadFileHandler method logRecRead.
/**
* LogReader calls this method method. It collects header, records,
* applies query (if any), sorts (if asked) the records on field, checks
* the max records to return, collects all the recods and returns.
*
* @param fileNames is a Set of filenames complete with path
* @param logQuery is user specified qury chriteria with sorting requirement
* @param sourceData it specifies whether return data should be original
* data received by logger (source) or formatted data as in file.
* @return all the matched records with query
* @throws IOException if it fails to read log records.
* @throws NoSuchFieldException if it fails to retrieve the name of field.
* @throws IllegalArgumentException if query has wrong value.
* @throws RuntimeException if it fails to retrieve log record.
* @throws Exception if it fails any of operation.
*/
public String[][] logRecRead(Set fileNames, LogQuery logQuery, boolean sourceData) throws IOException, NoSuchFieldException, IllegalArgumentException, RuntimeException, Exception {
// if the object is persistence use it otherwise don't
this.cleaner();
Set fNames = new HashSet();
boolean isFirstFile = true;
for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
String ss = (String) it.next();
fNames.add(ss);
this.logFileName = ss;
if (Debug.messageEnabled()) {
Debug.message("File:logRecRead/2: processing file " + ss + ", sourceData = " + sourceData);
}
boolean hdrExist = false;
if (sourceData == true) {
queryChriteria = logQuery;
}
// to collect field names
try {
hdrExist = this.getFieldNames(isFirstFile, logQuery);
isFirstFile = false;
} catch (IOException e) {
// catch & rethrow it
throw e;
} catch (RuntimeException e) {
// catch & rethrow it
throw e;
} catch (Exception e) {
// catch & rethrow it
throw e;
}
if (hdrExist == false) {
return queryResult;
}
if (logQuery != null) {
// this class has no idea about the value
if ((logQuery.getNumRecordsWanted() == LogQuery.MOST_RECENT_MAX_RECORDS) || (logQuery.getNumRecordsWanted() < LogQuery.ALL_RECORDS)) {
this.maxNoOfRecs = 1;
} else {
this.maxNoOfRecs = logQuery.getNumRecordsWanted();
}
} else {
// can't be 0, headers will be returned
this.maxNoOfRecs = 1;
}
try {
if (sourceData == true) {
this.getRecordsMulti(true);
} else {
this.getRecordsMulti(false);
}
} catch (IOException e) {
// catch & rethrow
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow
throw e;
} catch (RuntimeException e) {
// catch & rethrow
throw e;
} catch (Exception e) {
// catch & rethrow
throw e;
}
}
// end of for loop for all files
int recSize = listOfValidRecords.size();
// checks whether it has got any record or not
if (recSize <= 0) {
// if no record found return null
return null;
}
// if Sorting is specified
if (queryChriteria != null) {
String sortByField = queryChriteria.getSortingField();
if (sortByField != null) {
try {
this.sorter = new LogRecordSorter(sortByField, listOfValidRecords);
queryResult = this.sorter.getSortedRecords();
return (queryResult);
} catch (NoSuchFieldException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (IllegalArgumentException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (RuntimeException e) {
// catch & rethrow else cud be caught in other place
throw e;
} catch (Exception e) {
// catch & rethrow else cud be caught in other place
throw e;
// don't do any processing
}
}
}
queryResult = new String[recSize][];
for (int i = 0; i < recSize; i++) {
queryResult[i] = (String[]) listOfValidRecords.get(i);
}
return queryResult;
}
Aggregations