changeset 171:4e68f6f489cd

Ensure that javaws can not clear cache while plugin or javaws is running.
author Andrew Su <asu@redhat.com>
date Thu, 17 Mar 2011 15:10:32 -0400
parents 2d359e723fef
children 466ad8570145
files ChangeLog netx/net/sourceforge/jnlp/Launcher.java netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java
diffstat 4 files changed, 100 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 16 12:01:58 2011 +0100
+++ b/ChangeLog	Thu Mar 17 15:10:32 2011 -0400
@@ -1,3 +1,19 @@
+2011-03-14  Andrew Su <asu@redhat.com>
+
+	* netx/net/sourceforge/jnlp/Launcher.java:
+	(fileLock): Removed private static field.
+	(launch): Mark NetX as running before launching apps.
+	(launchApplication): Removed call to markNetxRunning() and removed
+	shutdown hook for calling markNetxStopped().
+	(markNetxRunning): Removed method.
+	(markNetxStopped): Removed method.
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java:
+	(okToClearCache): Removed closing of channel.
+	* netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java:
+	(fileLock): New private static field.
+	(markNetxRunning): New method to indicate NetX is running.
+	(markNetxStopped): New method to indicate NetX has stopped.
+
 2011-03-16  Jiri Vanek  <jvanek@redhat.com>
 
 	* extras/net/sourceforge/jnlp/about/Main.java: removed hyperlinkUpdate 
--- a/netx/net/sourceforge/jnlp/Launcher.java	Wed Mar 16 12:01:58 2011 +0100
+++ b/netx/net/sourceforge/jnlp/Launcher.java	Thu Mar 17 15:10:32 2011 -0400
@@ -21,15 +21,10 @@
 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 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.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 @@
     /** 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 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 @@
         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 @@
     }
 
     /**
-     * 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
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Wed Mar 16 12:01:58 2011 +0100
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Thu Mar 17 15:10:32 2011 -0400
@@ -165,23 +165,20 @@
         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("Other instances of netx are running");
                     }
+                    return false;
+                }
 
-                    if (JNLPRuntime.isDebug()) {
-                        System.out.println("No other instances of netx are running");
-                    }
-                    return true;
+                if (JNLPRuntime.isDebug()) {
+                    System.out.println("No other instances of netx are running");
+                }
+                return true;
 
-                } finally {
-                    fis.close();
-                }
             } else {
                 if (JNLPRuntime.isDebug()) {
                     System.out.println("No instance file found");
--- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Wed Mar 16 12:01:58 2011 +0100
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Thu Mar 17 15:10:32 2011 -0400
@@ -19,6 +19,8 @@
 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 @@
     /** 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 @@
         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();
+        }
+    }
+
 }