use of com.sun.identity.log.AMLogException in project OpenAM by OpenRock.
the class AMLogTest method readLogQuery.
private void readLogQuery(String fileName, SSOToken ssot, int recsWritten) throws AMLogException {
/*
* can have:
* LogQuery(), which sets:
* maxRecord = LogQuery.MOST_RECENT_MAX_RECORDS
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record), which sets:
* maxRecord = max_record
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record,
* int matchCriteria,
* String sortingBy), which sets:
* maxRecord = max_record
* globalOperand = matchCriteria
* sortBy = sortingBy
* queries = null
*
* use lq.addQuery(QueryElement qryElement) to
* add query elements to the LoqQuery's list of queries
* QueryElement(String fld, String val, int rel)
* fieldName = fld
* fieldValue = val
* relation = rel
* QueryElement()
* fieldName = new String()
* fieldValue = new String()
* relation = QueryElement.EQ
* use:
* QueryElement.setFieldName(String field)
* QueryElement.setFieldValue(String value)
* QueryElement.setRelation(int value)
*/
int totalRecsRead = 0;
/*
* test 1:
* all records, any condition, no sorting
* should be same as read all.
*/
LogQuery lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, null);
int numRecs = 0;
String[][] result = new String[1][1];
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != (recsWritten + 2)) {
throw new AMLogException("Number of records read test 1 (" + numRecs + ") doesn't match expected (" + (recsWritten + 1) + ")");
}
/*
* test 2:
* only read 2 most recent records
*/
lq = new LogQuery(2, LogQuery.MATCH_ANY_CONDITION, null);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 3) {
throw new AMLogException("Number of records read test 2 (" + (numRecs - 1) + ") doesn't match expected (2)");
}
// two most recent should be "...data #1" and "...data #0"
String temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "1")) {
throw new AMLogException("Read test 2: second most recent = " + temp + ", not " + msgDataPrefixData + "1");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 2: most recent = " + temp + ", not " + msgDataPrefixData + "0");
}
/*
* test 3:
* get records that end with "XX2" in the MessageID field.
* should only be one.
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ALL_CONDITIONS, null);
QueryElement qe = new QueryElement("MessageID", "XX2", QueryElement.EW);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 2) {
throw new AMLogException("Number of records read test 3 (" + (numRecs - 1) + ") doesn't match expected (1)");
}
// assume MessageID is in column 8 (starting from 0)
temp = result[1][8];
if (!temp.contains("XX2")) {
throw new AMLogException("Read test 3: record = " + temp + ", not XX2");
}
/*
* test 4:
* get records that contain "data #4", "data #0" or "data #2"
* in the Data field. specify them in that order, with
* sort by Data field.
* msgDataPrefixData = "data #"
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, "Data");
qe = new QueryElement("Data", msgDataPrefixData + "4", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "0", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "2", QueryElement.CN);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 4) {
throw new AMLogException("Number of records read test 4 (" + (numRecs - 1) + ") doesn't match expected (3)");
}
/*
* gonna expect Data column is in result[x][1]. also
* that with sorting, result[1][1] contains "...data #0",
* result[2][1] contains "...data #2", and result[3][1]
* contains "...data #4".
*/
temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 4: first sorted record = " + temp + ", not " + msgDataPrefixData + "0");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "2")) {
throw new AMLogException("Read test 4: second sorted record = " + temp + ", not " + msgDataPrefixData + "2");
}
temp = result[3][1];
if (!temp.contains(msgDataPrefixData + "4")) {
throw new AMLogException("Read test 4: third sorted record = " + temp + ", not " + msgDataPrefixData + "4");
}
}
use of com.sun.identity.log.AMLogException in project OpenAM by OpenRock.
the class IdentityServicesImpl method log.
@Override
public LogResponse log(Token app, Token subject, String logName, String message) throws AccessDenied, TokenExpired, GeneralFailure {
if (app == null) {
throw new AccessDenied("No logging application token specified");
}
SSOToken appToken;
SSOToken subjectToken;
appToken = getSSOToken(app);
subjectToken = subject == null ? appToken : getSSOToken(subject);
try {
LogRecord logRecord = new LogRecord(java.util.logging.Level.INFO, message, subjectToken);
//TODO Support internationalization via a resource bundle specification
Logger logger = (Logger) Logger.getLogger(logName);
logger.log(logRecord, appToken);
logger.flush();
} catch (AMLogException e) {
debug.error("IdentityServicesImpl:log", e);
throw new GeneralFailure(e.getMessage());
}
return new LogResponse();
}
use of com.sun.identity.log.AMLogException in project OpenAM by OpenRock.
the class DBHandler method logRecords.
private void logRecords(LinkedList<LogRecord> records) {
//
if ((conn == null) || connectionToDBLost) {
//
try {
reconnectToDatabase();
Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase successful.");
} catch (DriverLoadException dle) {
Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase:DLE: " + dle.getMessage());
//
// if the max mem buffer is exceeded, dump the records
//
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_DRIVER + "'" + driver + "'");
} catch (ConnectionException ce) {
Debug.error(tableName + ":DBHandler:logRecords:reconnectToDatabase:CE: " + ce.getMessage());
//
// if the max mem buffer is exceeded, dump the records
//
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_CONNECT_FAILED);
}
//
// re-established the connection to the DB. now
// check on the table.
//
connectionToDBLost = false;
try {
//
// any exception from createTable() might mean the
// table's not in the DB... just record the error, and
// let the insert let us know if the record didn't get
// logged.
//
createTable(tableName);
} catch (SQLException se) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:reconnect:cTable:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
}
} catch (UnsupportedEncodingException usee) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:reconnect:cTable:UE: " + usee.getMessage());
}
}
}
//
// when using oracle, and the db is down, you get an
// exception on the createStatement. unfortunately,
// it's a TTC message (e.g. [ORA-]17310... the getErrorCode
// returns 17310), a vendor-specific error code.
//
// MySQL db, on the other hand seems to return from
// the createStatement() call "ok". catch it on the
// executeUpdate(), below.
//
Statement testConnectionStatement = null;
try {
testConnectionStatement = conn.createStatement();
} catch (SQLException se) {
//
// observed that when Oracle's down, it's detected here.
// error code 1034.
//
Debug.error(tableName + ":DBHandler:logRecords:cStatement:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
//
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
//
if (Debug.messageEnabled()) {
Debug.error(tableName + ":DBHandler:logRecords:cStatement:close:SQLE (" + ex.getErrorCode() + ")" + ex.getMessage());
}
}
connectionToDBLost = true;
try {
reconnectToDatabase();
Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect successful.");
} catch (DriverLoadException dle) {
Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:DLE: " + dle.getMessage());
//
// if the max mem buffer is exceeded, dump the records
//
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_DRIVER + "'" + driver + "'");
} catch (ConnectionException ce) {
Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:CE: " + ce.getMessage());
//
// if the max mem buffer is exceeded, dump the records
//
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_CONNECT_FAILED);
}
connectionToDBLost = false;
//
try {
createTable(tableName);
} catch (SQLException sqle) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:cStatement:reconnect:cTable:SQLE (" + sqle.getErrorCode() + "): " + sqle.getMessage());
}
} catch (UnsupportedEncodingException usee) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:cStatement:reconnect: cTable:UE: " + usee.getMessage());
}
}
try {
testConnectionStatement = conn.createStatement();
} catch (SQLException sqle) {
//
// second time this failed (note that this whole block
// started with the createStatement()).
// log the error message, and continue on (for now)
//
Debug.error(tableName + ":DBHandler:logRecords:cStatement:reconnect:cSt:SQLE (" + sqle.getErrorCode() + "): " + sqle.getMessage());
throw new AMLogException(AMLogException.LOG_DB_CSTATEMENT);
}
} finally {
closeStatement(testConnectionStatement);
}
PreparedStatement insertStatement = null;
for (LogRecord record : records) {
List<String> values = getValues(record);
try {
insertStatement = getInsertPreparedStatement(values);
insertStatement.executeUpdate();
//Monit start
if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
dbLogHandlerForMonitoring.incHandlerSuccessCount(1);
}
//Monit end
} catch (SQLException sqle) {
// Attempt to close this just in case it is holding resources.
closeStatement(insertStatement);
insertStatement = null;
/*
* as mentioned above, connection errors to oracle
* seem to get caught in the createStatement(), while
* with mysql, they get caught here.
*
* the other thing that could happen is the table was
* dropped, but not the connection.
*/
int sqleErrCode = sqle.getErrorCode();
boolean tableDoesNotExist = false;
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:SQLException (" + sqleErrCode + "): " + sqle.getMessage());
}
/*
* unfortunately have to check which db and specific
* error codes...
* see if table's missing
* MySQL: 1146
* Oracle: 942
*/
if ((isMySQL && (sqleErrCode == 1146)) || (!isMySQL && (sqleErrCode == 942))) {
/*
* connection to DB's there, but table's missing
*
* gotta make the table; try the executeUpdate()
* again
*/
try {
createTable(tableName);
} catch (SQLException se) {
// just log the message and continue, for now
Debug.error(tableName + ":DBHandler:logRecords:execUpdate:cTable:SQLE (" + se.getErrorCode() + "): " + se.getMessage());
} catch (UnsupportedEncodingException usee) {
// just log the message and continue, for now
Debug.error(tableName + ":DBHandler:logRecords:execUpdate:cTable:UE: " + usee.getMessage());
}
try {
insertStatement = getInsertPreparedStatement(values);
insertStatement.executeUpdate();
} catch (SQLException sqle2) {
// guess NOW it's an error
Debug.error(tableName + ":DBHandler:flush:logRecords:exUpdate:SQLE (" + sqle2.getErrorCode() + "): " + sqle2.getMessage());
throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
} finally {
closeStatement(insertStatement);
insertStatement = null;
}
} else if ((isMySQL && (sqleErrCode == 0)) || (!isMySQL && ((sqleErrCode == 17002) || (sqleErrCode == 17410)))) {
try {
conn.close();
} catch (SQLException ex) {
// log and continue
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:execUpdate:close:SQLE (" + ex.getErrorCode() + "): " + ex.getMessage());
}
}
connectionToDBLost = true;
try {
reconnectToDatabase();
Debug.error(tableName + ":DBHandler:logRecords:execUpdate:reconnect successful.");
} catch (DriverLoadException dle) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:execUpdate:reconnect:DLE: " + dle.getMessage());
}
/*
* if the max mem buffer is exceeded,
* dump the records
*/
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_RECONNECT_FAILED);
} catch (ConnectionException ce) {
if (Debug.messageEnabled()) {
Debug.message(tableName + ":DBHandler:logRecords:execUpdate:reconnect:CE: " + ce.getMessage());
}
/*
* if the max mem buffer is exceeded,
* dump the records
*/
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_RECONNECT_FAILED);
} finally {
closeStatement(insertStatement);
insertStatement = null;
}
connectionToDBLost = false;
/*
* bunch the createTable, createStatement, and
* executeUpdate together because if any of these
* fail, throw an exception.
*/
try {
createTable(tableName);
insertStatement = getInsertPreparedStatement(values);
insertStatement.executeUpdate();
//Monit start
if (MonitoringUtil.isRunning() && dbLogHandlerForMonitoring != null) {
dbLogHandlerForMonitoring.incHandlerSuccessCount(1);
}
//Monit end
} catch (SQLException sqe) {
Debug.error(tableName + ":DBHandler:logRecords:executeUpd:reconnect:stmt:SQE: (" + sqe.getErrorCode() + "): " + sqe.getMessage());
/*
* if the max mem buffer is exceeded,
* dump the records
*/
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
} catch (UnsupportedEncodingException usee) {
Debug.error(tableName + ":DBHandler:logRecords:execUpd:reconnect:stmt:UE: " + usee.getMessage());
/*
* if the max mem buffer is exceeded,
* dump the records
*/
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
} finally {
closeStatement(insertStatement);
insertStatement = null;
}
} else {
/*
* not sure what to do here yet. log the error,
* throw an exception, and see what happens next.
*
* just for informational purposes, you get the
* following if the columns don't exist:
* if ((isMySQL && (sqleErrCode == 1054)) ||
* (!isMySQL && ((sqleErrCode == 904) ||
* (sqleErrCode == 913))))
*/
Debug.error(tableName + ":DBHandler:flush:executeUpdate failed (" + sqleErrCode + "): " + sqle.getMessage());
// if the max mem buffer is exceeded, dump the
// records
clearBuffer(records);
throw new AMLogException(AMLogException.LOG_DB_EXECUPDATE);
}
} finally {
closeStatement(insertStatement);
insertStatement = null;
}
}
}
use of com.sun.identity.log.AMLogException in project OpenAM by OpenRock.
the class RemoteHandler method flush.
/**
* Flush any buffered output.
*/
public synchronized void flush() {
if (recCount <= 0) {
if (Debug.messageEnabled()) {
Debug.message("RemoteHandler.flush(): no records " + "in buffer to send");
}
return;
}
Vector responses = new Vector();
if (Debug.messageEnabled()) {
Debug.message("RemoteHandler.flush(): sending buffered records");
}
String thisAMException = null;
try {
Iterator sidIter = reqSetMap.keySet().iterator();
while (sidIter.hasNext()) {
String currentLoggedBySID = (String) sidIter.next();
URL logHostURL = getLogHostURL(currentLoggedBySID);
if (logHostURL == null) {
Debug.error("RemoteHandler.flush(): logHostURL is null");
this.recCount = 0;
reqSetMap = new HashMap();
return;
}
RequestSet reqSet = (RequestSet) reqSetMap.get(currentLoggedBySID);
responses = PLLClient.send(logHostURL, reqSet);
Iterator respIter = responses.iterator();
while (respIter.hasNext()) {
Response resp = (Response) respIter.next();
String respContent = resp.getContent();
if (!respContent.equals("OK")) {
Debug.error("RemoteHandler.flush(): " + respContent + " on remote machine");
if (thisAMException == null) {
thisAMException = "RemoteHandler.flush(): " + respContent + " on remote machine";
}
}
}
}
} catch (Exception e) {
Debug.error("RemoteHandler.flush(): ", e);
}
this.recCount = 0;
reqSetMap = new HashMap();
if (thisAMException != null) {
throw new AMLogException(thisAMException);
}
}
use of com.sun.identity.log.AMLogException in project OpenAM by OpenRock.
the class LogSample method logWriteProcessing.
private void logWriteProcessing() {
/*
* get:
* 1. subject userid (subject of the LogRecord)
* 2. subject userid's password
* 3. Log filename to log to
* 4. LogRecord's "data"
* 5. LoggedBy userid (who's doing the logging)
* 6. LoggedBy userid's password
* 7. Realm (for both subject userid and LoggedBy userid
* in this sample)
*/
String userSID = sampleUtils.getLine("Subject Userid", DEF_USERNAME);
String userPWD = sampleUtils.getLine("Subject Userid " + userSID + "'s password", DEF_USERPSWD);
String logName = sampleUtils.getLine("Log file", DEF_LOGNAME);
String message = sampleUtils.getLine("Log message", DEF_LOGMSG);
;
String loggedBySID = sampleUtils.getLine("LoggedBy Userid", DEF_LOGGEDBY);
String loggedByPWD = sampleUtils.getLine("LoggedBy Userid's password", DEF_LOGGEDBYPSWD);
String realmName = sampleUtils.getLine("Realm", DEF_REALM);
// get AuthContexts for subject userid and loggedby userid
try {
userAC = new AuthContext(realmName);
loggerAC = new AuthContext(realmName);
} catch (AuthLoginException le) {
System.err.println("LogSampleUtils: could not get AuthContext for realm " + realmName);
System.exit(2);
}
// do user and loggedby login and get the SSOToken
try {
userSSOToken = sampleUtils.realmLogin(userSID, userPWD, userAC);
loggerSSOToken = sampleUtils.realmLogin(loggedBySID, loggedByPWD, loggerAC);
} catch (SSOException ssoe) {
System.err.println("logWriteProcessing: could not get SSOToken: " + ssoe.getMessage());
System.exit(3);
} catch (AuthLoginException ale) {
System.err.println("logWriteProcessing: could not authenticate: " + ale.getMessage());
System.exit(4);
} catch (Exception e) {
System.err.println("logWriteProcessing: exception getting SSOToken: " + e.getMessage());
System.exit(5);
}
try {
LogRecord logRecord = new LogRecord(java.util.logging.Level.INFO, message, userSSOToken);
logRecord.addLogInfo("ModuleName", DEF_MODULENAME);
java.net.InetAddress ipAddr = java.net.InetAddress.getLocalHost();
logRecord.addLogInfo("IPAddr", ipAddr.getHostAddress());
Logger logger = (Logger) Logger.getLogger(logName);
logger.log(logRecord, loggerSSOToken);
System.out.println("LogSample: Logging Successful !!!");
userAC.logout();
loggerAC.logout();
} catch (AMLogException amex) {
System.err.println("LogSample: AMLogException: " + amex.getMessage());
System.err.println("LogSample: Logging Failed; " + "Is user '" + loggedBySID + "' a member of a Role or Group with log writing privileges?");
} catch (Exception ssoe) {
System.err.println("LogSample: Exception: " + ssoe.getMessage());
System.err.println("LogSample: Logging Failed !!!");
}
}
Aggregations