1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.mode.repository.cluster.lock.holder;
19
20 import org.apache.shardingsphere.infra.props.TypedProperties;
21 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
22 import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
23 import org.apache.shardingsphere.mode.repository.cluster.lock.creator.DistributedLockCreator;
24
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28
29
30
31 public final class DistributedLockHolder {
32
33 private final DistributedLockCreator<Object, TypedProperties<?>> creator;
34
35 private final Object client;
36
37 private final TypedProperties<?> props;
38
39 private final Map<String, DistributedLock> locks = new ConcurrentHashMap<>();
40
41 @SuppressWarnings("unchecked")
42 public DistributedLockHolder(final String type, final Object client, final TypedProperties<?> props) {
43 creator = TypedSPILoader.getService(DistributedLockCreator.class, type);
44 this.client = client;
45 this.props = props;
46 }
47
48
49
50
51
52
53
54 public DistributedLock getDistributedLock(final String lockKey) {
55 return locks.computeIfAbsent(lockKey, key -> creator.create(key, client, props));
56 }
57 }