From: Sasa M. <sa...@us...> - 2004-06-29 12:21:13
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3086/org/jrobin/core Modified Files: RrdFileBackend.java Log Message: Minor change in the code to disallow R/W access to the same file from two different RrdDb objects Index: RrdFileBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFileBackend.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** RrdFileBackend.java 29 Jun 2004 09:02:22 -0000 1.5 --- RrdFileBackend.java 29 Jun 2004 12:20:55 -0000 1.6 *************** *** 31,34 **** --- 31,35 ---- import java.nio.channels.FileChannel; import java.nio.channels.FileLock; + import java.util.HashSet; /** *************** *** 41,44 **** --- 42,47 ---- static final long LOCK_DELAY = 100; // 0.1sec + private static HashSet openFiles = new HashSet(); + protected RandomAccessFile file; protected FileLock fileLock; *************** *** 46,49 **** --- 49,55 ---- protected RrdFileBackend(String path, boolean readOnly, int lockMode) throws IOException { super(path); + if(!readOnly) { + registerWriter(path); + } file = new RandomAccessFile(path, readOnly? "r": "rw"); // We'll try to lock the file only in "rw" mode *************** *** 54,57 **** --- 60,74 ---- } + private static synchronized void registerWriter(String path) throws IOException { + String canonicalPath = getCanonicalPath(path); + if(openFiles.contains(canonicalPath)) { + throw new IOException("File \"" + path + "\" already open for R/W access. " + + "You cannot open the same file for R/W access twice"); + } + else { + openFiles.add(canonicalPath); + } + } + private void lockFile(int lockMode) throws IOException { if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { *************** *** 86,89 **** --- 103,112 ---- unlockFile(); file.close(); + unregisterWriter(getPath()); + } + + private static synchronized void unregisterWriter(String path) throws IOException { + String canonicalPath = getCanonicalPath(path); + openFiles.remove(canonicalPath); } |