/
CPSubsystemImpl.java
111 lines (95 loc) · 4.37 KB
/
CPSubsystemImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.cp.internal;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.core.IAtomicLong;
import com.hazelcast.core.IAtomicReference;
import com.hazelcast.core.ICountDownLatch;
import com.hazelcast.core.ISemaphore;
import com.hazelcast.cp.CPMember;
import com.hazelcast.cp.CPSubsystem;
import com.hazelcast.cp.CPSubsystemManagementService;
import com.hazelcast.cp.internal.datastructures.atomiclong.RaftAtomicLongService;
import com.hazelcast.cp.internal.datastructures.atomicref.RaftAtomicRefService;
import com.hazelcast.cp.internal.datastructures.countdownlatch.RaftCountDownLatchService;
import com.hazelcast.cp.internal.datastructures.lock.RaftLockService;
import com.hazelcast.cp.internal.datastructures.semaphore.RaftSemaphoreService;
import com.hazelcast.cp.internal.datastructures.spi.RaftRemoteService;
import com.hazelcast.cp.internal.session.RaftSessionService;
import com.hazelcast.cp.lock.FencedLock;
import com.hazelcast.cp.session.CPSessionManagementService;
import com.hazelcast.instance.HazelcastInstanceImpl;
import static com.hazelcast.util.Preconditions.checkNotNull;
/**
* Provides access to CP subsystem utilities
*/
public class CPSubsystemImpl implements CPSubsystem {
private final HazelcastInstanceImpl instance;
public CPSubsystemImpl(HazelcastInstanceImpl instance) {
this.instance = instance;
}
@Override
public IAtomicLong getAtomicLong(String name) {
checkNotNull(name, "Retrieving an atomic long instance with a null name is not allowed!");
RaftRemoteService service = getService(RaftAtomicLongService.SERVICE_NAME);
return service.createProxy(name);
}
@Override
public <E> IAtomicReference<E> getAtomicReference(String name) {
checkNotNull(name, "Retrieving an atomic reference instance with a null name is not allowed!");
RaftRemoteService service = getService(RaftAtomicRefService.SERVICE_NAME);
return service.createProxy(name);
}
@Override
public ICountDownLatch getCountDownLatch(String name) {
checkNotNull(name, "Retrieving a count down latch instance with a null name is not allowed!");
RaftRemoteService service = getService(RaftCountDownLatchService.SERVICE_NAME);
return service.createProxy(name);
}
@Override
public FencedLock getLock(String name) {
checkNotNull(name, "Retrieving an fenced lock instance with a null name is not allowed!");
RaftRemoteService service = getService(RaftLockService.SERVICE_NAME);
return service.createProxy(name);
}
@Override
public ISemaphore getSemaphore(String name) {
checkNotNull(name, "Retrieving a semaphore instance with a null name is not allowed!");
RaftRemoteService service = getService(RaftSemaphoreService.SERVICE_NAME);
return service.createProxy(name);
}
@Override
public CPMember getLocalCPMember() {
return getCPSubsystemManagementService().getLocalCPMember();
}
@Override
public CPSubsystemManagementService getCPSubsystemManagementService() {
if (instance.getConfig().getCPSubsystemConfig().getCPMemberCount() == 0) {
throw new HazelcastException("CP Subsystem is not enabled!");
}
return instance.node.getNodeEngine().getService(RaftService.SERVICE_NAME);
}
@Override
public CPSessionManagementService getCPSessionManagementService() {
if (instance.getConfig().getCPSubsystemConfig().getCPMemberCount() == 0) {
throw new HazelcastException("CP Subsystem is not enabled!");
}
return instance.node.getNodeEngine().getService(RaftSessionService.SERVICE_NAME);
}
private <T> T getService(String serviceName) {
return instance.node.getNodeEngine().getService(serviceName);
}
}