OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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