You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2013/12/16 23:01:19 UTC

svn commit: r1551370 - /archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java

Author: olamy
Date: Mon Dec 16 22:01:19 2013
New Revision: 1551370

URL: http://svn.apache.org/r1551370
Log:
better handling of FNFE

Modified:
    archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java

Modified: archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java
URL: http://svn.apache.org/viewvc/archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java?rev=1551370&r1=1551369&r2=1551370&view=diff
==============================================================================
--- archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java (original)
+++ archiva/trunk/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java Mon Dec 16 22:01:19 2013
@@ -63,76 +63,65 @@ public class DefaultFileLockManager
         StopWatch stopWatch = new StopWatch();
         boolean acquired = false;
         mkdirs( file.getParentFile() );
-        try
-        {
 
-            Lock lock = null;
+        Lock lock = null;
 
-            stopWatch.start();
+        stopWatch.start();
 
-            while ( !acquired )
-            {
+        while ( !acquired )
+        {
 
-                if ( timeout > 0 )
-                {
-                    long delta = stopWatch.getTime();
-                    log.debug( "delta {}, timeout {}", delta, timeout );
-                    if ( delta > timeout )
-                    {
-                        log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
-                        // we could not get the lock within the timeout period, so  throw  FileLockTimeoutException
-                        throw new FileLockTimeoutException();
-                    }
+            if ( timeout > 0 )
+            {
+                long delta = stopWatch.getTime();
+                log.debug( "delta {}, timeout {}", delta, timeout );
+                if ( delta > timeout )
+                {
+                    log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
+                    // we could not get the lock within the timeout period, so  throw  FileLockTimeoutException
+                    throw new FileLockTimeoutException();
                 }
+            }
 
-                lock = new Lock( file, false );
-
-                Lock current = lockFiles.get( file );
+            Lock current = lockFiles.get( file );
 
-                if ( current != null )
-                {
-                    log.debug( "read lock file exist continue wait" );
-                    // close RandomAccessFile!!!
-                    RandomAccessFile raf =  lock.getRandomAccessFile();
-                    if (raf != null)
-                    {
-                        raf.close();
-                    }
-                    continue;
-                }
+            if ( current != null )
+            {
+                log.debug( "read lock file exist continue wait" );
+                continue;
+            }
 
-                try
-                {
-                    createNewFileQuietly( file );
-                    lock.openLock( false, timeout > 0 );
-                    acquired = true;
-                }
-                catch ( FileNotFoundException e )
-                {
-                    // can happen if an other thread has deleted the file
-                    log.debug( "read Lock skip: {} try to create file", e.getMessage() );
-                    createNewFileQuietly( file );
-                }
-                catch ( IOException e )
-                {
-                    throw new FileLockException( e.getMessage(), e );
-                }
-                catch ( IllegalStateException e )
-                {
-                    log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
-                }
+            try
+            {
+                lock = new Lock( file, false );
+                createNewFileQuietly( file );
+                lock.openLock( false, timeout > 0 );
+                acquired = true;
             }
-            Lock current = lockFiles.putIfAbsent( file, lock );
-            if ( current != null )
+            catch ( FileNotFoundException e )
             {
-                lock = current;
+                // can happen if an other thread has deleted the file
+                // close RandomAccessFile!!!
+                closeQuietly( lock.getRandomAccessFile() );
+                log.debug( "read Lock skip: {} try to create file", e.getMessage() );
+                createNewFileQuietly( file );
+            }
+            catch ( IOException e )
+            {
+                throw new FileLockException( e.getMessage(), e );
+            }
+            catch ( IllegalStateException e )
+            {
+                log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
             }
-            return lock;
         }
-        catch ( IOException e )
+        Lock current = lockFiles.putIfAbsent( file, lock );
+        if ( current != null )
         {
-            throw new FileLockException( e.getMessage(), e );
+            lock = current;
         }
+        return lock;
+
     }
 
 
@@ -150,85 +139,86 @@ public class DefaultFileLockManager
         StopWatch stopWatch = new StopWatch();
         boolean acquired = false;
 
-        try
-        {
-            Lock lock = null;
+        Lock lock = null;
 
-            stopWatch.start();
+        stopWatch.start();
 
-            while ( !acquired )
-            {
+        while ( !acquired )
+        {
 
-                if ( timeout > 0 )
-                {
-                    long delta = stopWatch.getTime();
-                    log.debug( "delta {}, timeout {}", delta, timeout );
-                    if ( delta > timeout )
-                    {
-                        log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
-                        // we could not get the lock within the timeout period, so throw FileLockTimeoutException
-                        throw new FileLockTimeoutException();
-                    }
+            if ( timeout > 0 )
+            {
+                long delta = stopWatch.getTime();
+                log.debug( "delta {}, timeout {}", delta, timeout );
+                if ( delta > timeout )
+                {
+                    log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
+                    // we could not get the lock within the timeout period, so throw FileLockTimeoutException
+                    throw new FileLockTimeoutException();
                 }
+            }
 
-                lock = new Lock( file, true );
+            Lock current = lockFiles.get( file );
 
-                Lock current = lockFiles.get( file );
+            try
+            {
 
-                try
+                if ( current != null )
                 {
+                    log.debug( "write lock file exist continue wait" );
 
-                    if ( current != null )
-                    {
-                        log.debug( "write lock file exist continue wait" );
-                        // close RandomAccessFile!!!
-                        RandomAccessFile raf =  lock.getRandomAccessFile();
-                        if (raf != null)
-                        {
-                            raf.close();
-                        }
-                        continue;
-                    }
-
-                    createNewFileQuietly( file );
-                    lock.openLock( true, timeout > 0 );
-                    acquired = true;
-                }
-                catch ( FileNotFoundException e )
-                {
-                    // can happen if an other thread has deleted the file
-                    log.debug( "write Lock skip: {} try to create file", e.getMessage() );
-                    createNewFileQuietly( file );
-                }
-                catch ( IOException e )
-                {
-                    throw new FileLockException( e.getMessage(), e );
-                }
-                catch ( IllegalStateException e )
-                {
-                    log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
+                    continue;
                 }
+                lock = new Lock( file, true );
+                createNewFileQuietly( file );
+                lock.openLock( true, timeout > 0 );
+                acquired = true;
             }
+            catch ( FileNotFoundException e )
+            {
+                // can happen if an other thread has deleted the file
+                // close RandomAccessFile!!!
+                closeQuietly( lock.getRandomAccessFile() );
 
-            Lock current = lockFiles.putIfAbsent( file, lock );
-            if ( current != null )
+                log.debug( "write Lock skip: {} try to create file", e.getMessage() );
+                createNewFileQuietly( file );
+            }
+            catch ( IOException e )
+            {
+                throw new FileLockException( e.getMessage(), e );
+            }
+            catch ( IllegalStateException e )
             {
-                lock = current;
+                log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
             }
+        }
 
-            return lock;
+        Lock current = lockFiles.putIfAbsent( file, lock );
+        if ( current != null )
+        {
+            lock = current;
         }
 
-        catch (
+        return lock;
 
-            FileNotFoundException e
 
-            )
+    }
 
+    private void closeQuietly( RandomAccessFile randomAccessFile )
+    {
+        if ( randomAccessFile == null )
         {
-            throw new FileLockException( e.getMessage(), e );
+            return;
         }
 
+        try
+        {
+            randomAccessFile.close();
+        }
+        catch ( IOException e )
+        {
+            // ignore
+        }
     }
 
     private void createNewFileQuietly( File file )