KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > log > LogFile


1 package com.quikj.server.log;
2
3 import com.quikj.server.framework.*;
4
5 import java.io.*;
6
7 public class LogFile
8 {
9     public LogFile(int msg_type, String JavaDoc host)
10     throws AceException
11     {
12         // get the log file name
13
msgType = msg_type;
14         myHostName = host;
15         
16         logFileName = LogConfiguration.Instance().getLogFileName(msg_type, host);
17         
18         if (openLogFile() == false)
19         {
20             throw new AceException(getErrorMessage());
21         }
22     }
23     
24     public void dispose()
25     {
26         if (closeLogFileAndArchive() == false)
27         {
28             // print error message
29
String JavaDoc message = new String JavaDoc("LogFile.LogProcessor() -- Error closing log file and archiving : "
30             + logFileName);
31             LogProcessor lp = LogProcessor.Instance();
32             if (lp == null)
33             {
34                 System.err.println(message);
35             }
36             else
37             {
38                 lp.log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
39                 message);
40             }
41         }
42     }
43     
44     public synchronized boolean write(String JavaDoc message)
45     {
46         try
47         {
48             logOutputStream.writeBytes(message + '\n');
49             logOutputStream.flush();
50         }
51         catch (IOException ex)
52         {
53             writeErrorMessage(ex.getMessage());
54             return false;
55         }
56         
57         return true;
58     }
59     
60     public synchronized boolean archive()
61     {
62         boolean ret;
63         if ((ret = closeLogFileAndArchive()) == true)
64         {
65             ret = openLogFile();
66         }
67         return ret;
68     }
69     
70     public String JavaDoc getErrorMessage()
71     {
72         return errorMessage;
73     }
74     
75     private boolean closeLogFileAndArchive()
76     {
77         boolean ret = false;
78         try
79         {
80             if (write("</" + AceLogger.EL_LOGS + ">") == false)
81             {
82                 // print error message
83
// and continue
84
String JavaDoc message = new String JavaDoc("LogFile.closeLogFileAndArchive() -- Error writing XML terminating tag to log file "
85                 + logFileName);
86                 LogProcessor lp = LogProcessor.Instance();
87                 if (lp == null)
88                 {
89                     System.err.println(message);
90                 }
91                 else
92                 {
93                     lp.log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
94                     message);
95                 }
96             }
97             
98             logFileStream.close(); // close the log file
99

100             int count = 0;
101             File archive_file = new File(archiveFileName + '.' + count);
102             File log_file = new File(logFileName);
103             
104             while (archive_file.exists() == true)
105             {
106                 count++;
107                 archive_file = new File(archiveFileName + '.' + count);
108             }
109             
110             ret = log_file.renameTo(archive_file);
111             if (ret == false) // rename failed
112
{
113                 writeErrorMessage("Could not move the log file to the archives directory");
114             }
115         }
116         catch (IOException ex)
117         {
118             writeErrorMessage(ex.getMessage());
119             return false;
120         }
121         return ret;
122     }
123     
124     
125     private boolean openLogFile()
126     {
127         try
128         {
129             logFileStream = new FileOutputStream(logFileName, true);
130             logOutputStream = new DataOutputStream(logFileStream);
131             archiveFileName = LogConfiguration.Instance().getArchivesFileName(msgType,
132             LogConfiguration.Instance().getNextArchivesInterval(),
133             myHostName);
134             
135             if (write("<?xml version=\"1.0\" encoding=\"us-ascii\"?>"
136             + '\n'
137             + "<" + AceLogger.EL_LOGS + ">") == false)
138             {
139                 return false;
140             }
141         }
142         catch (IOException ex)
143         {
144             writeErrorMessage(ex.getMessage());
145             return false;
146         }
147         return true;
148     }
149     
150     private void writeErrorMessage(String JavaDoc message)
151     {
152         errorMessage = new String JavaDoc(message);
153     }
154     
155     private String JavaDoc myHostName;
156     private int msgType;
157     private String JavaDoc errorMessage = "";
158     private String JavaDoc logFileName;
159     private DataOutputStream logOutputStream;
160     private FileOutputStream logFileStream;
161     private String JavaDoc archiveFileName;
162 }
163
Popular Tags