001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.ubt;
006:
007: import com.sun.portal.ubt.UBTLogRecord;
008: import com.sun.portal.ubt.UBTLogManager;
009:
010: import java.util.logging.*;
011: import java.util.Hashtable;
012: import java.util.Vector;
013: import java.sql.*;
014:
015: /**
016: * Sample JDBC Handler for UBT.
017: * This handler expects a table 'ubt' to be present in the database.
018: * The table column names need to match with the name of the UBT log fields.
019: */
020: public class UBTJDBCHandler extends Handler {
021: /**
022: * Key name to provide JDBC driver name in the UBT Configuration.
023: */
024: public static String DRIVER_PROPERTY_KEY = "jdbc.driver";
025: /**
026: * Key name to provide connection URL as part of UBT Configuration.
027: */
028: public static String CONNECTION_PROPERTY_KEY = "jdbc.connection";
029: /**
030: * A string that contains the classname of the JDBC driver.
031: * This value is filled by the constructor.
032: */
033: String driverString;
034:
035: /**
036: * A string that contains the connection string used by the
037: * JDBC driver. This value is filled by the constructor.
038: */
039: String connectionString;
040:
041: /**
042: * Used to hold the connection to the JDBC data source.
043: */
044: Connection connection;
045:
046: /**
047: * A SQL statement used to insert into the log table.
048: */
049: String insertSQL = "insert into ubt (";
050: String insertSQLSuffix1 = ") values (";
051: String insertSQLSuffix2 = "";
052: String insertSQLSuffix3 = ")";
053: /**
054: * A SQL statement used to clear the log table.
055: */
056: String clearSQL = "delete from ubt;";
057:
058: /**
059: * A PreparedStatement object used to hold the main
060: * insert statement.
061: */
062: protected PreparedStatement prepInsert;
063:
064: /**
065: * A PreparedStatement object used to hold the clear
066: * statement.
067: */
068: protected PreparedStatement prepClear;
069:
070: /**
071: * Configures the handler by getting configuration of the database to use.
072: */
073: public UBTJDBCHandler() {
074: try {
075: UBTLogManager ubtLogManager = UBTLogManager.getInstance();
076: this .driverString = ubtLogManager
077: .getProperty(UBTJDBCHandler.DRIVER_PROPERTY_KEY);
078: this .connectionString = ubtLogManager
079: .getProperty(UBTJDBCHandler.CONNECTION_PROPERTY_KEY);
080: Class.forName(driverString);
081: connection = DriverManager.getConnection(connectionString);
082: Vector fields = ubtLogManager.getLogFields(Level.FINEST);
083: for (int i = 0; i < fields.size(); i++) {
084: insertSQL += fields.get(i).toString();
085: insertSQLSuffix2 += "?";
086: if (i != fields.size() - 1) {
087: insertSQL += ",";
088: insertSQLSuffix2 += ",";
089: }
090: }
091: insertSQL = insertSQL + insertSQLSuffix1 + insertSQLSuffix2
092: + insertSQLSuffix3;
093: prepInsert = connection.prepareStatement(insertSQL);
094: prepClear = connection.prepareStatement(clearSQL);
095: } catch (ClassNotFoundException e) {
096: System.err.println("Error on open: " + e);
097: } catch (SQLException e) {
098: System.err.println("Error on open: " + e);
099: }
100: }
101:
102: /**
103: * Internal method used to truncate a string to a specified width.
104: * Used to ensure that SQL table widths are not exceeded.
105: *
106: * @param str The string to be truncated.
107: * @param length The maximum length of the string.
108: * @return The string truncated.
109: */
110: private String truncate(String str, int length) {
111: if (str.length() < length)
112: return str;
113: return (str.substring(0, length));
114: }
115:
116: /**
117: * Overridden method used to capture log entries and put them
118: * into a JDBC database.
119: *
120: * @param record The log record to be stored.
121: */
122: public synchronized void publish(LogRecord record) {
123: // first see if this entry should be filtered out
124: if (getFilter() != null) {
125: if (!getFilter().isLoggable(record))
126: return;
127: }
128: Vector fields = UBTLogManager.getInstance().getLogFields(
129: Level.FINEST);
130: UBTLogRecord rec = ((UBTLogRecord) record);
131: Hashtable logInfoTable = rec.getLogFieldTable();
132:
133: // now store the log entry into the table
134: try {
135: for (int i = 0; i < fields.size(); i++) {
136: Object val = logInfoTable.get(fields.get(i));
137: if (val == null) {
138: prepInsert.setString(i + 1, "null");
139: continue;
140: }
141: prepInsert.setString(i + 1, truncate(val.toString(),
142: 255));
143: }
144: prepInsert.executeUpdate();
145: } catch (SQLException e) {
146: System.err.println("Error on open: " + e);
147: }
148:
149: }
150:
151: /**
152: * Called to close this log handler.
153: */
154: public synchronized void close() {
155: try {
156: if (connection != null)
157: connection.close();
158: } catch (SQLException e) {
159: System.err.println("Error on close: " + e);
160: }
161: }
162:
163: /**
164: * Called to clear all log entries from the database.
165: */
166: public synchronized void clear() {
167: try {
168: prepClear.executeUpdate();
169: } catch (SQLException e) {
170: System.err.println("Error on clear: " + e);
171: }
172: }
173:
174: /**
175: * Not really used, but required to implement a handler. Since
176: * all data is immediately sent to the database, there is no
177: * reason to flush.
178: */
179: public synchronized void flush() {
180: }
181: }
|