From dc4876fa337df18ac8b798e919db63bed7ee3c99 Mon Sep 17 00:00:00 2001 From: Max Schenkelberg Date: Fri, 16 Nov 2012 14:03:18 -0600 Subject: [PATCH] Issue #1243 Revert locking in UtilityManager.getFileChecksum to avoid lock timing out until FileLocker can be fixed Change-Id: I25ab9832d725647c17aea505514334a7c0026ae7 Former-commit-id: 34c4ec44c35d97be20a340248e24bdd28740d28b [formerly 0daa22064b561264d170c1eadc6c5f5e64c64698] [formerly 34c4ec44c35d97be20a340248e24bdd28740d28b [formerly 0daa22064b561264d170c1eadc6c5f5e64c64698] [formerly 71367ef5bbb0316d39badfe4f767e724591e2b22 [formerly a7386c6570455fe6f9175eaeae1225923124cce8]]] Former-commit-id: 71367ef5bbb0316d39badfe4f767e724591e2b22 Former-commit-id: afc4de9a1ab8a1a23d9774fe16cc7df2cbdb4fbb [formerly 102c2dbaa165af145e9f7906cbf94281f486cccb] Former-commit-id: 755c64bfdc4decd09455588ede219b99eb61f7d5 --- .../uf/viz/points/PointsDataManager.java | 13 +++++++++---- .../edex/services/UtilityManager.java | 7 ++----- .../uf/common/localization/FileLocker.java | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/cave/com.raytheon.uf.viz.points/src/com/raytheon/uf/viz/points/PointsDataManager.java b/cave/com.raytheon.uf.viz.points/src/com/raytheon/uf/viz/points/PointsDataManager.java index e254caa7a1..db6d844c89 100644 --- a/cave/com.raytheon.uf.viz.points/src/com/raytheon/uf/viz/points/PointsDataManager.java +++ b/cave/com.raytheon.uf.viz.points/src/com/raytheon/uf/viz/points/PointsDataManager.java @@ -1756,10 +1756,15 @@ public class PointsDataManager implements ILocalizationFileObserver { */ private Point unmarshalPointFromXmlFile(LocalizationFile lFile) throws LocalizationException, IOException { - InputStream stream = lFile.openInputStream(); - Point point = JAXB.unmarshal(stream, Point.class); - stream.close(); - return point; + InputStream stream = null; + try { + stream = lFile.openInputStream(); + return JAXB.unmarshal(stream, Point.class); + } finally { + if (stream != null) { + stream.close(); + } + } } /** diff --git a/edexOsgi/com.raytheon.edex.utilitysrv/src/com/raytheon/edex/services/UtilityManager.java b/edexOsgi/com.raytheon.edex.utilitysrv/src/com/raytheon/edex/services/UtilityManager.java index 9081ad9b54..115429bf90 100644 --- a/edexOsgi/com.raytheon.edex.utilitysrv/src/com/raytheon/edex/services/UtilityManager.java +++ b/edexOsgi/com.raytheon.edex.utilitysrv/src/com/raytheon/edex/services/UtilityManager.java @@ -34,8 +34,6 @@ import org.apache.commons.logging.LogFactory; import com.raytheon.edex.utility.ProtectedFiles; import com.raytheon.uf.common.localization.Checksum; -import com.raytheon.uf.common.localization.FileLocker; -import com.raytheon.uf.common.localization.FileLocker.Type; import com.raytheon.uf.common.localization.FileUpdatedMessage; import com.raytheon.uf.common.localization.FileUpdatedMessage.FileChangeType; import com.raytheon.uf.common.localization.LocalizationContext; @@ -123,12 +121,13 @@ public class UtilityManager { * @throws EdexException */ private static String getFileChecksum(File file) throws EdexException { - FileLocker.lock(UtilityManager.class, file, Type.WRITE); + // TODO: Fix FileLocker so it never times out in test driver File checksumFile = getChecksumFile(file); String chksum = null; try { if (checksumFile.exists() && checksumFile.lastModified() >= file.lastModified()) { + BufferedReader reader = new BufferedReader(new FileReader( checksumFile)); try { @@ -144,8 +143,6 @@ public class UtilityManager { } catch (Exception e) { // log, no checksum will be provided logger.error("Error determing file checksum for: " + file, e); - } finally { - FileLocker.unlock(UtilityManager.class, file); } return chksum; } diff --git a/edexOsgi/com.raytheon.uf.common.localization/src/com/raytheon/uf/common/localization/FileLocker.java b/edexOsgi/com.raytheon.uf.common.localization/src/com/raytheon/uf/common/localization/FileLocker.java index a8d2de7e5b..5cc0038443 100644 --- a/edexOsgi/com.raytheon.uf.common.localization/src/com/raytheon/uf/common/localization/FileLocker.java +++ b/edexOsgi/com.raytheon.uf.common.localization/src/com/raytheon/uf/common/localization/FileLocker.java @@ -78,6 +78,8 @@ public class FileLocker { READ, WRITE } + private static final int MAX_WAIT = 30 * 1000; + /** Map of waiters on threads */ private Map> waiters = new HashMap>(); @@ -215,7 +217,7 @@ public class FileLocker { while (true) { // Sleep try { - Thread.sleep(1); + Thread.sleep(10); } catch (InterruptedException e) { // Ignore } @@ -231,6 +233,20 @@ public class FileLocker { false); } } + } else { + synchronized (lock) { + if (lock.lockFile.exists() == false + || (System.currentTimeMillis() + - lock.lockFile.lastModified() > MAX_WAIT)) { + System.err + .println("Releasing lock since: " + + (lock.lockFile.exists() ? "Lock has been allocated for more than " + + (MAX_WAIT / 1000) + + "s" + : "Lock file no longer exists!")); + locks.remove(file); + } + } } } } @@ -317,7 +333,6 @@ public class FileLocker { File lockFile = new File(parentDir, "." + file.getName() + "_LOCK"); boolean gotLock = lockFile.createNewFile(); if (!gotLock) { - long MAX_WAIT = 30 * 1000; long waitInterval = 500; long tryCount = MAX_WAIT / waitInterval; for (int i = 0; !gotLock && i < tryCount; ++i) {