changeset 168:091c5e83d793

Handling error code returning functions.
author Denis Lila <dlila@redhat.com>
date Tue, 15 Mar 2011 11:11:07 -0400
parents d78bbff25267
children 6db9ffeb881b
files ChangeLog netx/net/sourceforge/jnlp/Launcher.java netx/net/sourceforge/jnlp/cache/CacheDirectory.java netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java netx/net/sourceforge/jnlp/controlpanel/CachePane.java netx/net/sourceforge/jnlp/resources/Messages.properties netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java netx/net/sourceforge/jnlp/services/XPersistenceService.java netx/net/sourceforge/jnlp/util/FileUtils.java
diffstat 10 files changed, 116 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Mar 15 10:22:27 2011 -0400
+++ b/ChangeLog	Tue Mar 15 11:11:07 2011 -0400
@@ -1,3 +1,27 @@
+2011-03-15  Denis Lila  <dlila@redhat.com>
+
+	* netx/net/sourceforge/jnlp/Launcher.java
+	(markNetxRunning): Throw exception if directories can't be created.
+	* netx/net/sourceforge/jnlp/cache/CacheDirectory.java
+	(cleanParent): Print error message if file can't be deleted.
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java
+	(getCacheFile): Throw exception if directories can't be created.
+	* netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java
+	(save): Throw exception if directories can't be created.
+	* netx/net/sourceforge/jnlp/controlpanel/CachePane.java
+	(createButtonPanel): Print error message if file can't be deleted.
+	* netx/net/sourceforge/jnlp/resources/Messages.properties
+	Added messages.
+	* netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java
+	(initializeStreams): Throw exception if directories can't be created.
+	* netx/net/sourceforge/jnlp/services/XPersistenceService.java
+	(create, get): Throw exception if directories can't be created.
+	(delete): Print error message if file can't be deleted.
+	* netx/net/sourceforge/jnlp/util/FileUtils.java
+	(createRestrictedFile): Throw exception if file permissions can't be
+	changed.
+	(createParentDir, deleteWithErrMesg): new functions.
+
 2011-03-15  Omair Majid  <omajid@redhat.com>
 
 	* Makefile.am (LAUNCHER_BOOTCLASSPATH, PLUGIN_BOOTCLASSPATH)
--- a/netx/net/sourceforge/jnlp/Launcher.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/Launcher.java	Tue Mar 15 11:11:07 2011 -0400
@@ -709,7 +709,7 @@
             File netxRunningFile = new File(JNLPRuntime.getConfiguration()
                     .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE));
             if (!netxRunningFile.exists()) {
-                netxRunningFile.getParentFile().mkdirs();
+                FileUtils.createParentDir(netxRunningFile);
                 FileUtils.createRestrictedFile(netxRunningFile, true);
                 FileOutputStream fos = new FileOutputStream(netxRunningFile);
                 try {
--- a/netx/net/sourceforge/jnlp/cache/CacheDirectory.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheDirectory.java	Tue Mar 15 11:11:07 2011 -0400
@@ -39,6 +39,8 @@
 import java.io.File;
 import java.util.ArrayList;
 
+import net.sourceforge.jnlp.util.FileUtils;
+
 public class CacheDirectory {
     /**
      * Get the structure of directory for keeping track of the protocol and
@@ -103,7 +105,7 @@
         if (parent.getParent() == null)
             return; // Don't delete the root.
         if (parent.getChildren().size() == 0) {
-            parent.getFile().delete();
+            FileUtils.deleteWithErrMesg(parent.getFile());
             parent.getParent().removeChild(parent);
             cleanParent(parent);
         }
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Tue Mar 15 11:11:07 2011 -0400
@@ -291,7 +291,7 @@
             String cacheDir = JNLPRuntime.getConfiguration()
                     .getProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR);
             File localFile = urlToPath(source, cacheDir);
-            localFile.getParentFile().mkdirs();
+            FileUtils.createParentDir(localFile);
 
             return localFile;
         } catch (Exception ex) {
--- a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java	Tue Mar 15 11:11:07 2011 -0400
@@ -38,6 +38,7 @@
 import javax.naming.ConfigurationException;
 
 import net.sourceforge.jnlp.runtime.JNLPRuntime;
+import net.sourceforge.jnlp.util.FileUtils;
 
 /**
  * Manages the various properties and configuration related to deployment.
@@ -518,7 +519,7 @@
             }
         }
 
-        userPropertiesFile.getParentFile().mkdirs();
+        FileUtils.createParentDir(userPropertiesFile);
         OutputStream out = new BufferedOutputStream(new FileOutputStream(userPropertiesFile));
         try {
             toSave.store(out, DEPLOYMENT_COMMENT);
--- a/netx/net/sourceforge/jnlp/controlpanel/CachePane.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/controlpanel/CachePane.java	Tue Mar 15 11:11:07 2011 -0400
@@ -43,6 +43,7 @@
 import net.sourceforge.jnlp.cache.DirectoryNode;
 import net.sourceforge.jnlp.config.DeploymentConfiguration;
 import net.sourceforge.jnlp.runtime.Translator;
+import net.sourceforge.jnlp.util.FileUtils;
 
 public class CachePane extends JPanel {
 
@@ -132,7 +133,7 @@
                     DirectoryNode fileNode = ((DirectoryNode) cacheTable.getModel().getValueAt(modelRow, 0));
                     if (fileNode.getFile().delete()) {
                         fileNode.getParent().removeChild(fileNode);
-                        fileNode.getInfoFile().delete();
+                        FileUtils.deleteWithErrMesg(fileNode.getInfoFile());
                         ((DefaultTableModel) cacheTable.getModel()).removeRow(modelRow);
                         cacheTable.getSelectionModel().setSelectionInterval(row, row);
                         CacheDirectory.cleanParent(fileNode);
--- a/netx/net/sourceforge/jnlp/resources/Messages.properties	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/resources/Messages.properties	Tue Mar 15 11:11:07 2011 -0400
@@ -135,6 +135,13 @@
 RExitTaken=Exit class already set and caller is not exit class.
 RCantReplaceSM=Changing the SecurityManager is not allowed.
 RCantCreateFile=Cant create file {0}
+RCantDeleteFile=Cant delete file {0}
+RRemoveRPermFailed=Removing read permission on file {0} failed
+RRemoveWPermFailed=Removing write permissions on file {0} failed
+RRemoveXPermFailed=Removing execute permissions on file {0} failed
+RGetRPermFailed=Acquiring read permissions on file {0} failed
+RGetWPermFailed=Acquiring write permissions on file {0} failed
+RGetXPermFailed=Acquiring execute permissions on file {0} failed
 RCantCreateDir=Cant create directory {0}
 RCantRename=Cant rename {0} to {0}
 RDenyStopped=Stopped applications have no permissions.
--- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Tue Mar 15 11:11:07 2011 -0400
@@ -277,10 +277,10 @@
 
             try {
                 File errFile = new File(logDir, JNLPRuntime.STDERR_FILE);
-                errFile.getParentFile().mkdirs();
+                FileUtils.createParentDir(errFile);
                 FileUtils.createRestrictedFile(errFile, true);
                 File outFile = new File(logDir, JNLPRuntime.STDOUT_FILE);
-                outFile.getParentFile().mkdirs();
+                FileUtils.createParentDir(outFile);
                 FileUtils.createRestrictedFile(outFile, true);
 
                 if (redirectStreams) {
--- a/netx/net/sourceforge/jnlp/services/XPersistenceService.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/services/XPersistenceService.java	Tue Mar 15 11:11:07 2011 -0400
@@ -93,7 +93,8 @@
         checkLocation(location);
 
         File file = toCacheFile(location);
-        file.getParentFile().mkdirs();
+        FileUtils.createParentDir(file, "Persistence store for "
+                    + location.toString());
 
         if (file.exists())
             throw new IOException("File already exists.");
@@ -110,7 +111,7 @@
     public void delete(URL location) throws MalformedURLException, IOException {
         checkLocation(location);
 
-        toCacheFile(location).delete();
+        FileUtils.deleteWithErrMesg(toCacheFile(location), " tocache");
     }
 
     /**
@@ -124,7 +125,8 @@
         if (!file.exists())
             throw new FileNotFoundException("Persistence store for "
                     + location.toString() + " is not found.");
-        file.getParentFile().mkdirs();
+        FileUtils.createParentDir(file, "Persistence store for "
+                    + location.toString());
 
         return (FileContents) ServiceUtil.createPrivilegedProxy(FileContents.class, new XFileContents(file));
     }
--- a/netx/net/sourceforge/jnlp/util/FileUtils.java	Tue Mar 15 10:22:27 2011 -0400
+++ b/netx/net/sourceforge/jnlp/util/FileUtils.java	Tue Mar 15 11:11:07 2011 -0400
@@ -96,6 +96,59 @@
     }
 
     /**
+     * Tries to create the ancestor directories of file f. Throws
+     * an IOException if it can't be created (but not if it was
+     * already there).
+     * @param f
+     * @param eMsg - the message to use for the exception. null
+     * if the file name is to be used.
+     * @throws IOException if the directory can't be created and doesn't exist.
+     */
+    public static void createParentDir(File f, String eMsg) throws IOException {
+        File parent = f.getParentFile();
+        if (!parent.isDirectory() && !parent.mkdirs()) {
+            throw new IOException(R("RCantCreateDir",
+                    eMsg == null ? parent : eMsg));
+        }
+    }
+
+    /**
+     * Tries to create the ancestor directories of file f. Throws
+     * an IOException if it can't be created (but not if it was
+     * already there).
+     * @param f
+     * @throws IOException if the directory can't be created and doesn't exist.
+     */
+    public static void createParentDir(File f) throws IOException {
+        createParentDir(f, null);
+    }
+
+    /**
+     * Tries to delete file f. If the file exists but couldn't be deleted,
+     * print an error message to stderr with the file name, or eMsg if eMsg
+     * is not null.
+     * @param f the file to be deleted
+     * @param eMsg the message to print on failure (or null to print the
+     * the file name).
+     */
+    public static void deleteWithErrMesg(File f, String eMsg) {
+        if (f.exists()) {
+            if (!f.delete()) {
+                System.err.println(R("RCantDeleteFile", eMsg == null ? f : eMsg));
+            }
+        }
+    }
+
+    /**
+     * Tries to delete file f. If the file exists but couldn't be deleted,
+     * print an error message to stderr with the file name.
+     * @param f the file to be deleted
+     */
+    public static void deleteWithErrMesg(File f) {
+        deleteWithErrMesg(f, null);
+    }
+
+    /**
      * Creates a new file or directory with minimum permissions. The file is not
      * readable or writable by anyone other than the owner. If writeableByOnwer
      * is false, even the owner can not write to it. If isDir is true, then the
@@ -120,21 +173,29 @@
         }
 
         // remove all permissions
-        tempFile.setExecutable(false, false);
-        tempFile.setReadable(false, false);
-        tempFile.setWritable(false, false);
+        if (!tempFile.setExecutable(false, false)) {
+            throw new IOException(R("RRemoveXPermFailed", tempFile));
+        }
+        if (!tempFile.setReadable(false, false)) {
+            throw new IOException(R("RRemoveRPermFailed", tempFile));
+        }
+        if (!tempFile.setWritable(false, false)) {
+            throw new IOException(R("RRemoveWPermFailed", tempFile));
+        }
 
         // allow owner to read
-        tempFile.setReadable(true, true);
+        if (!tempFile.setReadable(true, true)) {
+            throw new IOException(R("RGetRPermFailed", tempFile));
+        }
 
         // allow owner to write
-        if (writableByOwner) {
-            tempFile.setWritable(true, true);
+        if (writableByOwner && !tempFile.setWritable(true, true)) {
+            throw new IOException(R("RGetWPermFailed", tempFile));
         }
 
         // allow owner to enter directories
-        if (isDir) {
-            tempFile.setExecutable(true, true);
+        if (isDir && !tempFile.setExecutable(true, true)) {
+            throw new IOException(R("RGetXPermFailed", tempFile));
         }
 
         // rename this file. Unless the file is moved/renamed, any program that