aboutsummaryrefslogtreecommitdiffstats
path: root/netx/net/sourceforge/jnlp/util
diff options
context:
space:
mode:
authorAndrew Su <[email protected]>2011-04-18 17:38:31 -0400
committerAndrew Su <[email protected]>2011-04-18 17:38:31 -0400
commitd2ab6fbc6ac11d8d4684f2f855a5324548227917 (patch)
tree56a36a6682480f03a927791db26b63f6a310e9c8 /netx/net/sourceforge/jnlp/util
parent3614db44151bb47e747226e5bd49d30bb9620e96 (diff)
Changed cache to prevent jar overwriting when update happens.
Diffstat (limited to 'netx/net/sourceforge/jnlp/util')
-rw-r--r--netx/net/sourceforge/jnlp/util/FileUtils.java42
-rw-r--r--netx/net/sourceforge/jnlp/util/XDesktopEntry.java2
2 files changed, 43 insertions, 1 deletions
diff --git a/netx/net/sourceforge/jnlp/util/FileUtils.java b/netx/net/sourceforge/jnlp/util/FileUtils.java
index a682fa2..80a303a 100644
--- a/netx/net/sourceforge/jnlp/util/FileUtils.java
+++ b/netx/net/sourceforge/jnlp/util/FileUtils.java
@@ -19,7 +19,11 @@ package net.sourceforge.jnlp.util;
import static net.sourceforge.jnlp.runtime.Translator.R;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
import net.sourceforge.jnlp.runtime.JNLPRuntime;
@@ -292,4 +296,42 @@ public final class FileUtils {
}
+ /**
+ * This will return a lock to the file specified.
+ *
+ * @param path File path to file we want to lock.
+ * @param shared Specify if the lock will be a shared lock.
+ * @param allowBlock Specify if we should block when we can not get the
+ * lock. Getting a shared lock will always block.
+ * @return FileLock if we were successful in getting a lock, otherwise null.
+ * @throws FileNotFoundException If the file does not exist.
+ */
+ public static FileLock getFileLock(String path, boolean shared, boolean allowBlock) throws FileNotFoundException {
+ RandomAccessFile rafFile = new RandomAccessFile(path, "rw");
+ FileChannel fc = rafFile.getChannel();
+ FileLock lock = null;
+ try {
+ if (!shared) {
+ if (allowBlock) {
+ lock = fc.lock(0, Long.MAX_VALUE, false);
+ } else {
+ lock = fc.tryLock(0, Long.MAX_VALUE, false);
+ }
+ } else { // We want shared lock. This will block regardless if allowBlock is true or not.
+ // Test to see if we can get a shared lock.
+ lock = fc.lock(0, 1, true); // Block if a non exclusive lock is being held.
+ if (!lock.isShared()) { // This lock is an exclusive lock. Use alternate solution.
+ FileLock tempLock = null;
+ for (long pos = 1; tempLock == null && pos < Long.MAX_VALUE - 1; pos++) {
+ tempLock = fc.tryLock(pos, 1, false);
+ }
+ lock.release();
+ lock = tempLock; // Get the unique exclusive lock.
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return lock;
+ }
}
diff --git a/netx/net/sourceforge/jnlp/util/XDesktopEntry.java b/netx/net/sourceforge/jnlp/util/XDesktopEntry.java
index 6e3a7e1..b06222b 100644
--- a/netx/net/sourceforge/jnlp/util/XDesktopEntry.java
+++ b/netx/net/sourceforge/jnlp/util/XDesktopEntry.java
@@ -74,7 +74,7 @@ public class XDesktopEntry {
String cacheDir = JNLPRuntime.getConfiguration()
.getProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR);
- File cacheFile = CacheUtil.urlToPath(file.getSourceLocation(), cacheDir);
+ File cacheFile = CacheUtil.getCacheFile(file.getSourceLocation(), null);
String fileContents = "[Desktop Entry]\n";
fileContents += "Version=1.0\n";