1 package org.eclipse.aether.named.hazelcast;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.hazelcast.core.HazelcastInstance;
23 import com.hazelcast.cp.ISemaphore;
24 import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
25 import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
26 import org.eclipse.aether.named.support.NamedLockFactorySupport;
27
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.TimeUnit;
31 import java.util.function.BiFunction;
32
33
34
35
36
37 public class HazelcastSemaphoreNamedLockFactory
38 extends NamedLockFactorySupport
39 {
40 protected static final String NAME_PREFIX = "maven:resolver:";
41
42 private final HazelcastInstance hazelcastInstance;
43
44 private final BiFunction<HazelcastInstance, String, ISemaphore> semaphoreFunction;
45
46 private final boolean destroySemaphore;
47
48 private final boolean manageHazelcast;
49
50 private final ConcurrentMap<String, ISemaphore> semaphores;
51
52 public HazelcastSemaphoreNamedLockFactory(
53 final HazelcastInstance hazelcastInstance,
54 final BiFunction<HazelcastInstance, String, ISemaphore> semaphoreFunction,
55 final boolean destroySemaphore,
56 final boolean manageHazelcast
57 )
58 {
59 this.hazelcastInstance = hazelcastInstance;
60 this.semaphoreFunction = semaphoreFunction;
61 this.destroySemaphore = destroySemaphore;
62 this.manageHazelcast = manageHazelcast;
63 this.semaphores = new ConcurrentHashMap<>();
64 }
65
66 @Override
67 protected AdaptedSemaphoreNamedLock createLock( final String name )
68 {
69 ISemaphore semaphore = semaphores.computeIfAbsent( name, k ->
70 {
71 ISemaphore result = semaphoreFunction.apply( hazelcastInstance, k );
72 result.init( Integer.MAX_VALUE );
73 return result;
74 } );
75 return new AdaptedSemaphoreNamedLock( name, this, new HazelcastSemaphore( semaphore ) );
76 }
77
78 @Override
79 public void shutdown()
80 {
81 if ( manageHazelcast )
82 {
83 hazelcastInstance.shutdown();
84 }
85 }
86
87 @Override
88 protected void destroyLock( final String name )
89 {
90 ISemaphore semaphore = semaphores.remove( name );
91 if ( destroySemaphore )
92 {
93 if ( semaphore == null )
94 {
95 throw new IllegalStateException( "Semaphore expected but does not exist: " + name );
96 }
97 semaphore.destroy();
98 }
99 }
100
101 private static final class HazelcastSemaphore implements AdaptedSemaphore
102 {
103 private final ISemaphore semaphore;
104
105 private HazelcastSemaphore( final ISemaphore semaphore )
106 {
107 this.semaphore = semaphore;
108 }
109
110 @Override
111 public boolean tryAcquire( final int perms, final long time, final TimeUnit unit )
112 throws InterruptedException
113 {
114 return semaphore.tryAcquire( perms, time, unit );
115 }
116
117 @Override
118 public void release( final int perms )
119 {
120 semaphore.release( perms );
121 }
122 }
123 }