imported properties via -props should never be overwritten
[jalview.git] / src / jalview / bin / Cache.java
index f80d729..f77c2f1 100755 (executable)
@@ -173,6 +173,8 @@ public class Cache
   /** Default file is ~/.jalview_properties */
   static String propertiesFile;
 
+  private static boolean propsAreReadOnly=false;
+
   public static void initLogger()
   {
     if (log != null)
@@ -225,16 +227,29 @@ public class Cache
     {
       propertiesFile = System.getProperty("user.home") + File.separatorChar
               + ".jalview_properties";
+    } else {
+      // don't corrupt the file we've been given.
+      propsAreReadOnly = true;
     }
 
     try
     {
       InputStream fis;
-      try {
+      try
+      {
         fis = new java.net.URL(propertiesFile).openStream();
-        System.out.println("Loading jalview properties from : "+propertiesFile);
-      } catch (Exception ex) {fis=null;}
-      if (fis==null) {
+        System.out.println("Loading jalview properties from : "
+                + propertiesFile);
+        System.out
+                .println("Disabling Jalview writing to user's local properties file.");
+        propsAreReadOnly = true;
+
+      } catch (Exception ex)
+      {
+        fis = null;
+      }
+      if (fis == null)
+      {
         fis = new FileInputStream(propertiesFile);
       }
       applicationProperties.load(fis);
@@ -424,12 +439,16 @@ public class Cache
    */
   public static String setProperty(String key, String obj)
   {
+
     try
     {
-      FileOutputStream out = new FileOutputStream(propertiesFile);
       applicationProperties.setProperty(key, obj);
-      applicationProperties.store(out, "---JalviewX Properties File---");
-      out.close();
+      if (!propsAreReadOnly)
+      {
+        FileOutputStream out = new FileOutputStream(propertiesFile);
+        applicationProperties.store(out, "---JalviewX Properties File---");
+        out.close();
+      }
     } catch (Exception ex)
     {
       System.out.println("Error setting property: " + key + " " + obj
@@ -454,14 +473,17 @@ public class Cache
    */
   public static void saveProperties()
   {
-    try
-    {
-      FileOutputStream out = new FileOutputStream(propertiesFile);
-      applicationProperties.store(out, "---JalviewX Properties File---");
-      out.close();
-    } catch (Exception ex)
+    if (!propsAreReadOnly)
     {
-      System.out.println("Error saving properties: " + ex);
+      try
+      {
+        FileOutputStream out = new FileOutputStream(propertiesFile);
+        applicationProperties.store(out, "---JalviewX Properties File---");
+        out.close();
+      } catch (Exception ex)
+      {
+        System.out.println("Error saving properties: " + ex);
+      }
     }
   }