Author: mvw
Date: 2008-09-07 09:48:50-0700
New Revision: 15684

Modified:
   
trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java

Log:
Fix for issue 5364: User preferences not migrated.

This patch was discussed and approved for 0.26 during beta period.

I hope this fixes issue 5351, too - to be investigated.

Modified: 
trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java
Url: 
http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java?view=diff&rev=15684&p1=trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java&p2=trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java&r1=15683&r2=15684
==============================================================================
--- 
trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java
    (original)
+++ 
trunk/src/argouml-app/src/org/argouml/configuration/ConfigurationProperties.java
    2008-09-07 09:48:50-0700
@@ -26,6 +26,7 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.URL;
@@ -101,49 +102,119 @@
     }
 
     /**
-     * Load the configuration from a specified location.
+     * Copy a file from source to destination.
+     * 
+     * TODO: Perhaps belongs in a utilities class of some sort.
+     * 
+     * @param source the source file to be copied
+     * @param dest the destination file
+     * @return success status flag
+     */
+    private static boolean copyFile(final File source, final File dest) {
+        try {
+            final FileInputStream fis = new FileInputStream(source);
+            final FileOutputStream fos = new FileOutputStream(dest);
+            byte[] buf = new byte[1024];
+            int i = 0;
+            while ((i = fis.read(buf)) != -1) {
+                fos.write(buf, 0, i);
+            }
+            fis.close();
+            fos.close();
+            return true;
+        } catch (final FileNotFoundException e) {
+            LOG.error("File not found while copying", e);
+            return false;
+        } catch (final IOException e) {
+            LOG.error("IO error copying file", e);
+            return false;
+        } catch (final SecurityException e) {
+            LOG.error("You are not allowed to copy these files", e);
+            return false;
+        }
+    }
+
+    /**
+     * Load the configuration from a specified location. <p>
+     * 
+     * Before version 0.25.4, ArgoUML used to store the 
+     * properties file in a different location. A user who
+     * upgrades his ArgoUML to a newer version, 
+     * would not like to loose his settings.
+     * Hence, in case a properties file does not exist 
+     * (in the new location),
+     * this code attempts to copy the file
+     * from the old location to the new location. <p>
+     * 
+     * In this upgrade case, the properties file 
+     * is copied, not moved.
+     * Rationale: see issue 5364. <p>
+     * 
+     * The meaning of the return value is not simply success
+     * in loading the properties file, 
+     * but it indicates if we may save the properties 
+     * on top of this file later.
+     * Hence, in case a properties file did not exist 
+     * (not in the new location, nor in the old location), 
+     * then a new empty file is created, 
+     * and in this case the return value is true. <p>
+     * 
+     * Returning false here would mean that no properties
+     * will be saved at all. 
      *
      * @param file  the path to load the configuration from.
      *
-     * @return true if the load was successful, false if not.
+     * @return true if the given file-location may be used 
+     * for writing the properties later.
      */
     public boolean loadFile(File file) {
         try {
-            propertyBundle.load(new FileInputStream(file));
-            LOG.info("Configuration loaded from " + file);
-            return true;
-        } catch (Exception e) {
-            if (canComplain) {
-                LOG.warn("Unable to load configuration " + file);
-            }
-            // Try to create an empty file.
-            try {
+            if (!file.exists()) {
+                // check for the older properties file and 
+                // copy it over if possible
+
                 // This is done for compatibility with previous version: 
                 // Move the argo.user.properties
                 // written before 0.25.4 to the new location, if it exists.
-                // TODO: Remove this when the next major release is done.
-                File oldFile = new File(getOldDefaultPath());
-                if (oldFile.exists()) {
-                    oldFile.renameTo(file);
-                    propertyBundle.load(new FileInputStream(file));
-                    LOG.info("Configuration moved from " 
-                            + oldFile + " to " + file);
+                final File oldFile = new File(getOldDefaultPath());
+                if (oldFile.exists() && oldFile.isFile() && oldFile.canRead() 
+                        && file.getParentFile().canWrite()) {
+                    // copy to new file and let the regular load code 
+                    // do the actual load
+                    final boolean result = copyFile(oldFile, file);
+                    if (result) {
+                        LOG.info("Configuration copied from " 
+                                + oldFile + " to " + file);
+                    } else {
+                        LOG.error("Error copying old configuration to new, "
+                             + "see previous log messages");
+                    }
+                } else {
+                    try {
+                        file.createNewFile();
+                    } catch (IOException e) {
+                        LOG.error("Could not create the properties file at: " 
+                                + file.getAbsolutePath(), e);
+                    }
                 }
-                else {
-                    file.createNewFile();
-                }
-                if (file.exists() && file.isFile()) {
-                    LOG.info("New configuration created as " + file);
-                    // Pretend we loaded the file correctly
+            }
+
+            if (file.exists() && file.isFile() && file.canRead()) {
+                try {
+                    propertyBundle.load(new FileInputStream(file));
+                    LOG.info("Configuration loaded from " + file);
                     return true;
+                } catch (final IOException e) {
+                    if (canComplain) {
+                        LOG.warn("Unable to load configuration " + file);
+                    }
+                    canComplain = false;
                 }
-            } catch (IOException e1) {
-                // Ignore an error here
-                LOG.warn("Unable to create configuration " + file, e1);
             }
-            canComplain = false;
+        } catch (final SecurityException e) {
+            LOG.error("A security exception occurred trying to load"
+                + " the configuration, check your security settings", e);
         }
-
         return false;
     }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to