1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.ipc;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Collection;
28 import java.util.stream.Collectors;
29
30 import org.eclipse.aether.named.NamedLock;
31 import org.eclipse.aether.named.NamedLockKey;
32 import org.eclipse.aether.named.support.NamedLockFactorySupport;
33 import org.eclipse.aether.named.support.NamedLockSupport;
34 import org.eclipse.aether.util.StringDigestUtil;
35
36 import static java.util.Objects.requireNonNull;
37
38
39
40
41
42
43 @Singleton
44 @Named(IpcNamedLockFactory.NAME)
45 public class IpcNamedLockFactory extends NamedLockFactorySupport {
46 public static final String NAME = "ipc";
47
48 protected final IpcClient client;
49
50 @Inject
51 public IpcNamedLockFactory() {
52 this(Paths.get(System.getProperty("user.home")).resolve(".ipc-sync"));
53 }
54
55 public IpcNamedLockFactory(Path ipcHome) {
56 requireNonNull(ipcHome);
57 Path repository = ipcHome.resolve("repository");
58 Path logPath = ipcHome.resolve("log");
59 Path syncPath = null;
60 this.client = new IpcClient(repository, logPath, syncPath);
61 }
62
63 @Override
64 protected NamedLock doGetLock(Collection<NamedLockKey> keys) {
65 StringDigestUtil sha1 = StringDigestUtil.sha1();
66 keys.forEach(k -> sha1.update(k.name()));
67 NamedLockKey key = NamedLockKey.of(
68 sha1.digest(),
69 keys.stream()
70 .map(NamedLockKey::resources)
71 .flatMap(Collection::stream)
72 .collect(Collectors.toList()));
73 return getLockAndRefTrack(
74 key,
75 () -> new IpcNamedLock(
76 key, this, client, keys.stream().map(NamedLockKey::name).collect(Collectors.toList())));
77 }
78
79 @Override
80 protected NamedLockSupport createLock(NamedLockKey key) {
81 throw new IllegalStateException("should not get here");
82 }
83
84 @Override
85 protected void doShutdown() {
86 client.close();
87 }
88 }