1 16 17 package org.pentaho.core.audit; 18 19 import java.io.BufferedWriter ; 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.math.BigDecimal ; 24 import java.text.DecimalFormat ; 25 import java.text.SimpleDateFormat ; 26 import java.util.Date ; 27 28 import org.pentaho.core.system.PentahoSystem; 29 import org.pentaho.messages.Messages; 30 import org.pentaho.util.logging.Logger; 31 32 38 public class AuditFileEntry implements IAuditEntry { 39 private static String auditDirPath = "system/logs/audit"; 41 private static String auditFileName = PentahoSystem.getSystemSetting("audit/auditLogFile", "/PentahoAuditLog.log"); 43 private static File auditFile = null; 44 45 private static String ID_SEPARATOR = PentahoSystem.getSystemSetting("audit/id_separator", "\t"); 47 private static final SimpleDateFormat auditDateFormat = new SimpleDateFormat (PentahoSystem.getSystemSetting("audit/auditDateFormat", "yyyy/MM/dd k:mm:ss")); 49 public AuditFileEntry() { 50 File auditDir = new File (PentahoSystem.getApplicationContext().getFileOutputPath(auditDirPath)); 51 if (!auditDir.exists()) { 52 auditDir.mkdirs(); 53 } else if (!auditDir.isDirectory()) { 54 Logger.error(this, Messages.getErrorString("AUDFILEENT.ERROR_0001_AUDIT_PATH_NOT_DIRECTORY", auditDirPath)); return; 56 } 57 auditFile = new File (auditDir, auditFileName); 58 if ("\\t".equals(ID_SEPARATOR)) { ID_SEPARATOR = "\t"; } 61 } 62 63 public synchronized void auditAll(String jobId, String instId, String objId, String objType, String actor, String messageType, String messageName, String messageTxtValue, BigDecimal messageNumValue, BigDecimal duration) throws AuditException { 64 65 if (auditFile == null) { 66 return; 67 } 68 try { 69 BufferedWriter fw = new BufferedWriter (new FileWriter (auditFile, true)); 70 try { 71 Date dt = new Date (); 72 fw.write(auditDateFormat.format(dt)); 73 fw.write(ID_SEPARATOR); 74 fw.write(getWritable(jobId)); 75 fw.write(ID_SEPARATOR); 76 fw.write(getWritable(instId)); 77 fw.write(ID_SEPARATOR); 78 fw.write(getWritable(objId)); 79 fw.write(ID_SEPARATOR); 80 fw.write(getWritable(objType)); 81 fw.write(ID_SEPARATOR); 82 fw.write(getWritable(actor)); 83 fw.write(ID_SEPARATOR); 84 fw.write(getWritable(messageType)); 85 fw.write(ID_SEPARATOR); 86 fw.write(getWritable(messageName)); 87 fw.write(ID_SEPARATOR); 88 fw.write(getWritable(messageTxtValue)); 89 fw.write(ID_SEPARATOR); 90 fw.write(getWritable(messageNumValue)); 91 fw.write(ID_SEPARATOR); 92 if (duration == null) { 93 fw.write('0'); 94 } else { 95 fw.write(getWritable(duration)); 96 } 97 fw.newLine(); 98 } finally { 99 fw.flush(); 100 fw.close(); 101 } 102 } catch (IOException ex) { 103 throw new AuditException(ex); 104 } 105 } 106 107 private String getWritable(Object obj) { 108 if (obj instanceof BigDecimal ) { 109 DecimalFormat format = new DecimalFormat ("#.###"); return format.format(obj); 111 } else { 112 return (obj != null) ? obj.toString() : ""; } 114 } 115 116 } 117 | Popular Tags |