KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > handler > DateFileHandler


1 package org.jzonic.jlo.handler;
2
3 import org.jzonic.jlo.LogRecord;
4 import org.jzonic.jlo.VariableManager;
5 import org.jzonic.jlo.error.ErrorHandler;
6
7 import java.io.File JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.text.SimpleDateFormat JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.Map JavaDoc;
12
13 // Referenced classes of package org.jlo.handler:
14
// Handler
15

16 public class DateFileHandler extends AbstractHandler {
17     
18     private static final VariableManager vm = VariableManager.getInstance();
19     private String JavaDoc fileName;
20     private int maxSize;
21     private String JavaDoc dateFormat;
22     
23     public DateFileHandler(String JavaDoc configName) {
24         super(configName);
25         fileName = null;
26         maxSize = -1;
27         dateFormat = "dd.MM.yyyy";
28     }
29     
30     public void publish(String JavaDoc msg) {
31         String JavaDoc fn = prepareFileName();
32         if (fileName == null)
33             ErrorHandler.reportError("No filename specified");
34         try {
35             File JavaDoc file = new File JavaDoc(fn);
36             boolean append = true;
37             if ( file.exists() && maxSize != -1 ) {
38                 long length = file.length();
39                 if ( length > maxSize*1024 ) {
40                     append = false;
41                 }
42             }
43             FileWriter JavaDoc fw = new FileWriter JavaDoc(fn, append);
44             fw.write(msg + "\n");
45             fw.close();
46         } catch (Exception JavaDoc e) {
47             ErrorHandler.reportError("Exception while trying to write to file: " + fn,e);
48         }
49     }
50     
51     public void publish(LogRecord lr) {
52         publish(lr.getMessage());
53     }
54     
55     public void setParameter(Map JavaDoc parameters) {
56         if (parameters.containsKey("file"))
57             fileName = (String JavaDoc) parameters.get("file");
58         if (parameters.containsKey("maxsize"))
59             maxSize = Integer.parseInt((String JavaDoc) parameters.get("maxsize"));
60         if (parameters.containsKey("format"))
61             dateFormat = (String JavaDoc) parameters.get("format");
62     }
63     
64     public String JavaDoc prepareFileName() {
65         if (fileName != null) {
66             String JavaDoc fn = null;
67             String JavaDoc tmpName = fileName.toLowerCase();
68             int pos = tmpName.indexOf("${date}");
69             if (pos == -1) {
70                 fn = fileName;
71             } else {
72                 Date JavaDoc rightNow = new Date JavaDoc(System.currentTimeMillis());
73                 String JavaDoc firstPart = fileName.substring(0, pos );
74                 String JavaDoc secondPart = fileName.substring(pos + 7);
75                 SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(dateFormat);
76                 String JavaDoc dateString = formatter.format(rightNow);
77                 fn = firstPart + dateString + secondPart;
78             }
79             return vm.replaceVariables(fn, getConfigurationName());
80         } else {
81             return null;
82         }
83     }
84     
85 }
Popular Tags