1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.util.concurrent.locks.ReadWriteLock; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
@version |
32 |
|
@since |
33 |
|
|
|
|
| 77.4% |
Uncovered Elements: 12 (53) |
Complexity: 19 |
Complexity Density: 0.66 |
|
34 |
|
public class FileDeleteTransactionRunnable extends StartableTransactionRunnable<TransactionRunnable> |
35 |
|
{ |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
private final File toDelete; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private final File backupFile; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private final ReadWriteLock lock; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private boolean preRunComplete; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
|
64 |
|
|
65 |
|
@param |
66 |
|
|
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
68 |
11 |
public FileDeleteTransactionRunnable(final File toDelete,... |
69 |
|
final File backupFile, |
70 |
|
final ReadWriteLock lock) |
71 |
|
{ |
72 |
11 |
this.toDelete = toDelete; |
73 |
11 |
this.backupFile = backupFile; |
74 |
11 |
this.lock = lock; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
@inheritDoc |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@see |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
85 |
11 |
protected void onPreRun() throws IOException... |
86 |
|
{ |
87 |
11 |
this.lock.writeLock().lock(); |
88 |
11 |
this.clearBackup(); |
89 |
11 |
this.preRunComplete = true; |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
92 |
10 |
protected void onRun() throws IOException... |
93 |
|
{ |
94 |
10 |
if (this.toDelete.exists()) { |
95 |
8 |
this.toDelete.renameTo(this.backupFile); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
@inheritDoc |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@see |
115 |
|
|
|
|
| 63.6% |
Uncovered Elements: 8 (22) |
Complexity: 10 |
Complexity Density: 0.83 |
|
116 |
2 |
protected void onRollback()... |
117 |
|
{ |
118 |
|
|
119 |
2 |
if (this.preRunComplete) { |
120 |
2 |
boolean isBackupFile = this.backupFile.exists(); |
121 |
2 |
boolean isMainFile = this.toDelete.exists(); |
122 |
|
|
123 |
|
|
124 |
2 |
if (isBackupFile && !isMainFile) { |
125 |
1 |
this.backupFile.renameTo(this.toDelete); |
126 |
1 |
return; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
1 |
if (!isBackupFile && isMainFile) { |
131 |
0 |
return; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
1 |
if (!isBackupFile && !isMainFile) { |
136 |
1 |
return; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
0 |
if (isBackupFile && isMainFile) { |
141 |
0 |
throw new IllegalStateException("Tried to rollback the deletion of file " |
142 |
|
+ this.toDelete.getAbsolutePath() + " and encountered a " |
143 |
|
+ "backup and a main file. Since the main file is renamed " |
144 |
|
+ "to a backup location before deleting, this should never " |
145 |
|
+ "happen."); |
146 |
|
} |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
@inheritDoc |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@see |
158 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
159 |
11 |
protected void onComplete() throws IOException... |
160 |
|
{ |
161 |
11 |
if (!this.preRunComplete) { |
162 |
0 |
throw new IllegalStateException("Deleting file: " + this.toDelete.getAbsolutePath() |
163 |
|
+ " onPreRun has not been called, maybe the class was extended " |
164 |
|
+ "and it was overridden?"); |
165 |
|
} |
166 |
11 |
try { |
167 |
11 |
this.clearBackup(); |
168 |
|
} finally { |
169 |
11 |
this.lock.writeLock().unlock(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@throws |
177 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
178 |
22 |
private void clearBackup() throws IOException... |
179 |
|
{ |
180 |
22 |
if (this.backupFile.exists()) { |
181 |
12 |
this.backupFile.delete(); |
182 |
|
} |
183 |
22 |
if (this.backupFile.exists()) { |
184 |
0 |
throw new IOException("Could not remove backup file, cannot proceed."); |
185 |
|
} |
186 |
|
} |
187 |
|
} |