OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 80
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.ui.state;
14
 package org.openconcerto.ui.state;
15
 
15
 
16
import java.io.File;
16
import java.io.File;
17
import java.io.IOException;
17
import java.io.IOException;
-
 
18
import java.security.AccessController;
-
 
19
import java.security.PrivilegedAction;
-
 
20
import java.security.PrivilegedActionException;
-
 
21
import java.security.PrivilegedExceptionAction;
18
 
22
 
-
 
23
// TODO use FilePermission just for this.configFile
19
public abstract class AbstractStateManager<T> {
24
public abstract class AbstractStateManager<T> {
20
 
25
 
21
    private File configFile;
26
    private File configFile;
22
    private final T src;
27
    private final T src;
23
 
28
 
Line 49... Line 54...
49
 
54
 
50
    public abstract void endAutoSave();
55
    public abstract void endAutoSave();
51
 
56
 
52
    public final void deleteState() {
57
    public final void deleteState() {
53
        check();
58
        check();
-
 
59
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
-
 
60
            @Override
-
 
61
            public Object run() {
54
        this.configFile.delete();
62
                getConfigFile().delete();
-
 
63
                return null;
-
 
64
            }
-
 
65
        });
55
    }
66
    }
56
 
67
 
57
    private void check() {
68
    private void check() {
58
        if (this.configFile == null)
69
        if (this.configFile == null)
59
            throw new IllegalStateException("configFile undefined.");
70
            throw new IllegalStateException("configFile undefined.");
60
    }
71
    }
61
 
72
 
62
    public final void saveState() throws IOException {
73
    public final void saveState() throws IOException {
63
        check();
74
        check();
64
        this.saveState(this.configFile);
75
        this.saveState(this.getConfigFile());
65
    }
76
    }
66
 
77
 
67
    public final void saveState(File file) throws IOException {
78
    public final void saveState(final File file) throws IOException {
68
        if (file == null) {
79
        if (file == null) {
69
            throw new IllegalArgumentException("null File specified");
80
            throw new IllegalArgumentException("null File specified");
70
        }
81
        }
-
 
82
        try {
-
 
83
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
-
 
84
                @Override
-
 
85
                public Object run() throws IOException {
71
        if (file.getParentFile() != null && !file.getParentFile().exists()) {
86
                    if (file.getParentFile() != null && !file.getParentFile().exists()) {
72
            file.getParentFile().mkdirs();
87
                        file.getParentFile().mkdirs();
73
        }
88
                    }
74
 
89
 
75
        writeState(file);
90
                    writeState(file);
-
 
91
                    return null;
-
 
92
                }
-
 
93
            });
-
 
94
        } catch (PrivilegedActionException e) {
-
 
95
            throw (IOException) e.getCause();
-
 
96
        }
76
    }
97
    }
77
 
98
 
78
    /**
99
    /**
79
     * Should actually write the file.
100
     * Should actually write the file.
80
     * 
101
     * 
Line 83... Line 104...
83
     */
104
     */
84
    protected abstract void writeState(File file) throws IOException;
105
    protected abstract void writeState(File file) throws IOException;
85
 
106
 
86
    public final boolean loadState() {
107
    public final boolean loadState() {
87
        check();
108
        check();
88
        return loadState(this.configFile);
109
        return loadState(this.getConfigFile());
89
    }
110
    }
90
 
111
 
91
    /**
112
    /**
92
     * Loads the state from the specified file.
113
     * Loads the state from the specified file.
93
     * 
114
     * 
94
     * @param file the file from which to load.
115
     * @param file the file from which to load.
95
     * @return if the state was restored.
116
     * @return if the state was restored.
96
     */
117
     */
97
    public final boolean loadState(File file) {
118
    public final boolean loadState(final File file) {
-
 
119
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
-
 
120
            @Override
-
 
121
            public Boolean run() {
98
        if (file.exists() && file.length() > 0) {
122
                if (file.exists() && file.length() > 0) {
99
            this.configFile = file;
123
                    setConfigFile(file);
100
            return this.readState(file);
124
                    return readState(file);
101
        } else {
125
                } else {
102
            return false;
126
                    return false;
103
        }
127
                }
104
    }
128
            }
-
 
129
        });
-
 
130
    }
105
 
131
 
106
    /**
132
    /**
107
     * Should actually read the file.
133
     * Should actually read the file.
108
     * 
134
     * 
109
     * @param file the file from which to load.
135
     * @param file the file from which to load.