| 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.FileOutputStream; |
| 24 |
|
import java.io.IOException; |
| 25 |
|
import java.io.InputStream; |
| 26 |
|
import java.io.OutputStream; |
| 27 |
|
import java.util.concurrent.locks.ReadWriteLock; |
| 28 |
|
|
| 29 |
|
import org.apache.commons.io.IOUtils; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
@version |
| 37 |
|
@since |
| 38 |
|
|
| |
|
| 56.7% |
Uncovered Elements: 42 (97) |
Complexity: 31 |
Complexity Density: 0.53 |
|
| 39 |
|
public class FileSaveTransactionRunnable extends StartableTransactionRunnable<TransactionRunnable> |
| 40 |
|
{ |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
private final File toSave; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
private final File tempFile; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
private final File backupFile; |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
private final ReadWriteLock lock; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
private final StreamProvider provider; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
private boolean runComplete; |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
@param |
| 77 |
|
@param |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@param |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
@param |
| 84 |
|
|
| 85 |
|
@param |
| 86 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 87 |
21 |
public FileSaveTransactionRunnable(final File toSave,... |
| 88 |
|
final File tempFile, |
| 89 |
|
final File backupFile, |
| 90 |
|
final ReadWriteLock lock, |
| 91 |
|
final StreamProvider provider) |
| 92 |
|
{ |
| 93 |
21 |
this.toSave = toSave; |
| 94 |
21 |
this.tempFile = tempFile; |
| 95 |
21 |
this.backupFile = backupFile; |
| 96 |
21 |
this.lock = lock; |
| 97 |
21 |
this.provider = provider; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
@inheritDoc |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
@see |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 108 |
21 |
protected void onPreRun() throws IOException... |
| 109 |
|
{ |
| 110 |
21 |
this.lock.writeLock().lock(); |
| 111 |
21 |
this.clearTempAndBackup(); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
@inheritDoc |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
@see |
| 121 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 122 |
20 |
protected void onRun() throws Exception... |
| 123 |
|
{ |
| 124 |
20 |
if (!this.toSave.getParentFile().exists() && !this.toSave.getParentFile().mkdirs()) { |
| 125 |
0 |
throw new IOException("Could not make directory tree to place file in. " |
| 126 |
|
+ "Do you have permission to write to [" |
| 127 |
|
+ this.toSave.getAbsolutePath() + "] ?"); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
20 |
final InputStream in = this.provider.getStream(); |
| 131 |
20 |
try { |
| 132 |
20 |
final OutputStream out = new FileOutputStream(this.tempFile); |
| 133 |
20 |
try { |
| 134 |
20 |
IOUtils.copy(in, out); |
| 135 |
|
} finally { |
| 136 |
20 |
out.close(); |
| 137 |
|
} |
| 138 |
|
} finally { |
| 139 |
20 |
this.runComplete = true; |
| 140 |
20 |
in.close(); |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
@inheritDoc |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
@see |
| 152 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 153 |
18 |
protected void onCommit()... |
| 154 |
|
{ |
| 155 |
18 |
if (this.toSave.exists()) { |
| 156 |
2 |
this.toSave.renameTo(this.backupFile); |
| 157 |
|
} |
| 158 |
18 |
this.tempFile.renameTo(this.toSave); |
| 159 |
|
} |
| 160 |
|
|
| |
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 161 |
2 |
protected void onRollback()... |
| 162 |
|
{ |
| 163 |
|
|
| 164 |
2 |
if (this.runComplete) { |
| 165 |
2 |
if (this.tempFile.exists()) { |
| 166 |
2 |
this.onRollbackWithTempFile(); |
| 167 |
|
} else { |
| 168 |
0 |
this.onRollbackNoTempFile(); |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
@inheritDoc |
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
@see |
| 181 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 182 |
21 |
protected void onComplete() throws IOException... |
| 183 |
|
{ |
| 184 |
21 |
try { |
| 185 |
21 |
this.clearTempAndBackup(); |
| 186 |
|
} finally { |
| 187 |
21 |
this.lock.writeLock().unlock(); |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| |
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 9 |
Complexity Density: 0.64 |
|
| 209 |
0 |
private void onRollbackNoTempFile()... |
| 210 |
|
{ |
| 211 |
0 |
boolean isBackupFile = this.backupFile.exists(); |
| 212 |
0 |
boolean isMainFile = this.toSave.exists(); |
| 213 |
|
|
| 214 |
|
|
| 215 |
0 |
if (!isBackupFile && !isMainFile) { |
| 216 |
0 |
return; |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
|
| 220 |
0 |
if (!isBackupFile && isMainFile) { |
| 221 |
0 |
this.toSave.renameTo(this.tempFile); |
| 222 |
0 |
return; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
0 |
if (isBackupFile && !isMainFile) { |
| 227 |
0 |
this.backupFile.renameTo(this.toSave); |
| 228 |
|
|
| 229 |
0 |
return; |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
|
| 233 |
0 |
if (isBackupFile && isMainFile) { |
| 234 |
0 |
this.toSave.renameTo(this.tempFile); |
| 235 |
0 |
this.backupFile.renameTo(this.toSave); |
| 236 |
0 |
return; |
| 237 |
|
} |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| |
|
| 33.3% |
Uncovered Elements: 10 (15) |
Complexity: 6 |
Complexity Density: 0.67 |
|
| 254 |
2 |
private void onRollbackWithTempFile()... |
| 255 |
|
{ |
| 256 |
2 |
boolean isBackupFile = this.backupFile.exists(); |
| 257 |
2 |
boolean isMainFile = this.toSave.exists(); |
| 258 |
|
|
| 259 |
|
|
| 260 |
2 |
if (!isBackupFile) { |
| 261 |
2 |
return; |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
|
| 265 |
0 |
if (isBackupFile && !isMainFile) { |
| 266 |
0 |
this.backupFile.renameTo(this.toSave); |
| 267 |
0 |
return; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
|
| 271 |
0 |
if (isBackupFile && isMainFile) { |
| 272 |
0 |
throw new IllegalStateException("Tried to rollback the saving of file " |
| 273 |
|
+ this.toSave.getAbsolutePath() + " and encountered a " |
| 274 |
|
+ "backup, a temp file, and a main file. Since any existing " |
| 275 |
|
+ "main file is renamed to a temp location and the content is " |
| 276 |
|
+ "saved in the backup location and then renamed to the main " |
| 277 |
|
+ "location, the existance of all 3 at once should never " |
| 278 |
|
+ "happen."); |
| 279 |
|
} |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
@throws |
| 286 |
|
|
| |
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
| 287 |
42 |
private void clearTempAndBackup() throws IOException... |
| 288 |
|
{ |
| 289 |
42 |
if (this.tempFile.exists()) { |
| 290 |
7 |
this.tempFile.delete(); |
| 291 |
|
} |
| 292 |
42 |
if (this.tempFile.exists()) { |
| 293 |
0 |
throw new IOException("Could not remove temporary file, cannot proceed."); |
| 294 |
|
} |
| 295 |
42 |
if (this.backupFile.exists()) { |
| 296 |
7 |
this.backupFile.delete(); |
| 297 |
|
} |
| 298 |
42 |
if (this.backupFile.exists()) { |
| 299 |
0 |
throw new IOException("Could not remove backup file, cannot proceed."); |
| 300 |
|
} |
| 301 |
|
} |
| 302 |
|
} |