001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oña, 107 1º2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataobjs;
034:
035: import java.io.FileWriter;
036: import java.io.IOException;
037: import java.io.LineNumberReader;
038: import java.io.FileReader;
039: import java.io.BufferedReader;
040:
041: import java.sql.PreparedStatement;
042: import java.sql.SQLException;
043: import java.sql.Timestamp;
044: import java.sql.Types;
045:
046: import java.util.TreeMap;
047: import java.util.Iterator;
048:
049: import com.knowgate.jdc.JDCConnection;
050: import com.knowgate.misc.Gadgets;
051:
052: /**
053: * Keeps an operations log at a file or database table.
054: * @author Sergio Montoro ten
055: * @version 3.0
056: */
057:
058: public class DBAudit {
059:
060: protected void finalize() throws IOException {
061: if (sAuditFile != "")
062: oLogWriter.close();
063: }
064:
065: // ----------------------------------------------------------
066:
067: /**
068: * Write a log entry into k_auditing database table
069: * @param oConn Database connection, if null then data if dumped to a flat file.
070: * @param iIdEntity - Internal ClassId short nombre for audited class.
071: * @param sCoOp - Operation Code (4 alphanumeric digits)
072: * @param sGUUser - GUID of user performing the operation (máx. 32 characters)
073: * @param sGUEntity1 - GUID of primary entity (or source entitity) for the operation (máx. 32 characters)
074: * @param sGUEntity2 - GUID of secondary entity (or target entitity) for the operation (máx. 32 characters)
075: * @param iIdTransact - Transaction Identifier
076: * @param iIPAddr - User IP address
077: * @param sTxParams1 - Aditional parameters related to entity 1 (máx 255 characters)
078: * @param sTxParams2 - Aditional parameters related to entity 2 (máx 255 characters)
079: * @throws SQLException
080: * @throws SecurityException
081: */
082:
083: public static void log(JDCConnection oConn, short iIdEntity,
084: String sCoOp, String sGUUser, String sGUEntity1,
085: String sGUEntity2, int iIdTransact, int iIPAddr,
086: String sTxParams1, String sTxParams2) throws SQLException {
087: PreparedStatement oStmt;
088:
089: if (sCoOp == null)
090: throw new SQLException(
091: "DBAudit.log() operation code cannot be null",
092: "23000", 23000);
093: if (sCoOp.length() > 4)
094: throw new SQLException("DBAudit.log() operation code "
095: + sCoOp + " cannot be longer than 4 characters",
096: "01004", 1004);
097: if (sGUUser == null)
098: throw new SQLException(
099: "DBAudit.log() user GUID cannot be null", "23000",
100: 23000);
101: if (sGUUser.length() > 32)
102: throw new SQLException(
103: "DBAudit.log() user GUID cannot be longer than 32 characters",
104: "23000", 23000);
105: if (sGUEntity1 == null)
106: throw new SQLException(
107: "DBAudit.log() user entity GUID cannot be null",
108: "23000", 23000);
109: if (sGUEntity1.length() > 32)
110: throw new SQLException(
111: "DBAudit.log() entity GUID cannot be longer than 32 characters",
112: "23000", 23000);
113:
114: if (null == oConn) {
115: writeLog(iIdEntity, sCoOp, sGUUser, sGUEntity1, sGUEntity2,
116: iIdTransact, String.valueOf(iIPAddr), sTxParams1,
117: sTxParams2);
118: } else {
119: oStmt = oConn
120: .prepareStatement("INSERT INTO k_auditing VALUES (?,?,?,?,?,?,?,?,?,?)");
121: oStmt.setShort(1, iIdEntity);
122: oStmt.setString(2, sCoOp);
123: oStmt.setString(3, sGUUser);
124: oStmt.setTimestamp(4, new Timestamp(new java.util.Date()
125: .getTime()));
126: oStmt.setString(5, sGUEntity1);
127: if (null == sGUEntity2)
128: oStmt.setNull(6, Types.VARCHAR);
129: else
130: oStmt.setString(6, sGUEntity2);
131: oStmt.setInt(7, iIdTransact);
132: oStmt.setInt(8, iIPAddr);
133: if (null == sTxParams1)
134: oStmt.setNull(9, Types.VARCHAR);
135: else
136: oStmt.setString(9, Gadgets.left(sTxParams1, 100));
137: if (null == sTxParams2)
138: oStmt.setNull(10, Types.VARCHAR);
139: else
140: oStmt.setString(10, Gadgets.left(sTxParams2, 100));
141:
142: oStmt.execute();
143: oStmt.close();
144: } // fi (oConn)
145: } // log()
146:
147: // ----------------------------------------------------------
148:
149: /**
150: * Write a log entry into javatrx.txt file
151: * @param iIdEntity - Internal ClassId short nombre for audited class.
152: * @param sCoOp - Operation Code (4 alphanumeric digits)
153: * @param sGUUser - GUID of user performing the operation (máx. 32 characters)
154: * @param sGUEntity1 - GUID of primary entity (or source entitity) for the operation (máx. 32 characters)
155: * @param sGUEntity2 - GUID of secondary entity (or target entitity) for the operation (máx. 32 characters)
156: * @param iIdTransact - Transaction Identifier
157: * @param iIPAddr - User IP address
158: * @param sTxParams1 - Aditional parameters related to entity 1 (máx 255 characters)
159: * @param sTxParams2 - Aditional parameters related to entity 2 (máx 255 characters)
160: * @throws SecurityException if there aren't sufficient permissions for writting at javatrc.txt file
161: */
162: public static void log(short iIdEntity, String sCoOp,
163: String sGUUser, String sGUEntity1, String sGUEntity2,
164: int iIdTransact, String sIPAddr, String sTxParams1,
165: String sTxParams2) {
166:
167: writeLog(iIdEntity, sCoOp, sGUUser, sGUEntity1, sGUEntity2,
168: iIdTransact, sIPAddr, sTxParams1, sTxParams2);
169: } // log()
170:
171: // ----------------------------------------------------------
172:
173: /**
174: * Set path to log output file
175: * @param sFilePath Physical file path
176: * @throws IOException
177: */
178: public static void setAuditFile(String sFilePath)
179: throws IOException {
180: if (sAuditFile != "")
181: oLogWriter.close();
182: sAuditFile = "";
183: oLogWriter = new FileWriter(sFilePath, true);
184: sAuditFile = sFilePath;
185: } // setAuditFile
186:
187: // ----------------------------------------------------------
188:
189: /**
190: * Write an operation line to the log file
191: * @param sTransactId Transaction Identifier
192: * @param sUserId GUID of user performing the operation (máx. 32 characters)
193: * @param sObject GUID of primary entity for the operation
194: * @param sOpCode Operation code (máx. 4 characters)
195: * @param sParams Aditional parameters related to entity 1 (máx 255 characters)
196: * @throws SecurityException if there aren't sufficient permissions for writting at javatrc.txt file
197: */
198: private static void writeLog(short iIdEntity, String sCoOp,
199: String sUserId, String sGUEntity1, String sGUEntity2,
200: int iIdTransact, String sIPAddr, String sTxParams1,
201: String sTxParams2) throws SecurityException {
202: try {
203: if (null == oLogWriter)
204: if (sOSName.startsWith("Windows"))
205: setAuditFile("C:\\javaudit.txt");
206: else
207: setAuditFile("/tmp/javaudit.txt");
208:
209: oLogWriter.write(new java.util.Date().toString() + ";"
210: + String.valueOf(iIdEntity) + ";" + sCoOp + ";"
211: + sUserId + ";" + sGUEntity1 + ";" + sGUEntity2
212: + ";" + String.valueOf(iIdTransact) + ";" + sIPAddr
213: + ";" + sTxParams1 + ";" + sTxParams2 + "\n");
214:
215: } catch (IOException ioe) {
216: } catch (NullPointerException npe) {
217: }
218:
219: } // writeLog()
220:
221: // ----------------------------------------------------------
222:
223: public static String analyze(String sFile) throws IOException {
224:
225: int f, s, e;
226: StringBuffer oReport = new StringBuffer(4096);
227: String sLine, sOpCode = null, sEntity = null;
228:
229: TreeMap oOpen = new TreeMap();
230: Integer oCount;
231:
232: LineNumberReader lnr;
233:
234: FileReader oReader = new FileReader(sFile);
235: BufferedReader oBuffer = new BufferedReader(oReader);
236: LineNumberReader oLines = new LineNumberReader(oBuffer);
237:
238: while ((sLine = oLines.readLine()) != null) {
239: f = 0;
240: s = -1;
241: while ((s = sLine.indexOf(';', s + 1)) != -1) {
242: f++;
243: switch (f) {
244: case 2:
245: sOpCode = sLine.substring(s, sLine.indexOf(';',
246: s + 1));
247: break;
248: case 4:
249: sEntity = sLine.substring(s, sLine.indexOf(';',
250: s + 1));
251:
252: if (sOpCode.equals("ODBC")
253: || sOpCode.equals("OJSP")) {
254: oCount = (Integer) oOpen.get(sEntity);
255: if (oCount == null) {
256: oCount = new Integer(1);
257: oOpen.put(sEntity, oCount);
258: } else {
259: oCount = new Integer(oCount.intValue() + 1);
260: oOpen.remove(sEntity);
261: oOpen.put(sEntity, oCount);
262: }
263: } // fi (sOpCode==ODBC)
264: else if (sOpCode.equals("CDBC")
265: || sOpCode.equals("CJSP")) {
266: oCount = (Integer) oOpen.get(sEntity);
267: if (oCount == null) {
268: oCount = new Integer(-1);
269: oOpen.put(sEntity, oCount);
270: } else {
271: oCount = new Integer(oCount.intValue() - 1);
272: oOpen.remove(sEntity);
273: oOpen.put(sEntity, oCount);
274: }
275: }
276: break;
277: } // switch(f)
278: } // wend
279: if (f % 10 == 0)
280: System.out.print('.');
281: } // wend
282:
283: System.out.print("\n");
284:
285: oReader.close();
286: oLines.close();
287: oBuffer.close();
288:
289: Iterator oKeys = oOpen.keySet().iterator();
290:
291: while (oKeys.hasNext()) {
292: sEntity = (String) oKeys.next();
293: oCount = (Integer) oOpen.get(sEntity);
294:
295: if (oCount.intValue() != 0) {
296: oReport.append(sEntity + " open/close mismatch "
297: + oCount.toString() + "\n");
298: }
299: } // wend
300:
301: return oReport.toString();
302: }
303:
304: // ----------------------------------------------------------
305:
306: private static void printUsage() {
307: System.out.println("");
308: System.out.println("Usage:\n");
309: System.out.println("DBAudit analyze <file_path>");
310: }
311:
312: public static void main(String[] argv) throws IOException {
313:
314: if (argv.length < 2)
315: printUsage();
316: else if (!argv[0].equals("analyze"))
317: printUsage();
318: else
319: System.out.print(DBAudit.analyze(argv[1]));
320:
321: }
322:
323: // ----------------------------------------------------------
324:
325: private static String sOSName = System.getProperty("os.name");
326:
327: private static String sAuditFile = "";
328:
329: private static FileWriter oLogWriter = null;
330:
331: } // DBAudit
|