|
26 | 26 |
|
27 | 27 | abstract class SetupCleanup<K> { |
28 | 28 | public void start(File keyFile, K key) throws Exception { |
29 | | - byte[] required = toBytes(key); |
30 | | - if (keyFile.exists()) { |
31 | | - byte[] actual = Files.readAllBytes(keyFile.toPath()); |
32 | | - if (Arrays.equals(actual, required)) { |
33 | | - // short-circuit if our state is already setup |
34 | | - return; |
35 | | - } else { |
36 | | - Files.delete(keyFile.toPath()); |
37 | | - @SuppressWarnings("unchecked") |
38 | | - K lastKey = (K) fromBytes(required); |
39 | | - doStop(lastKey); |
| 29 | + synchronized (key.getClass()) { |
| 30 | + byte[] required = toBytes(key); |
| 31 | + if (keyFile.exists()) { |
| 32 | + byte[] actual = Files.readAllBytes(keyFile.toPath()); |
| 33 | + if (Arrays.equals(actual, required)) { |
| 34 | + // short-circuit if our state is already setup |
| 35 | + return; |
| 36 | + } else { |
| 37 | + Files.delete(keyFile.toPath()); |
| 38 | + @SuppressWarnings("unchecked") |
| 39 | + K lastKey = (K) fromBytes(required); |
| 40 | + doStop(lastKey); |
| 41 | + } |
40 | 42 | } |
| 43 | + // write out the key |
| 44 | + doStart(key); |
| 45 | + Files.createDirectories(keyFile.toPath().getParent()); |
| 46 | + Files.write(keyFile.toPath(), required); |
41 | 47 | } |
42 | | - // write out the key |
43 | | - doStart(key); |
44 | | - Files.createDirectories(keyFile.toPath().getParent()); |
45 | | - Files.write(keyFile.toPath(), required); |
46 | 48 | } |
47 | 49 |
|
48 | 50 | protected abstract void doStart(K key) throws Exception; |
|
0 commit comments