OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.state;
15
 
174 ilm 16
import org.openconcerto.utils.FileUtils;
17
 
17 ilm 18
import java.io.File;
19
import java.io.IOException;
80 ilm 20
import java.security.AccessController;
21
import java.security.PrivilegedAction;
22
import java.security.PrivilegedActionException;
23
import java.security.PrivilegedExceptionAction;
17 ilm 24
 
80 ilm 25
// TODO use FilePermission just for this.configFile
17 ilm 26
public abstract class AbstractStateManager<T> {
27
 
28
    private File configFile;
29
    private final T src;
30
 
31
    public AbstractStateManager(T src, File f) {
32
        this(src, f, true);
33
    }
34
 
35
    public AbstractStateManager(T src, File f, boolean autosave) {
36
        this.src = src;
37
        this.configFile = f;
38
        if (autosave) {
39
            beginAutoSave();
40
        }
41
    }
42
 
43
    public final File getConfigFile() {
44
        return this.configFile;
45
    }
46
 
47
    public final void setConfigFile(File configFile) {
48
        this.configFile = configFile;
49
    }
50
 
51
    protected final T getSrc() {
52
        return this.src;
53
    }
54
 
55
    public abstract void beginAutoSave();
56
 
57
    public abstract void endAutoSave();
58
 
59
    public final void deleteState() {
60
        check();
80 ilm 61
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
62
            @Override
63
            public Object run() {
64
                getConfigFile().delete();
65
                return null;
66
            }
67
        });
17 ilm 68
    }
69
 
70
    private void check() {
71
        if (this.configFile == null)
72
            throw new IllegalStateException("configFile undefined.");
73
    }
74
 
75
    public final void saveState() throws IOException {
76
        check();
80 ilm 77
        this.saveState(this.getConfigFile());
17 ilm 78
    }
79
 
80 ilm 80
    public final void saveState(final File file) throws IOException {
17 ilm 81
        if (file == null) {
82
            throw new IllegalArgumentException("null File specified");
83
        }
80 ilm 84
        try {
85
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
86
                @Override
87
                public Object run() throws IOException {
174 ilm 88
                    FileUtils.mkParentDirs(file);
80 ilm 89
                    writeState(file);
90
                    return null;
91
                }
92
            });
93
        } catch (PrivilegedActionException e) {
94
            throw (IOException) e.getCause();
17 ilm 95
        }
96
    }
97
 
98
    /**
99
     * Should actually write the file.
100
     *
101
     * @param file the file to save.
102
     * @throws IOException if an error occurs.
103
     */
104
    protected abstract void writeState(File file) throws IOException;
105
 
106
    public final boolean loadState() {
107
        check();
80 ilm 108
        return loadState(this.getConfigFile());
17 ilm 109
    }
110
 
111
    /**
112
     * Loads the state from the specified file.
113
     *
114
     * @param file the file from which to load.
115
     * @return if the state was restored.
116
     */
80 ilm 117
    public final boolean loadState(final File file) {
118
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
119
            @Override
120
            public Boolean run() {
121
                if (file.exists() && file.length() > 0) {
122
                    setConfigFile(file);
123
                    return readState(file);
124
                } else {
125
                    return false;
126
                }
127
            }
128
        });
17 ilm 129
    }
130
 
131
    /**
132
     * Should actually read the file.
133
     *
134
     * @param file the file from which to load.
135
     * @return if the state was restored.
136
     */
137
    protected abstract boolean readState(File file);
138
 
139
}