KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > OpenFileLog


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import com.protomatter.util.*;
59
60 /**
61  * A logger that opens the file for each log entry and closes it
62  * after it's done writing.
63  *
64  * @see com.protomatter.syslog.xml.OpenFileLog_Helper XML configuration class
65  */

66 public class OpenFileLog
67 extends BasicLogger
68 {
69    private boolean fileChanged = true;
70
71    private File file;
72
73    /**
74     * Create an OpenFileLog attached to the given file.
75     */

76    public OpenFileLog(File f)
77    {
78       this();
79       this.file = f;
80    }
81
82    /**
83     * You will need to call the configure() method if you use this constructor.
84     */

85    public OpenFileLog()
86    {
87      super();
88    }
89
90    /**
91     * Set the file we're writing to.
92     */

93    public void setFile(File f)
94    {
95       cleanup();
96       file = f;
97       fileChanged = true;
98    }
99
100    /**
101     * Get the file we're writing to.
102     */

103    public File getFile()
104    {
105      return this.file;
106    }
107
108    /**
109     * Write a log message.
110     */

111    public final void log(SyslogMessage message)
112    {
113       StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
114       formatLogEntry(b, message);
115
116       try
117       {
118          PrintWriter out = new PrintWriter(
119            new FileWriter(file.getCanonicalPath(), true));
120          if (fileChanged)
121          {
122            out.print(formatter.getLogHeader());
123            fileChanged = false;
124          }
125          out.print(b);
126          out.flush();
127          out.close();
128       }
129       catch (IOException x)
130       {
131         System.err.println(MessageFormat.format(
132           Syslog.getResourceString(MessageConstants.OPENFLOG_CANNOT_OPEN_MESSAGE),
133           new Object JavaDoc[] { x.toString() }));
134       }
135    }
136
137    private void cleanup()
138    {
139      try
140      {
141        if (file != null)
142        {
143          PrintWriter out = new PrintWriter(
144            new FileWriter(file.getCanonicalPath(), true));
145          out.print(formatter.getLogFooter());
146          out.flush();
147          out.close();
148        }
149      }
150      catch (IOException x)
151      {
152        System.err.println(MessageFormat.format(
153          Syslog.getResourceString(MessageConstants.OPENFLOG_CANNOT_OPEN_MESSAGE),
154          new Object JavaDoc[] { x.toString() }));
155      }
156    }
157
158    /**
159     * Cleanup our file and prepare for shutdown.
160     */

161    public void shutdown()
162    {
163      cleanup();
164    }
165
166    public void flush()
167    {
168      // do nothing.
169
}
170 }
171
Popular Tags