From 6c1f3f8e2b17683ca430d3440db45a86df10143c Mon Sep 17 00:00:00 2001 From: shuyangzhou Date: Sun, 3 May 2015 21:59:02 -0700 Subject: [PATCH] LPS-55159 Try-with-resources FileLock management --- .../whip/coveragedata/ProjectDataUtil.java | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/modules/test/whip/src/com/liferay/whip/coveragedata/ProjectDataUtil.java b/modules/test/whip/src/com/liferay/whip/coveragedata/ProjectDataUtil.java index 4f2956c070bb29..642af395906c4b 100644 --- a/modules/test/whip/src/com/liferay/whip/coveragedata/ProjectDataUtil.java +++ b/modules/test/whip/src/com/liferay/whip/coveragedata/ProjectDataUtil.java @@ -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); } } } @@ -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( @@ -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) {