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.net.URI;
27 import java.nio.channels.FileChannel;
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.NamedLock;
36 import org.eclipse.aether.named.NamedLockKey;
37 import org.eclipse.aether.named.support.FileLockNamedLock;
38 import org.eclipse.aether.named.support.NamedLockFactorySupport;
39 import org.eclipse.aether.named.support.NamedLockSupport;
40
41 import static org.eclipse.aether.named.support.Retry.retry;
42
43
44
45
46
47
48
49
50 @Singleton
51 @Named(FileLockNamedLockFactory.NAME)
52 public class FileLockNamedLockFactory extends NamedLockFactorySupport {
53 public static final String NAME = "file-lock";
54
55
56 private static final boolean IS_WINDOWS =
57 System.getProperty("os.name", "unknown").startsWith("Windows");
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static final String SYSTEM_PROP_DELETE_LOCK_FILES = "aether.named.file-lock.deleteLockFiles";
72
73 private static final boolean DELETE_LOCK_FILES =
74 Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DELETE_LOCK_FILES, Boolean.toString(!IS_WINDOWS)));
75
76
77
78
79
80
81
82
83
84
85
86 public static final String SYSTEM_PROP_ATTEMPTS = "aether.named.file-lock.attempts";
87
88 private static final int ATTEMPTS = Integer.parseInt(System.getProperty(SYSTEM_PROP_ATTEMPTS, "5"));
89
90
91
92
93
94
95
96
97
98 public static final String SYSTEM_PROP_SLEEP_MILLIS = "aether.named.file-lock.sleepMillis";
99
100 private static final long SLEEP_MILLIS = Long.parseLong(System.getProperty(SYSTEM_PROP_SLEEP_MILLIS, "50"));
101
102 private final ConcurrentMap<NamedLockKey, FileChannel> fileChannels;
103
104 public FileLockNamedLockFactory() {
105 this.fileChannels = new ConcurrentHashMap<>();
106 }
107
108 @Override
109 protected NamedLockSupport createLock(final NamedLockKey key) {
110 Path path = Paths.get(URI.create(key.name()));
111 FileChannel fileChannel = fileChannels.computeIfAbsent(key, k -> {
112 try {
113 Files.createDirectories(path.getParent());
114 FileChannel channel = retry(
115 ATTEMPTS,
116 SLEEP_MILLIS,
117 () -> {
118 if (DELETE_LOCK_FILES) {
119 return FileChannel.open(
120 path,
121 StandardOpenOption.READ,
122 StandardOpenOption.WRITE,
123 StandardOpenOption.CREATE,
124 StandardOpenOption.DELETE_ON_CLOSE);
125 } else {
126 return FileChannel.open(
127 path,
128 StandardOpenOption.READ,
129 StandardOpenOption.WRITE,
130 StandardOpenOption.CREATE);
131 }
132 },
133 null,
134 null);
135
136 if (channel == null) {
137 throw new IllegalStateException(
138 "Could not open file channel for '" + key + "' after " + ATTEMPTS + " attempts; giving up");
139 }
140 return channel;
141 } catch (InterruptedException e) {
142 Thread.currentThread().interrupt();
143 throw new RuntimeException("Interrupted while opening file channel for '" + key + "'", e);
144 } catch (IOException e) {
145 throw new UncheckedIOException("Failed to open file channel for '" + key + "'", e);
146 }
147 });
148 return new FileLockNamedLock(key, fileChannel, this);
149 }
150
151 @Override
152 protected void destroyLock(final NamedLock namedLock) {
153 if (namedLock instanceof FileLockNamedLock) {
154 final NamedLockKey key = namedLock.key();
155 FileChannel fileChannel = fileChannels.remove(key);
156 if (fileChannel == null) {
157 throw new IllegalStateException("File channel expected, but does not exist: " + key);
158 }
159
160 try {
161 fileChannel.close();
162 } catch (IOException e) {
163 throw new UncheckedIOException("Failed to close file channel for '" + key + "'", e);
164 }
165 }
166 }
167 }