KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > LockableFileWriter


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

16 package org.apache.commons.io.output;
17
18 import java.io.File JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /**
24  * FileWriter that will create and honor lock files to allow simple
25  * cross thread file lock handling. If <code>Writer</code> attributes
26  * are unspecified, the default behavior is to overwrite (rather than
27  * to append), and to use the value of the system property
28  * <code>java.io.tmpdir</code> for the lock file directory.
29  *
30  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
31  * @author <a HREF="mailto:ms@collab.net">Michael Salmon</a>
32  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
33  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
34  * @version $Id: LockableFileWriter.java,v 1.7 2004/02/23 04:40:29 bayard Exp $
35  */

36 public class LockableFileWriter extends Writer JavaDoc {
37
38     private static final String JavaDoc LCK = ".lck";
39
40     private File JavaDoc lockFile = null;
41
42     private FileWriter JavaDoc writer = null;
43
44     private boolean append = false;
45
46     /**
47      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
48      * @param fileName file to write to
49      * @throws IOException in case of an I/O error
50      */

51     public LockableFileWriter(String JavaDoc fileName)
52             throws IOException JavaDoc {
53         this(fileName, false, null);
54     }
55
56     /**
57      * Constructs a LockableFileWriter.
58      * @param fileName file to write to
59      * @param append true if content should be appended (default is to overwrite).
60      * @throws IOException in case of an I/O error
61      */

62     public LockableFileWriter(String JavaDoc fileName, boolean append)
63             throws IOException JavaDoc {
64         this(fileName, append, null);
65     }
66
67     /**
68      * Constructs a LockableFileWriter.
69      * @param fileName file to write to
70      * @param append true if content should be appended (default is to overwrite).
71      * @param lockDir Specifies the directory in which the lock file should be held.
72      * @throws IOException in case of an I/O error
73      */

74     public LockableFileWriter(String JavaDoc fileName, boolean append, String JavaDoc lockDir)
75             throws IOException JavaDoc {
76         this(new File JavaDoc(fileName), append, lockDir);
77     }
78
79     /**
80      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
81      * @param file file to write to
82      * @throws IOException in case of an I/O error
83      */

84     public LockableFileWriter(File JavaDoc file)
85             throws IOException JavaDoc {
86         this(file, false, null);
87     }
88
89     /**
90      * Constructs a LockableFileWriter.
91      * @param file file to write to
92      * @param append true if content should be appended (default is to overwrite).
93      * @throws IOException in case of an I/O error
94      */

95     public LockableFileWriter(File JavaDoc file, boolean append)
96             throws IOException JavaDoc {
97         this(file, append, null);
98     }
99
100     /**
101      * Constructs a LockableFileWriter.
102      * @param file file to write to
103      * @param append true if content should be appended (default is to overwrite).
104      * @param lockDir Specifies the directory in which the lock file should be held.
105      * @throws IOException in case of an I/O error
106      */

107     public LockableFileWriter(File JavaDoc file, boolean append, String JavaDoc lockDir)
108             throws IOException JavaDoc {
109         this.append = append;
110
111         if (lockDir == null) {
112             lockDir = System.getProperty("java.io.tmpdir");
113         }
114         testLockDir(new File JavaDoc(lockDir));
115         this.lockFile = new File JavaDoc(lockDir, file.getName() + LCK);
116         createLock();
117
118         this.writer = new FileWriter JavaDoc(file.getAbsolutePath(), this.append);
119     }
120
121     private void testLockDir(File JavaDoc lockDir)
122             throws IOException JavaDoc {
123         if (!lockDir.exists()) {
124             throw new IOException JavaDoc(
125                     "Could not find lockDir: " + lockDir.getAbsolutePath());
126         }
127         if (!lockDir.canWrite()) {
128             throw new IOException JavaDoc(
129                     "Could not write to lockDir: " + lockDir.getAbsolutePath());
130         }
131     }
132
133     private void createLock()
134             throws IOException JavaDoc {
135         synchronized (LockableFileWriter.class) {
136             if (!lockFile.createNewFile()) {
137                 throw new IOException JavaDoc("Can't write file, lock " +
138                         lockFile.getAbsolutePath() + " exists");
139             }
140             lockFile.deleteOnExit();
141         }
142     }
143
144     /** @see java.io.Writer#close() */
145     public void close()
146             throws IOException JavaDoc {
147         try {
148             writer.close();
149         } finally {
150             lockFile.delete();
151         }
152     }
153
154     /** @see java.io.Writer#write(char[], int, int) */
155     public void write(char[] cbuf, int off, int len)
156             throws IOException JavaDoc {
157         writer.write(cbuf, off, len);
158     }
159
160     /** @see java.io.Writer#flush() */
161     public void flush()
162             throws IOException JavaDoc {
163         writer.flush();
164     }
165 }
166
Popular Tags