KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > io > FileTarget


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output.io;
9
10 import org.jivesoftware.util.log.format.Formatter;
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * A basic target that writes to a File.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public class FileTarget extends StreamTarget {
21
22     ///File we are writing to
23
private File JavaDoc m_file;
24
25     ///Flag indicating whether or not file should be appended to
26
private boolean m_append;
27
28     /**
29      * Construct file target to send to a file with a formatter.
30      *
31      * @param file the file to send to
32      * @param append true if file is to be appended to, false otherwise
33      * @param formatter the Formatter
34      * @throws IOException if an error occurs
35      */

36     public FileTarget(final File JavaDoc file, final boolean append, final Formatter formatter)
37             throws IOException JavaDoc {
38         super(null, formatter);
39
40         if (null != file) {
41             setFile(file, append);
42             openFile();
43         }
44     }
45
46     /**
47      * Set the file for this target.
48      *
49      * @param file the file to send to
50      * @param append true if file is to be appended to, false otherwise
51      * @throws IOException if directories can not be created or file can not be opened
52      */

53     protected synchronized void setFile(final File JavaDoc file, final boolean append)
54             throws IOException JavaDoc {
55         if (null == file) {
56             throw new NullPointerException JavaDoc("file property must not be null");
57         }
58
59         if (isOpen()) {
60             throw new IOException JavaDoc("target must be closed before " +
61                     "file property can be set");
62         }
63
64         m_append = append;
65         m_file = file;
66     }
67
68     /**
69      * Open underlying file and allocate resources.
70      * This method will attempt to create directories below file and
71      * append to it if specified.
72      */

73     protected synchronized void openFile()
74             throws IOException JavaDoc {
75         if (isOpen()) close();
76
77         final File JavaDoc file = getFile().getCanonicalFile();
78
79         final File JavaDoc parent = file.getParentFile();
80         if (null != parent && !parent.exists()) {
81             parent.mkdir();
82         }
83
84         final FileOutputStream JavaDoc outputStream =
85                 new FileOutputStream JavaDoc(file.getPath(), m_append);
86
87         setOutputStream(outputStream);
88         open();
89     }
90
91     /**
92      * Retrieve file associated with target.
93      * This allows subclasses to access file object.
94      *
95      * @return the output File
96      */

97     protected synchronized File JavaDoc getFile() {
98         return m_file;
99     }
100 }
101
Popular Tags