Add file access synchronization to prevent conflicting tile updates

This commit is contained in:
Mike Primm 2011-05-31 21:20:23 -05:00
parent 5dbb69f2fe
commit c48d06eec1
5 changed files with 135 additions and 2 deletions

View file

@ -13,6 +13,7 @@ import org.dynmap.web.HttpHandler;
import org.dynmap.web.HttpRequest;
import org.dynmap.web.HttpResponse;
import org.dynmap.web.HttpStatus;
import org.dynmap.utils.FileLockManager;
public abstract class FileHandler implements HttpHandler {
protected static final Logger log = Logger.getLogger("Minecraft");
@ -42,6 +43,10 @@ public abstract class FileHandler implements HttpHandler {
}
protected abstract InputStream getFileInput(String path, HttpRequest request, HttpResponse response);
protected void closeFileInput(String path, InputStream in) throws IOException {
in.close();
}
protected String getExtension(String path) {
int dotindex = path.lastIndexOf('.');
@ -113,7 +118,7 @@ public abstract class FileHandler implements HttpHandler {
} finally {
freeReadBuffer(readBuffer);
}
fileInput.close();
closeFileInput(path, fileInput);
} catch (Exception e) {
if (fileInput != null) {
try { fileInput.close(); } catch (IOException ex) { }

View file

@ -3,8 +3,10 @@ package org.dynmap.web.handlers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.dynmap.utils.FileLockManager;
import org.dynmap.web.HttpField;
import org.dynmap.web.HttpRequest;
import org.dynmap.web.HttpResponse;
@ -20,6 +22,7 @@ public class FilesystemHandler extends FileHandler {
@Override
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
File file = new File(root, path);
FileLockManager.getReadLock(file);
if (file.getAbsolutePath().startsWith(root.getAbsolutePath()) && file.isFile()) {
FileInputStream result;
try {
@ -30,6 +33,13 @@ public class FilesystemHandler extends FileHandler {
response.fields.put(HttpField.ContentLength, Long.toString(file.length()));
return result;
}
FileLockManager.releaseReadLock(file);
return null;
}
protected void closeFileInput(String path, InputStream in) throws IOException {
super.closeFileInput(path, in);
File file = new File(root, path);
FileLockManager.releaseReadLock(file);
}
}