KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > audit > AuditFileEntry


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 24, 2005
14  * @author Marc Batchelor
15  */

16
17 package org.pentaho.core.audit;
18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.text.DecimalFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import org.pentaho.core.system.PentahoSystem;
29 import org.pentaho.messages.Messages;
30 import org.pentaho.util.logging.Logger;
31
32 /**
33  * @author mbatchel
34  *
35  * TODO To change the template for this generated type comment go to Window -
36  * Preferences - Java - Code Style - Code Templates
37  */

38 public class AuditFileEntry implements IAuditEntry {
39     private static String JavaDoc auditDirPath = "system/logs/audit"; //$NON-NLS-1$
40

41     private static String JavaDoc auditFileName = PentahoSystem.getSystemSetting("audit/auditLogFile", "/PentahoAuditLog.log"); //$NON-NLS-1$ //$NON-NLS-2$
42

43     private static File JavaDoc auditFile = null;
44
45     private static String JavaDoc ID_SEPARATOR = PentahoSystem.getSystemSetting("audit/id_separator", "\t"); //$NON-NLS-1$ //$NON-NLS-2$
46

47     private static final SimpleDateFormat JavaDoc auditDateFormat = new SimpleDateFormat JavaDoc(PentahoSystem.getSystemSetting("audit/auditDateFormat", "yyyy/MM/dd k:mm:ss")); //$NON-NLS-1$ //$NON-NLS-2$
48

49     public AuditFileEntry() {
50         File JavaDoc auditDir = new File JavaDoc(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)); //$NON-NLS-1$
55
return;
56         }
57         auditFile = new File JavaDoc(auditDir, auditFileName);
58         if ("\\t".equals(ID_SEPARATOR)) { //$NON-NLS-1$
59
ID_SEPARATOR = "\t"; //$NON-NLS-1$
60
}
61     }
62
63     public synchronized void auditAll(String JavaDoc jobId, String JavaDoc instId, String JavaDoc objId, String JavaDoc objType, String JavaDoc actor, String JavaDoc messageType, String JavaDoc messageName, String JavaDoc messageTxtValue, BigDecimal JavaDoc messageNumValue, BigDecimal JavaDoc duration) throws AuditException {
64
65         if (auditFile == null) {
66             return;
67         }
68         try {
69             BufferedWriter JavaDoc fw = new BufferedWriter JavaDoc(new FileWriter JavaDoc(auditFile, true));
70             try {
71                 Date JavaDoc dt = new Date JavaDoc();
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 JavaDoc ex) {
103             throw new AuditException(ex);
104         }
105     }
106
107     private String JavaDoc getWritable(Object JavaDoc obj) {
108         if (obj instanceof BigDecimal JavaDoc) {
109             DecimalFormat JavaDoc format = new DecimalFormat JavaDoc("#.###"); //$NON-NLS-1$
110
return format.format(obj);
111         } else {
112             return (obj != null) ? obj.toString() : ""; //$NON-NLS-1$
113
}
114     }
115
116 }
117
Popular Tags