jon 01/08/21 14:21:21
Modified: util/src/java/org/apache/commons/util
LockableFileWriter.java
Log:
next implementation of LFW
fixes a bunch of bugs and adds a few features and potential performance
improvements.
Revision Changes Path
1.2 +74 -45
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LockableFileWriter.java 2001/08/21 20:48:47 1.1
+++ LockableFileWriter.java 2001/08/21 21:21:21 1.2
@@ -60,99 +60,128 @@
import java.io.IOException;
/**
- * A <code>FileWriter</code> that will create and honor lock files to
- * allow simple cross thread file lock handling.
+ * FileWriter that will create and honor lock files to allow simple
+ * cross thread file lock handling. By default, append is false.
*
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Michael Salmon</a>
- * @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
- * @version $Id: LockableFileWriter.java,v 1.1 2001/08/21 20:48:47 dlr Exp $
+ * @version $Id: LockableFileWriter.java,v 1.2 2001/08/21 21:21:21 jon Exp $
*/
public class LockableFileWriter extends Writer
{
- private static final String LCK = ".lck";
-
+ private File fileName = null;
+ private static File lockDir = new File(System.getProperty("java.io.tmpdir"));
private File lockFile = null;
+ private final String LCK = ".lck";
private FileWriter writer = null;
-
- private String fileName = null;
-
- private String lockDir = null;
+ private boolean append = false;
+ private static final String DIR_SEPERATOR =
System.getProperty("file.seperator");
- public LockableFileWriter (String fileName)
+ /**
+ * 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 = fileName;
- createLock();
- this.writer = new FileWriter(fileName);
+ this(fileName, false);
}
- public LockableFileWriter (String fileName, String lockDir)
+ /**
+ * Write to a file.
+ */
+ public LockableFileWriter(String fileName, String lockDir)
throws IOException
{
- this.fileName = fileName;
- this.lockDir = lockDir;
- createLock();
- this.writer = new FileWriter(fileName);
+ this(new File(lockDir), lockDir);
}
- public LockableFileWriter (String fileName, boolean append)
+ public LockableFileWriter(String fileName, boolean append)
throws IOException
{
- this.fileName = fileName;
- createLock();
- this.writer = new FileWriter(fileName, append);
+ this.append = append;
+ this.fileName = new File(lockDir, fileName);
+ init();
}
- public LockableFileWriter (String fileName, boolean append, String lockDir)
+ public LockableFileWriter(String fileName, boolean append, String lockDir)
throws IOException
{
- this.fileName = fileName;
- this.lockDir = lockDir;
- createLock();
- this.writer = new FileWriter(fileName, append);
+ this.append = append;
+ this.lockDir = new File(lockDir);
+ this.fileName = new File(lockDir, fileName);
+ init();
}
- public LockableFileWriter (File file)
+ public LockableFileWriter(File file)
throws IOException
{
- this.fileName = file.getPath();
- createLock();
- this.writer = new FileWriter(file);
+ this.fileName = file;
+ init();
}
- public LockableFileWriter (File file, String lockDir)
+ public LockableFileWriter(File file, String lockDir)
+ throws IOException
+ {
+ this.fileName = file;
+ this.lockDir = new File(lockDir);
+ init();
+ }
+
+ private void init()
throws IOException
{
- this.fileName = file.getPath();
- this.lockDir = lockDir;
+ testLockDir();
+ this.lockFile = new File (lockDir, fileName.getName() + LCK);
createLock();
- this.writer = new FileWriter(file);
+ this.writer = new FileWriter(fileName.getAbsolutePath(), this.append);
}
+ private void testLockDir()
+ throws IOException
+ {
+ if (!lockDir.exists())
+ {
+ throw new IOException(
+ "Could not find lockDir: " + lockDir.getAbsolutePath());
+ }
+ if (!lockDir.canWrite())
+ {
+ throw new IOException(
+ "Could not write to lockDir: " + lockDir.getAbsolutePath());
+ }
+ }
+
private void createLock()
throws IOException
{
- synchronized (LockableFileWriter.class)
+ synchronized(LockFileWriter.class)
{
- File lock = new File(lockDir, fileName + LCK);
- if (lock.exists())
+ if (!lockFile.createNewFile())
{
- throw new IOException("Can't write file, lock " +
- lock.getName() + " exists");
+ throw new IOException("Can't write file, " +
+ lockFile.getAbsolutePath() + " exists");
}
- lockFile = lock;
+ lockFile.deleteOnExit();
}
}
public void close()
throws IOException
{
- writer.close();
- lockFile.delete();
+ try
+ {
+ writer.close();
+ }
+ finally
+ {
+ lockFile.delete();
+ }
}
- public void write(char[] cbuf, int off, int len)
+ public void write(char [] cbuf, int off, int len)
throws IOException
{
writer.write(cbuf, off, len);