1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.hazelcast;
20
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25 import java.util.concurrent.TimeUnit;
26
27 import com.hazelcast.core.HazelcastInstance;
28 import com.hazelcast.cp.ISemaphore;
29 import org.eclipse.aether.named.NamedLock;
30 import org.eclipse.aether.named.NamedLockKey;
31 import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
32 import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
33 import org.eclipse.aether.named.support.NamedLockFactorySupport;
34
35 import static java.util.Objects.requireNonNull;
36
37
38
39
40
41
42
43
44 @Deprecated
45 public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport {
46 protected final HazelcastInstance hazelcastInstance;
47
48 protected final boolean manageHazelcast;
49
50 private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider;
51
52 private final ConcurrentMap<NamedLockKey, ISemaphore> semaphores;
53
54 public HazelcastSemaphoreNamedLockFactory(
55 final HazelcastInstance hazelcastInstance,
56 final boolean manageHazelcast,
57 final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) {
58 this.hazelcastInstance = requireNonNull(hazelcastInstance);
59 this.manageHazelcast = manageHazelcast;
60 this.hazelcastSemaphoreProvider = requireNonNull(hazelcastSemaphoreProvider);
61 this.semaphores = new ConcurrentHashMap<>();
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 static void requireExplicitHazelcastConfig(
78 final String configSystemProperty, final ClassLoader classLoader, final String... configResourceNames) {
79 if (System.getProperty(configSystemProperty) != null) {
80 return;
81 }
82 for (String configResourceName : configResourceNames) {
83 if (classLoader.getResource(configResourceName) != null
84 || Files.isRegularFile(Paths.get(configResourceName))) {
85 return;
86 }
87 }
88 throw new IllegalStateException(
89 "Refusing to create a Hazelcast instance from built-in defaults (default cluster name,"
90 + " auto-discovery join, no authentication): distributed lock state guarding local"
91 + " repository writes would be modifiable by unauthenticated network peers. Provide an"
92 + " explicit Hazelcast configuration that secures or isolates the cluster, via the '"
93 + configSystemProperty + "' system property or one of "
94 + String.join(", ", configResourceNames)
95 + " on the classpath or in the working directory.");
96 }
97
98 @Override
99 protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) {
100 ISemaphore semaphore = semaphores.computeIfAbsent(
101 key, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, key));
102 return new AdaptedSemaphoreNamedLock(key, this, new HazelcastSemaphore(semaphore));
103 }
104
105 @Override
106 protected void destroyLock(final NamedLock namedLock) {
107 if (namedLock instanceof AdaptedSemaphoreNamedLock) {
108 final NamedLockKey key = namedLock.key();
109 hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, key, semaphores.remove(key));
110 }
111 }
112
113 @Override
114 protected void doShutdown() {
115 if (manageHazelcast) {
116 hazelcastInstance.shutdown();
117 }
118 }
119
120 private static final class HazelcastSemaphore implements AdaptedSemaphore {
121 private final ISemaphore semaphore;
122
123 private HazelcastSemaphore(final ISemaphore semaphore) {
124 this.semaphore = semaphore;
125 }
126
127 @Override
128 public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
129 return semaphore.tryAcquire(perms, time, unit);
130 }
131
132 @Override
133 public void release(final int perms) {
134 semaphore.release(perms);
135 }
136 }
137 }