View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.utils;
18  import java.io.IOException;
19  import java.io.File;
20  import java.util.Arrays;
21  import java.io.FileOutputStream;
22  import java.util.Enumeration;
23  import java.util.Properties;
24  import java.io.OutputStreamWriter;
25  import java.io.BufferedWriter;
26  import java.io.OutputStream;
27  import java.io.PrintWriter;
28  import java.util.Date;
29  
30  /**
31   * This class implements a SecureProperties object linked to a File object.
32   * Changes in the file will be automatically updated, and changes in the
33   * object will produce changes in the file.
34   *
35   * @author Luis Gaspart
36   * @version 1.0
37   */
38  public class SortedFileProperties extends SecureFileProperties {
39  
40    /**
41     * Default Serial UID.
42     */
43    private static final long serialVersionUID = 14376457265L;
44  
45    /**
46     * Creates a new instance.
47     *
48     * @param f        The file to link this instance to
49     * @param defaults The default values
50     */
51    public SortedFileProperties(File f, Properties defaults) {
52      super(f, defaults);
53    }
54  
55    /**
56     * Creates a new instance.
57     *
58     * @param f The file to link this instance to
59     */
60    public SortedFileProperties(File f) {
61      super(f);
62    }
63  
64    /**
65     * Propagates changes in memory to the file associated with this object.
66     */
67    @Override public synchronized void commit() {
68      FileOutputStream fos = null;
69      try {
70        fos = new FileOutputStream(f);
71        this.store(fos, header);
72        BufferedWriter wb =
73          new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
74        wb.write("completed=ok"); //Checks that the file is not corrupt
75        wb.newLine();
76        wb.close();
77      } catch (IOException e) {
78        // Do nothing here
79      } finally {
80        try {
81          fos.close();
82        } catch (Exception ignore) {
83        }
84        this.lastModified = f.lastModified();
85      }
86    }
87  
88    /**
89     * {@inheritDoc}
90     */
91    @Override
92    public synchronized void store(OutputStream out, String header) throws IOException {
93      PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "8859_1"));
94      if (header != null) {
95        writer.println("#" + header);
96      }
97      writer.println("#" + new Date().toString());
98      String[] sorted = new String[this.size()];
99      int i = 0;
100     for (Enumeration<Object> e = keys(); e.hasMoreElements();) {
101       sorted[i] = (String)e.nextElement();
102       i++;
103     }
104     Arrays.sort(sorted);
105     for (i = 0; i < sorted.length; i++) {
106       String key = sorted[i];
107       String val = (String)get(key);
108       writer.println(key + "=" + val);
109     }
110     writer.flush();
111   }
112 }