dlr 01/08/21 16:53:46
Modified: util/src/java/org/apache/commons/util
LockableFileWriter.java
Log:
* Reworked constructors to delegate to each other to remove duplicate
code. Removed some instance members in the process.
* Dumped init() in favor of additional constructor. This necessitated
adding a File param to testLockDir().
* Sync'd createLock() on the correct class name.
Revision Changes Path
1.5 +25 -41
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/LockableFileWriter.java
Index: LockableFileWriter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/LockableFileWriter.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -u -r1.4 -r1.5
--- LockableFileWriter.java 2001/08/21 23:13:34 1.4
+++ LockableFileWriter.java 2001/08/21 23:53:46 1.5
@@ -60,90 +60,74 @@
import java.io.IOException;
/**
- * FileWriter that will create and honor lock files to allow simple
- * cross thread file lock handling. By default, append is false.
+ * FileWriter that will create and honor lock files to allow simple
+ * cross thread file lock handling. If <code>Writer</code> attributes
+ * are unspecified, the default behavior is to overwrite (rather than
+ * to append), and to use the value of the system property
+ * <code>java.io.tmpdir</code> for the lock file directory.
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Michael Salmon</a>
- * @version $Id: LockableFileWriter.java,v 1.4 2001/08/21 23:13:34 dlr Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
+ * @version $Id: LockableFileWriter.java,v 1.5 2001/08/21 23:53:46 dlr Exp $
*/
public class LockableFileWriter extends Writer
{
private static final String LCK = ".lck";
- private static File lockDir =
- new File(System.getProperty("java.io.tmpdir"));
-
- private File file = null;
-
private File lockFile = null;
private FileWriter writer = null;
private boolean append = false;
- /**
- * Write to a file. Uses java.io.tmpdir System property for the
- * location of the temp directory to write the lock files to.
- * append is false.
- */
public LockableFileWriter(String fileName)
throws IOException
- {
- this(fileName, false);
- }
-
- /**
- * Write to a file.
- */
- public LockableFileWriter(String fileName, String lockDir)
- throws IOException
{
- this(new File(lockDir), lockDir);
+ this(fileName, false, null);
}
public LockableFileWriter(String fileName, boolean append)
throws IOException
{
- this.append = append;
- this.file = new File(lockDir, fileName);
- init();
+ this(fileName, append, null);
}
public LockableFileWriter(String fileName, boolean append, String lockDir)
throws IOException
{
- this.append = append;
- this.lockDir = new File(lockDir);
- this.file = new File(lockDir, fileName);
- init();
+ this(new File(fileName), append, lockDir);
}
public LockableFileWriter(File file)
throws IOException
{
- this.file = file;
- init();
+ this(file, false, null);
}
- public LockableFileWriter(File file, String lockDir)
+ public LockableFileWriter(File file, boolean append)
throws IOException
{
- this.file = file;
- this.lockDir = new File(lockDir);
- init();
+ this(file, append, null);
}
- private void init()
+ public LockableFileWriter(File file, boolean append, String lockDir)
throws IOException
{
- testLockDir();
+ this.append = append;
+
+ if (lockDir == null)
+ {
+ lockDir = System.getProperty("java.io.tmpdir");
+ }
+ testLockDir(new File(lockDir));
this.lockFile = new File(lockDir, file.getName() + LCK);
createLock();
+
this.writer = new FileWriter(file.getAbsolutePath(), this.append);
}
- private void testLockDir()
+ private void testLockDir(File lockDir)
throws IOException
{
if (!lockDir.exists())
@@ -161,7 +145,7 @@
private void createLock()
throws IOException
{
- synchronized(LockFileWriter.class)
+ synchronized (LockableFileWriter.class)
{
if (!lockFile.createNewFile())
{