KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > io > FileTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.log.format.Formatter;
23
24 /**
25  * A basic target that writes to a File.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @author Peter Donald
29  */

30 public class FileTarget
31     extends StreamTarget
32 {
33     ///File we are writing to
34
private File JavaDoc m_file;
35
36     ///Flag indicating whether or not file should be appended to
37
private boolean m_append;
38
39     /**
40      * Construct file target to write to a file with a formatter.
41      *
42      * @param file the file to write to
43      * @param append true if file is to be appended to, false otherwise
44      * @param formatter the Formatter
45      * @exception IOException if an error occurs
46      */

47     public FileTarget( final File JavaDoc file, final boolean append, final Formatter formatter )
48         throws IOException JavaDoc
49     {
50         super( null, formatter );
51
52         if( null != file )
53         {
54             setFile( file, append );
55             openFile();
56         }
57     }
58
59     /**
60      * Set the file for this target.
61      *
62      * @param file the file to write to
63      * @param append true if file is to be appended to, false otherwise
64      * @exception IOException if directories can not be created or file can not be opened
65      */

66     protected synchronized void setFile( final File JavaDoc file, final boolean append )
67         throws IOException JavaDoc
68     {
69         if( null == file )
70         {
71             throw new NullPointerException JavaDoc( "file property must not be null" );
72         }
73
74         if( isOpen() )
75         {
76             throw new IOException JavaDoc( "target must be closed before "
77                                    + "file property can be set" );
78         }
79
80         m_append = append;
81         m_file = file;
82     }
83
84     /**
85      * Open underlying file and allocate resources.
86      * This method will attempt to create directories below file and
87      * append to it if specified.
88      * @exception IOException if directories can not be created or file can not be opened
89      */

90     protected synchronized void openFile()
91         throws IOException JavaDoc
92     {
93         if( isOpen() )
94         {
95             close();
96         }
97
98         final File JavaDoc file = getFile().getCanonicalFile();
99
100         final File JavaDoc parent = file.getParentFile();
101         if( null != parent && !parent.exists() )
102         {
103             parent.mkdirs();
104         }
105
106         final FileOutputStream JavaDoc outputStream =
107             new FileOutputStream JavaDoc( file.getPath(), m_append );
108
109         setOutputStream( outputStream );
110         open();
111     }
112
113     /**
114      * Retrieve file associated with target.
115      * This allows subclasses to access file object.
116      *
117      * @return the output File
118      */

119     protected synchronized File JavaDoc getFile()
120     {
121         return m_file;
122     }
123 }
124
Popular Tags