diff options
author | Andrew Su <[email protected]> | 2011-03-17 15:10:32 -0400 |
---|---|---|
committer | Andrew Su <[email protected]> | 2011-03-17 15:10:32 -0400 |
commit | 8b14e30c2894902f33653ecac38332cb4bb84f11 (patch) | |
tree | 32c6b79f79916e6938b18fd597860dc4c7e63633 /netx/net/sourceforge | |
parent | b57ac4af342f216974001abcbf8e132f3c7d0327 (diff) |
Ensure that javaws can not clear cache while plugin or javaws is running.
Diffstat (limited to 'netx/net/sourceforge')
-rw-r--r-- | netx/net/sourceforge/jnlp/Launcher.java | 78 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/CacheUtil.java | 23 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java | 71 |
3 files changed, 83 insertions, 89 deletions
diff --git a/netx/net/sourceforge/jnlp/Launcher.java b/netx/net/sourceforge/jnlp/Launcher.java index efd3c4a..09212b8 100644 --- a/netx/net/sourceforge/jnlp/Launcher.java +++ b/netx/net/sourceforge/jnlp/Launcher.java @@ -21,15 +21,10 @@ import static net.sourceforge.jnlp.runtime.Translator.R; import java.applet.Applet; import java.awt.Container; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; -import java.nio.channels.FileChannel; -import java.nio.channels.FileLock; import java.util.LinkedList; import java.util.List; import java.util.jar.JarFile; @@ -37,7 +32,6 @@ import java.util.jar.JarFile; import net.sourceforge.jnlp.cache.CacheUtil; import net.sourceforge.jnlp.cache.ResourceTracker; import net.sourceforge.jnlp.cache.UpdatePolicy; -import net.sourceforge.jnlp.config.DeploymentConfiguration; import net.sourceforge.jnlp.runtime.AppThreadGroup; import net.sourceforge.jnlp.runtime.AppletInstance; import net.sourceforge.jnlp.runtime.ApplicationInstance; @@ -45,7 +39,6 @@ import net.sourceforge.jnlp.runtime.JNLPClassLoader; import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.services.InstanceExistsException; import net.sourceforge.jnlp.services.ServiceUtil; -import net.sourceforge.jnlp.util.FileUtils; import javax.swing.SwingUtilities; import javax.swing.text.html.parser.ParserDelegator; @@ -83,8 +76,6 @@ public class Launcher { /** If the application should call System.exit on fatal errors */ private boolean exitOnFailure = true; - /** a lock which is held to indicate that an instance of netx is running */ - private FileLock fileLock; /** * Create a launcher with the runtime's default update policy @@ -198,6 +189,8 @@ public class Launcher { public ApplicationInstance launch(JNLPFile file, Container cont) throws LaunchException { TgThread tg; + JNLPRuntime.markNetxRunning(); + //First checks whether offline-allowed tag is specified inside the jnlp //file. if (!file.getInformation().isOfflineAllowed()) { @@ -387,13 +380,6 @@ public class Launcher { if (!file.isApplication()) throw launchError(new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LNotApplication"), R("LNotApplicationInfo"))); - markNetxRunning(); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - markNetxStopped(); - } - }); - try { try { @@ -699,66 +685,6 @@ public class Launcher { } /** - * Indicate that netx is running by creating the {@link JNLPRuntime#INSTANCE_FILE} and - * acquiring a shared lock on it - */ - private void markNetxRunning() { - try { - String message = "This file is used to check if netx is running"; - - File netxRunningFile = new File(JNLPRuntime.getConfiguration() - .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE)); - if (!netxRunningFile.exists()) { - FileUtils.createParentDir(netxRunningFile); - FileUtils.createRestrictedFile(netxRunningFile, true); - FileOutputStream fos = new FileOutputStream(netxRunningFile); - try { - fos.write(message.getBytes()); - } finally { - fos.close(); - } - } - - FileInputStream is = new FileInputStream(netxRunningFile); - FileChannel channel = is.getChannel(); - fileLock = channel.tryLock(0, Long.MAX_VALUE, true); - if (fileLock != null && fileLock.isShared()) { - if (JNLPRuntime.isDebug()) { - System.out.println("Acquired shared lock on " + - netxRunningFile.toString() + " to indicate javaws is running"); - } - } else { - fileLock = null; - } - } catch (IOException e) { - e.printStackTrace(); - } - - } - - /** - * Indicate that netx is stopped by releasing the shared lock on - * {@link JNLPRuntime#INSTANCE_FILE}. - */ - private void markNetxStopped() { - if (fileLock == null) { - return; - } - try { - fileLock.release(); - fileLock.channel().close(); - fileLock = null; - if (JNLPRuntime.isDebug()) { - String file = JNLPRuntime.getConfiguration() - .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE); - System.out.println("Release shared lock on " + file); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** * Do hacks on per-application level to allow different AppContexts to work * * @see JNLPRuntime#doMainAppContextHacks diff --git a/netx/net/sourceforge/jnlp/cache/CacheUtil.java b/netx/net/sourceforge/jnlp/cache/CacheUtil.java index bb88325..4143738 100644 --- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java +++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java @@ -165,23 +165,20 @@ public class CacheUtil { try { if (otherJavawsRunning.isFile()) { FileOutputStream fis = new FileOutputStream(otherJavawsRunning); - try { - FileChannel channel = fis.getChannel(); - if (channel.tryLock() == null) { - if (JNLPRuntime.isDebug()) { - System.out.println("Other instances of netx are running"); - } - return false; - } - + + FileChannel channel = fis.getChannel(); + if (channel.tryLock() == null) { if (JNLPRuntime.isDebug()) { - System.out.println("No other instances of netx are running"); + System.out.println("Other instances of netx are running"); } - return true; + return false; + } - } finally { - fis.close(); + if (JNLPRuntime.isDebug()) { + System.out.println("No other instances of netx are running"); } + return true; + } else { if (JNLPRuntime.isDebug()) { System.out.println("No instance file found"); diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java index 7aeea90..02b6381 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java @@ -19,6 +19,8 @@ package net.sourceforge.jnlp.runtime; import java.io.*; import java.net.Authenticator; import java.net.ProxySelector; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; import java.awt.*; import java.text.*; import java.util.*; @@ -122,6 +124,9 @@ public class JNLPRuntime { /** contains the arguments passed to the jnlp runtime */ private static List<String> initialArguments; + /** a lock which is held to indicate that an instance of netx is running */ + private static FileLock fileLock; + public static final String STDERR_FILE = "java.stderr"; public static final String STDOUT_FILE = "java.stdout"; @@ -627,4 +632,70 @@ public class JNLPRuntime { return initialArguments; } + /** + * Indicate that netx is running by creating the {@link JNLPRuntime#INSTANCE_FILE} and + * acquiring a shared lock on it + */ + public synchronized static void markNetxRunning() { + if (fileLock != null) return; + try { + String message = "This file is used to check if netx is running"; + + File netxRunningFile = new File(JNLPRuntime.getConfiguration() + .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE)); + if (!netxRunningFile.exists()) { + FileUtils.createParentDir(netxRunningFile); + FileUtils.createRestrictedFile(netxRunningFile, true); + FileOutputStream fos = new FileOutputStream(netxRunningFile); + try { + fos.write(message.getBytes()); + } finally { + fos.close(); + } + } + + FileInputStream is = new FileInputStream(netxRunningFile); + FileChannel channel = is.getChannel(); + fileLock = channel.lock(0, Long.MAX_VALUE, true); + if (fileLock != null && fileLock.isShared()) { + if (JNLPRuntime.isDebug()) { + System.out.println("Acquired shared lock on " + + netxRunningFile.toString() + " to indicate javaws is running"); + } + } else { + fileLock = null; + } + } catch (IOException e) { + e.printStackTrace(); + } + + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + markNetxStopped(); + } + }); + } + + /** + * Indicate that netx is stopped by releasing the shared lock on + * {@link JNLPRuntime#INSTANCE_FILE}. + */ + private static void markNetxStopped() { + if (fileLock == null) { + return; + } + try { + fileLock.release(); + fileLock.channel().close(); + fileLock = null; + if (JNLPRuntime.isDebug()) { + String file = JNLPRuntime.getConfiguration() + .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE); + System.out.println("Release shared lock on " + file); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } |