1
1
/*
2
- * Copyright 2023 DiffPlug
2
+ * Copyright 2023-2024 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
import java .nio .file .attribute .BasicFileAttributes ;
28
28
import java .time .Duration ;
29
29
import java .util .concurrent .TimeoutException ;
30
+ import java .util .function .Supplier ;
30
31
31
32
import javax .annotation .Nonnull ;
32
33
@@ -39,13 +40,18 @@ class ShadowCopy {
39
40
40
41
private static final Logger logger = LoggerFactory .getLogger (ShadowCopy .class );
41
42
42
- private final File shadowCopyRoot ;
43
+ private final Supplier < File > shadowCopyRootSupplier ;
43
44
44
- public ShadowCopy (@ Nonnull File shadowCopyRoot ) {
45
- this .shadowCopyRoot = shadowCopyRoot ;
45
+ public ShadowCopy (@ Nonnull Supplier <File > shadowCopyRootSupplier ) {
46
+ this .shadowCopyRootSupplier = shadowCopyRootSupplier ;
47
+ }
48
+
49
+ private File shadowCopyRoot () {
50
+ File shadowCopyRoot = shadowCopyRootSupplier .get ();
46
51
if (!shadowCopyRoot .isDirectory ()) {
47
- throw new IllegalArgumentException ("Shadow copy root must be a directory: " + shadowCopyRoot );
52
+ throw new IllegalStateException ("Shadow copy root must be a directory: " + shadowCopyRoot );
48
53
}
54
+ return shadowCopyRoot ;
49
55
}
50
56
51
57
public void addEntry (String key , File orig ) {
@@ -86,17 +92,17 @@ private void cleanupReservation(String key) {
86
92
}
87
93
88
94
private Path markerFilePath (String key ) {
89
- return Paths .get (shadowCopyRoot .getAbsolutePath (), key + ".marker" );
95
+ return Paths .get (shadowCopyRoot () .getAbsolutePath (), key + ".marker" );
90
96
}
91
97
92
98
private File entry (String key , String origName ) {
93
- return Paths .get (shadowCopyRoot .getAbsolutePath (), key , origName ).toFile ();
99
+ return Paths .get (shadowCopyRoot () .getAbsolutePath (), key , origName ).toFile ();
94
100
}
95
101
96
102
private boolean reserveSubFolder (String key ) {
97
103
// put a marker file named "key".marker in "shadowCopyRoot" to make sure no other process is using it or return false if it already exists
98
104
try {
99
- Files .createFile (Paths .get (shadowCopyRoot .getAbsolutePath (), key + ".marker" ));
105
+ Files .createFile (Paths .get (shadowCopyRoot () .getAbsolutePath (), key + ".marker" ));
100
106
return true ;
101
107
} catch (FileAlreadyExistsException e ) {
102
108
return false ;
0 commit comments