1 32 33 package com.knowgate.dataobjs; 34 35 import java.io.FileWriter ; 36 import java.io.IOException ; 37 import java.io.LineNumberReader ; 38 import java.io.FileReader ; 39 import java.io.BufferedReader ; 40 41 import java.sql.PreparedStatement ; 42 import java.sql.SQLException ; 43 import java.sql.Timestamp ; 44 import java.sql.Types ; 45 46 import java.util.TreeMap ; 47 import java.util.Iterator ; 48 49 import com.knowgate.jdc.JDCConnection; 50 import com.knowgate.misc.Gadgets; 51 52 57 58 public class DBAudit { 59 60 protected void finalize() throws IOException { 61 if (sAuditFile!="") oLogWriter.close(); 62 } 63 64 66 81 82 public static void log(JDCConnection oConn, short iIdEntity, String sCoOp, String sGUUser, String sGUEntity1, String sGUEntity2, int iIdTransact, int iIPAddr, String sTxParams1, String sTxParams2) 83 throws SQLException { 84 PreparedStatement oStmt; 85 86 if (sCoOp==null) throw new SQLException ("DBAudit.log() operation code cannot be null", "23000", 23000); 87 if (sCoOp.length()>4) throw new SQLException ("DBAudit.log() operation code " + sCoOp + " cannot be longer than 4 characters", "01004", 1004); 88 if (sGUUser==null) throw new SQLException ("DBAudit.log() user GUID cannot be null", "23000", 23000); 89 if (sGUUser.length()>32) throw new SQLException ("DBAudit.log() user GUID cannot be longer than 32 characters", "23000", 23000); 90 if (sGUEntity1==null) throw new SQLException ("DBAudit.log() user entity GUID cannot be null", "23000", 23000); 91 if (sGUEntity1.length()>32) throw new SQLException ("DBAudit.log() entity GUID cannot be longer than 32 characters", "23000", 23000); 92 93 if (null==oConn) { 94 writeLog (iIdEntity, sCoOp, sGUUser, sGUEntity1, sGUEntity2, iIdTransact, String.valueOf(iIPAddr), sTxParams1, sTxParams2); 95 } 96 else { 97 oStmt = oConn.prepareStatement("INSERT INTO k_auditing VALUES (?,?,?,?,?,?,?,?,?,?)"); 98 oStmt.setShort (1, iIdEntity); 99 oStmt.setString (2, sCoOp); 100 oStmt.setString (3, sGUUser); 101 oStmt.setTimestamp(4, new Timestamp (new java.util.Date ().getTime())); 102 oStmt.setString (5, sGUEntity1); 103 if (null==sGUEntity2) 104 oStmt.setNull (6, Types.VARCHAR); 105 else 106 oStmt.setString (6, sGUEntity2); 107 oStmt.setInt (7, iIdTransact); 108 oStmt.setInt (8, iIPAddr); 109 if (null==sTxParams1) 110 oStmt.setNull (9, Types.VARCHAR); 111 else 112 oStmt.setString (9, Gadgets.left(sTxParams1,100)); 113 if (null==sTxParams2) 114 oStmt.setNull (10, Types.VARCHAR); 115 else 116 oStmt.setString (10, Gadgets.left(sTxParams2, 100)); 117 118 oStmt.execute(); 119 oStmt.close(); 120 } } 123 125 138 public static void log (short iIdEntity, String sCoOp, String sGUUser, String sGUEntity1, String sGUEntity2, int iIdTransact, String sIPAddr, String sTxParams1, String sTxParams2) { 139 140 writeLog (iIdEntity, sCoOp, sGUUser, sGUEntity1, sGUEntity2, iIdTransact, sIPAddr, sTxParams1, sTxParams2); 141 } 143 145 150 public static void setAuditFile(String sFilePath) throws IOException { 151 if (sAuditFile!="") oLogWriter.close(); 152 sAuditFile = ""; 153 oLogWriter = new FileWriter (sFilePath, true); 154 sAuditFile = sFilePath; 155 } 157 159 168 private static void writeLog (short iIdEntity, String sCoOp, String sUserId, String sGUEntity1, String sGUEntity2, int iIdTransact, String sIPAddr, String sTxParams1, String sTxParams2) 169 throws SecurityException { 170 try { 171 if (null==oLogWriter) 172 if (sOSName.startsWith("Windows")) 173 setAuditFile("C:\\javaudit.txt"); 174 else 175 setAuditFile("/tmp/javaudit.txt"); 176 177 oLogWriter.write (new java.util.Date ().toString() + ";" + String.valueOf(iIdEntity) + ";" + sCoOp + ";" + sUserId + ";" + sGUEntity1 + ";" + sGUEntity2 + ";" + String.valueOf(iIdTransact) + ";" + sIPAddr + ";" + sTxParams1 + ";" + sTxParams2 + "\n"); 178 179 } 180 catch (IOException ioe) { } 181 catch (NullPointerException npe) {} 182 183 } 185 187 public static String analyze(String sFile) throws IOException { 188 189 int f, s, e; 190 StringBuffer oReport = new StringBuffer (4096); 191 String sLine, sOpCode = null, sEntity = null; 192 193 TreeMap oOpen = new TreeMap (); 194 Integer oCount; 195 196 LineNumberReader lnr; 197 198 FileReader oReader = new FileReader (sFile); 199 BufferedReader oBuffer = new BufferedReader (oReader); 200 LineNumberReader oLines = new LineNumberReader (oBuffer); 201 202 while ((sLine = oLines.readLine())!=null) { 203 f = 0; 204 s = -1; 205 while ((s=sLine.indexOf(';',s+1))!=-1) { 206 f++; 207 switch (f) { 208 case 2: 209 sOpCode = sLine.substring(s, sLine.indexOf(';', s+1)); 210 break; 211 case 4: 212 sEntity = sLine.substring(s, sLine.indexOf(';', s+1)); 213 214 if (sOpCode.equals("ODBC") || sOpCode.equals("OJSP")) { 215 oCount = (Integer ) oOpen.get(sEntity); 216 if (oCount==null) { 217 oCount = new Integer (1); 218 oOpen.put(sEntity, oCount); 219 } 220 else { 221 oCount = new Integer (oCount.intValue()+1); 222 oOpen.remove(sEntity); 223 oOpen.put(sEntity, oCount); 224 } 225 } else if (sOpCode.equals("CDBC") || sOpCode.equals("CJSP")) { 227 oCount = (Integer ) oOpen.get(sEntity); 228 if (oCount==null) { 229 oCount = new Integer (-1); 230 oOpen.put(sEntity, oCount); 231 } 232 else { 233 oCount = new Integer (oCount.intValue()-1); 234 oOpen.remove(sEntity); 235 oOpen.put(sEntity, oCount); 236 } 237 } 238 break; 239 } } if (f%10==0) System.out.print('.'); 242 } 244 System.out.print("\n"); 245 246 oReader.close(); 247 oLines.close(); 248 oBuffer.close(); 249 250 Iterator oKeys = oOpen.keySet().iterator(); 251 252 while (oKeys.hasNext()) { 253 sEntity = (String ) oKeys.next(); 254 oCount = (Integer ) oOpen.get(sEntity); 255 256 if (oCount.intValue()!=0) { 257 oReport.append(sEntity + " open/close mismatch " + oCount.toString() + "\n"); 258 } 259 } 261 return oReport.toString(); 262 } 263 264 266 private static void printUsage() { 267 System.out.println(""); 268 System.out.println("Usage:\n"); 269 System.out.println("DBAudit analyze <file_path>"); 270 } 271 272 public static void main(String [] argv) throws IOException { 273 274 if (argv.length<2) 275 printUsage(); 276 else if (!argv[0].equals("analyze")) 277 printUsage(); 278 else 279 System.out.print(DBAudit.analyze(argv[1])); 280 281 } 282 284 private static String sOSName = System.getProperty("os.name"); 285 286 private static String sAuditFile = ""; 287 288 private static FileWriter oLogWriter = null; 289 290 }
| Popular Tags
|