Permalink
Browse files

try to work around internal FileChannel NPE

  • Loading branch information...
1 parent 090acd6 commit 9f4778a2a061a255f47867126dbf6bcab34db704 @harrah committed Jun 26, 2011
Showing with 10 additions and 3 deletions.
  1. +10 −3 launch/Locks.scala
View
@@ -54,13 +54,19 @@ object Locks extends xsbti.GlobalLock
}
private[this] def withFileLock[T](run: Callable[T]): T =
{
+ def withChannelRetries(retries: Int)(channel: FileChannel): T =
+ try { withChannel(channel) }
+ catch { case i: InternalLockNPE =>
+ if(retries > 0) withChannelRetries(retries - 1)(channel) else throw i
+ }
+
def withChannel(channel: FileChannel) =
{
- val freeLock = channel.tryLock
+ val freeLock = try { channel.tryLock } catch { case e: NullPointerException => throw new InternalLockNPE(e) }
if(freeLock eq null)
{
System.out.println("Waiting for lock on " + file + " to be available...");
- val lock = channel.lock
+ val lock = try { channel.lock } catch { case e: NullPointerException => throw new InternalLockNPE(e) }
try { run.call }
finally { lock.release() }
}
@@ -70,7 +76,8 @@ object Locks extends xsbti.GlobalLock
finally { freeLock.release() }
}
}
- Using(new FileOutputStream(file).getChannel)(withChannel)
+ Using(new FileOutputStream(file).getChannel)(withChannelRetries(5))
}
}
+ private[this] final class InternalLockNPE(cause: Exception) extends RuntimeException(cause)
}

0 comments on commit 9f4778a

Please sign in to comment.