Skip to content

Commit

Permalink
LPS-55159 Try-with-resources FileLock management
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyangzhou authored and brianchandotcom committed May 4, 2015
1 parent d5b773b commit 6c1f3f8
Showing 1 changed file with 24 additions and 41 deletions.
Expand Up @@ -36,26 +36,36 @@ public static ProjectData captureProjectData(boolean saveSessionData) {
String className = ProjectDataUtil.class.getName();

synchronized (className.intern()) {
FileLock fileLock = _lockFile();
try (RandomAccessFile randomAccessFile = new RandomAccessFile(
InstrumentationAgent.getLockFile(), "rw")) {

try {
File dataFile = new File(
System.getProperty("net.sourceforge.cobertura.datafile"));
FileChannel fileChannel = randomAccessFile.getChannel();

if (dataFile.exists()) {
_projectData.merge(_readProjectData(dataFile));
FileLock fileLock = fileChannel.lock();

dataFile.delete();
}
try {
File dataFile = new File(
System.getProperty(
"net.sourceforge.cobertura.datafile"));

if (saveSessionData) {
_writeProjectData(_projectData, dataFile);
}
if (dataFile.exists()) {
_projectData.merge(_readProjectData(dataFile));

dataFile.delete();
}

return _projectData;
if (saveSessionData) {
_writeProjectData(_projectData, dataFile);
}

return _projectData;
}
finally {
fileLock.release();
}
}
finally {
_unlockFile(fileLock);
catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
Expand All @@ -64,20 +74,6 @@ public static ProjectData getProjectData() {
return _projectData;
}

private static FileLock _lockFile() {
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(
InstrumentationAgent.getLockFile(), "rw");

FileChannel fileChannel = randomAccessFile.getChannel();

return fileChannel.lock();
}
catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}

private static ProjectData _readProjectData(File dataFile) {
try (FileInputStream fileInputStream = new FileInputStream(dataFile);
ObjectInputStream objectInputStream = new ObjectInputStream(
Expand All @@ -90,19 +86,6 @@ private static ProjectData _readProjectData(File dataFile) {
}
}

private static void _unlockFile(FileLock fileLock) {
try {
fileLock.release();

FileChannel fileChannel = fileLock.channel();

fileChannel.close();
}
catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}

private static void _writeProjectData(
ProjectData projectData, File dataFile) {

Expand Down

0 comments on commit 6c1f3f8

Please sign in to comment.