View Javadoc

1   // Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   // This file is part of iGallery.
3   //
4   // iGallery is free software; you can redistribute it and/or modify
5   // it under the terms of the GNU General Public License as published by
6   // the Free Software Foundation; either version 2 of the License, or
7   // (at your option) any later version.
8   //
9   // iGallery is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  // GNU General Public License for more details.
13  //
14  // You should have received a copy of the GNU General Public License
15  // along with Foobar; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  package org.ninjasoft.igallery.data;
18  
19  import java.util.*;
20  import java.io.*;
21  
22  /***
23   * Class to load/save application properties.  It is mindful of the 
24   * multiuser nature of the OS and sets permissions on the saved file
25   * such that nobody else can read it (because it may contain a password).
26   * 
27   * @author enigma
28   */
29  public class ApplicationProperties extends Properties {
30      private String filename = ""; 
31      public ApplicationProperties() throws IOException {
32          super();
33          filename = System.getProperty("user.home") + "/Library/Preferences/org.ninjasoft.igallery.properties";
34          this.load();
35      }
36      
37      public void load() throws IOException {
38          File f = new File(filename);
39          InputStream in = null;
40          if (!f.exists())
41              return;
42          in = new FileInputStream(f);
43          this.load(in);
44          in.close();
45      }
46      
47      public void save() throws IOException {
48          File saveFile = new File(filename);
49          File tempFile = new File(filename + ".temp");
50          if (tempFile.exists())
51              tempFile.delete();
52          tempFile.createNewFile();
53          // Boy, is this chmod cheesy:
54          Runtime.getRuntime().exec("/bin/chmod 600 " + tempFile.getCanonicalPath());
55          OutputStream out = new FileOutputStream(tempFile, true);
56          this.store(out, "");
57          out.close();
58          saveFile.delete();
59          tempFile.renameTo(saveFile);
60          // The first chmod doesn't seem to stick...
61          Runtime.getRuntime().exec("/bin/chmod 600 " + saveFile.getCanonicalPath());
62      }
63  }