changeset 1496:323a36b41c4a

LockedFile.java: caching enabled, or at least semi-enabled on windows
author Jiri Vanek <jvanek@redhat.com>
date Wed, 17 Oct 2018 18:36:19 +0200
parents 5b3e94efb41c
children d0eb4fb4bc55
files ChangeLog netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java
diffstat 2 files changed, 41 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Oct 17 18:16:40 2018 +0200
+++ b/ChangeLog	Wed Oct 17 18:36:19 2018 +0200
@@ -1,3 +1,8 @@
+2018-10-17  Jiri Vanek <jvanek@redhat.com>
+            Alex Kashchenko <akashche@redhat.com>
+
+	* netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java: caching enabled, or at least semi-enabled on windows
+
 2018-10-17  Laurent Bourgès <bourges.laurent@gmail.com>
 
 	* netx/net/sourceforge/jnlp/controlpanel/CachePane.java: using SwingUtils.invokeLater instead of 
--- a/netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java	Wed Oct 17 18:16:40 2018 +0200
+++ b/netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java	Wed Oct 17 18:36:19 2018 +0200
@@ -94,7 +94,13 @@
      */
     synchronized public static LockedFile getInstance(File file) {
         if (!instanceCache.containsKey(file)) {
-            instanceCache.put(file, new LockedFile(file));
+            LockedFile l;
+            if (JNLPRuntime.isWindows()) {
+                l = new WindowsLockedFile(file);
+            } else {
+                l = new LockedFile(file);
+            }
+            instanceCache.put(file, l);
         }
 
         return instanceCache.get(file);
@@ -113,9 +119,6 @@
      * Lock access to the file. Lock is reentrant.
      */
     public void lock() throws IOException {
-        if (JNLPRuntime.isWindows()) {
-            return;
-        }
         // Create if does not already exist, cannot lock non-existing file
         if (!isReadOnly()) {
             this.file.createNewFile();
@@ -153,10 +156,14 @@
      * Unlock access to the file. Lock is reentrant. Does not do anything if not holding the lock.
      */
     public void unlock() throws IOException {
-        if (JNLPRuntime.isWindows() || !this.threadLock.isHeldByCurrentThread()) {
+        if (!this.threadLock.isHeldByCurrentThread()) {
             return;
         }
         boolean releaseProcessLock = (this.threadLock.getHoldCount() == 1);
+        unlockImpl(releaseProcessLock);
+    }
+
+    protected void unlockImpl(boolean releaseProcessLock) throws IOException {
         try {
             if (releaseProcessLock) {
                 if (this.processLock != null){
@@ -180,4 +187,27 @@
     public boolean isHeldByCurrentThread() {
         return this.threadLock.isHeldByCurrentThread();
     }
-}
\ No newline at end of file
+
+    private static class WindowsLockedFile extends LockedFile {
+
+        public WindowsLockedFile(File file) {
+            super(file);
+        }
+
+        /*Comment why itis different*/
+        @Override
+        public void lock() throws IOException {
+            super.file.createNewFile();
+            super.threadLock.lock();
+        }
+
+        /*Comment why itis different*/
+        @Override
+        public void unlock() throws IOException {
+            if (!super.threadLock.isHeldByCurrentThread()) {
+                return;
+            }
+            unlockImpl(false);
+        }
+    }
+}