KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
10 /**
11  *@author Andreas Mecky
12  *@author Terry Dye
13  *@created 9. März 2002
14  *@version 1.0
15  */

16 public class FileHandler extends AbstractHandler {
17     
18     private static final VariableManager vm = VariableManager.getInstance();
19     private String JavaDoc fileName = null;
20     private int maxSize = -1;
21
22     /**
23      * Constructor for the FileHandler object
24      */

25     public FileHandler(String JavaDoc configName) {
26         super(configName);
27     }
28
29
30     /**
31      *@param msg
32      */

33     public void publish(String JavaDoc msg) {
34         if ( fileName == null )
35             ErrorHandler.reportError("No filename specified");
36         try {
37             File JavaDoc file = new File JavaDoc(fileName);
38             boolean append = true;
39             if ( file.exists() && maxSize != -1 ) {
40                 long length = file.length();
41                 if ( length > maxSize*1024 ) {
42                     append = false;
43                 }
44             }
45             FileWriter JavaDoc fw = new FileWriter JavaDoc(fileName, append);
46             fw.write(msg + "\n");
47             fw.close();
48         } catch (Exception JavaDoc e) {
49             ErrorHandler.reportError("Exception while trying to write to file: " + fileName, e);
50         }
51     }
52
53
54     /**
55      * Description of the Method
56      *
57      *@param lr Description of the Parameter
58      */

59     public void publish(LogRecord lr) {
60         publish(lr.getMessage());
61     }
62
63
64     /**
65      *@param parameters
66      */

67     public void setParameter(Map JavaDoc parameters) {
68         if ( parameters.containsKey("file") ) {
69             fileName = (String JavaDoc) parameters.get("file");
70             fileName = vm.replaceVariables(fileName, getConfigurationName());
71         }
72         if ( parameters.containsKey("maxsize") )
73             maxSize = Integer.parseInt((String JavaDoc)parameters.get("maxsize"));
74     }
75 }
76
Popular Tags