1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.providers;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.IOException;
25 import java.io.UncheckedIOException;
26 import java.nio.channels.FileChannel;
27 import java.nio.file.AccessDeniedException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.nio.file.StandardOpenOption;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.concurrent.ConcurrentMap;
34
35 import org.eclipse.aether.named.support.FileLockNamedLock;
36 import org.eclipse.aether.named.support.NamedLockFactorySupport;
37 import org.eclipse.aether.named.support.NamedLockSupport;
38
39 import static org.eclipse.aether.named.support.Retry.retry;
40
41
42
43
44
45
46
47 @Singleton
48 @Named(FileLockNamedLockFactory.NAME)
49 public class FileLockNamedLockFactory extends NamedLockFactorySupport {
50 public static final String NAME = "file-lock";
51
52
53
54
55
56
57
58
59
60
61
62 public static final String SYSTEM_PROP_DELETE_LOCK_FILES = "aether.named.file-lock.deleteLockFiles";
63
64 private static final boolean DELETE_LOCK_FILES =
65 Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DELETE_LOCK_FILES, Boolean.TRUE.toString()));
66
67
68
69
70
71
72
73
74
75
76
77 public static final String SYSTEM_PROP_ATTEMPTS = "aether.named.file-lock.attempts";
78
79 private static final int ATTEMPTS = Integer.parseInt(System.getProperty(SYSTEM_PROP_ATTEMPTS, "5"));
80
81
82
83
84
85
86
87
88
89 public static final String SYSTEM_PROP_SLEEP_MILLIS = "aether.named.file-lock.sleepMillis";
90
91 private static final long SLEEP_MILLIS = Long.parseLong(System.getProperty(SYSTEM_PROP_SLEEP_MILLIS, "50"));
92
93 private final ConcurrentMap<String, FileChannel> fileChannels;
94
95 public FileLockNamedLockFactory() {
96 this.fileChannels = new ConcurrentHashMap<>();
97 }
98
99 @Override
100 protected NamedLockSupport createLock(final String name) {
101 Path path = Paths.get(name);
102 FileChannel fileChannel = fileChannels.computeIfAbsent(name, k -> {
103 try {
104 Files.createDirectories(path.getParent());
105 FileChannel channel = retry(
106 ATTEMPTS,
107 SLEEP_MILLIS,
108 () -> {
109 try {
110 if (DELETE_LOCK_FILES) {
111 return FileChannel.open(
112 path,
113 StandardOpenOption.READ,
114 StandardOpenOption.WRITE,
115 StandardOpenOption.CREATE,
116 StandardOpenOption.DELETE_ON_CLOSE);
117 } else {
118 return FileChannel.open(
119 path,
120 StandardOpenOption.READ,
121 StandardOpenOption.WRITE,
122 StandardOpenOption.CREATE);
123 }
124 } catch (AccessDeniedException e) {
125 return null;
126 }
127 },
128 null,
129 null);
130
131 if (channel == null) {
132 throw new IllegalStateException("Could not open file channel for '" + name + "' after "
133 + SYSTEM_PROP_ATTEMPTS + " attempts; giving up");
134 }
135 return channel;
136 } catch (InterruptedException e) {
137 Thread.currentThread().interrupt();
138 throw new RuntimeException("Interrupted while opening file channel for '" + name + "'", e);
139 } catch (IOException e) {
140 throw new UncheckedIOException("Failed to open file channel for '" + name + "'", e);
141 }
142 });
143 return new FileLockNamedLock(name, fileChannel, this);
144 }
145
146 @Override
147 protected void destroyLock(final String name) {
148 FileChannel fileChannel = fileChannels.remove(name);
149 if (fileChannel == null) {
150 throw new IllegalStateException("File channel expected, but does not exist: " + name);
151 }
152
153 try {
154 fileChannel.close();
155 } catch (IOException e) {
156 throw new UncheckedIOException("Failed to close file channel for '" + name + "'", e);
157 }
158 }
159 }