DBFormatter.java revision 86edd2209e1a3e78ae8c0fb3175d8a48425e291a
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: DBFormatter.java,v 1.11 2009/08/19 21:12:50 ww203982 Exp $
*
*/
/*
* Portions Copyrighted 2011 ForgeRock Inc
* Portions Copyrighted 2012 Open Source Solution Technology Corporation
*/
/**
* This Formatter provides support for formatting LogRecords that will help
* Database Logging.
* <p>
* Typically this Formatter will be associated with a DBHandler(a handler meant
* to handle Database logging). <tt> DBFormatter </TT> takes a LogRecord and
* converts it to a Formatted string which DBHandler can understand.
*
*/
public class DBFormatter extends Formatter {
private boolean isMySQL = false;
/** max length of literal for Oracle */
private static final int MAX_LITERAL_LENGTH = 4000;
/**
* Creates <code>DBFormatter</code> object
*/
public DBFormatter() {
try {
} catch (ClassNotFoundException cnfe) {
"not found", cnfe);
} catch (InstantiationException ie) {
"be Instantiated", ie);
} catch (IllegalAccessException iae) {
"be Instantiated", iae);
}
} else {
isMySQL = false;
isMySQL = true;
} else {
isMySQL = false;
"' is Oracle-compatible.");
}
}
}
/**
* Returns the set of all fields converted into a COMMA seperated
* string. A typical sql query for logging a record looks like this. <p>
* insert into table "amSSO_access" (time, data, loginid, domain, level,
* ipAddress, hostname) values('10:10:10', '10th June, 2002',
* ..., ..., ...)<p>
* The getHead method returns back the set of all fields converted into a
* COMMA seperated string. It is the duty of the formatter to fetch the all
* field set from the LogManager and convert into a COMMA seperated string.
* By doing this the handler can be kept independent of the all field and
* selected field set.
*
* @param h The target handler (can be null)
* @return the set of all fields converted into a COMMA seperated string.
*/
if (Debug.messageEnabled()) {
+ retString);
}
return retString;
}
/**
* Returns a null string whenever called.
* @param h The target handler (can be null)
* @return a null string whenever called.
*/
return "";
}
/**
* Format the given LogRecord and return back a formatted String.
* <p>
* The formatted String has the values of the fields which are selected and
* NULL if any field is not selected. All fields are enclosed in single-
* quotes.
* <p>
* A typical formatted string can be given as follows:
* '10:10:10', '10th June, 2002', 'NULL', 'NULL',
* 'Session Created Successfull', 'INFO', 'NULL', 'NULL'
* <p>
* This formatted string will be enclosed within braces by Handler to
* construct the query string.
*
* @param logRecord the log record to be formatted.
* @return formatted string.
*/
if ((LogManagerUtil.isAMLoggingMode()) &&
.getLogInfoMap();
}
if(secureTimestampGenerator != null) {
} else {
strTime ="";
}
/*
* the "yyyy-mm-dd hh:mn:ss" format (24hr). if it changes
* then there'll need to be a change to the dbdate-format
* attribute.
*/
if (!isMySQL) {
toDate = "TO_DATE('";
} else {
toDate = "STR_TO_DATE('";
}
/* Need to check for single-quote in the DATA field to be written
* to the db
*/
}
if (isMySQL) {
// MySQL has a problem with backslash
}
} else {
// Currently MySQL and Oracle are supported. If isMySQL is not true, we assume it is Oracle.
// Split string data since Oracle only accept string literal less than 4000 bytes.
int beginIndex = 0;
int endIndex = splitLength;
}
}
}
}
}
}
if (Debug.messageEnabled()) {
+ "#");
}
int len = 0;
}
if ((logInfoTable != null) &&
(selectedFields != null) &&
//
// if there are any single-quotes in the data, they have to be
// made double-single-quotes, so it'll pass through sql
//
) {
if (Debug.messageEnabled()) {
+ tempstr);
}
//
// funky case of "'" at the beginning
//
if (tmps.hasMoreTokens()) {
}
} else {
if (tmps.hasMoreTokens()) {
}
}
while (tmps.hasMoreTokens()) {
}
//
// if string ends in "'"
//
}
}
}
} else {
}
}
if (Debug.messageEnabled()) {
}
}
} else {
}
if (Debug.messageEnabled()) {
}
}
{
if (Debug.messageEnabled()) {
" in data: " + theString);
}
/*
* Weird cases of char at beginning or end of the data
*/
if (tmps.hasMoreTokens()) {
}
if (Debug.messageEnabled()) {
}
} else {
if (tmps.hasMoreTokens()) {
}
}
while (tmps.hasMoreTokens()) {
if (Debug.messageEnabled()) {
}
}
/*
* See if it ends in "'"
*/
}
}
}