-
Notifications
You must be signed in to change notification settings - Fork 251
/
HazelcastStateRepository.java
161 lines (132 loc) · 3.73 KB
/
HazelcastStateRepository.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package org.togglz.hazelcast;
import org.togglz.core.Feature;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.repository.StateRepository;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
/**
* <p>
* A state repository which stores the feature state in a Hazelcast distributed map.
* </p>
*
* <p>
* The class provides a builder which can be used to configure the Hazelcast instance and map to be used:
* </p>
*
* <pre>
* StateRepository repository = HazelcastStateRepository.newBuilder().mapName("my_map")
* .config(hazelcastConfig).build();
* </pre>
*
* @author Camiel de Vleeschauwer
*/
public class HazelcastStateRepository implements StateRepository {
protected final HazelcastInstance hazelcastInstance;
protected final Config hazelcastConfig;
protected final String mapName;
public HazelcastStateRepository(Config hazelcastConfig, String mapName)
{
this.mapName = mapName;
this.hazelcastConfig = hazelcastConfig;
hazelcastInstance=createHazelcastInstance();
}
private HazelcastStateRepository(Builder builder) {
mapName = builder.mapName;
hazelcastConfig = builder.hazelcastConfig;
hazelcastInstance=createHazelcastInstance();
}
private HazelcastInstance createHazelcastInstance() {
if (hazelcastConfig != null) {
return Hazelcast.newHazelcastInstance(hazelcastConfig);
} else {
return Hazelcast.newHazelcastInstance();
}
}
@Override
public FeatureState getFeatureState(Feature feature) {
IMap<Feature, FeatureState> map = hazelcastInstance.getMap(mapName);
FeatureState featureState = map.get(feature);
return featureState;
}
@Override
public void setFeatureState(FeatureState featureState) {
IMap<Feature, FeatureState> map = hazelcastInstance.getMap(mapName);
map.set(featureState.getFeature(), featureState);
}
/**
* Creates a new builder for creating a {@link HazelcastStateRepository}.
*
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* Creates a new builder for creating a {@link HazelcastStateRepository}.
*
* @param mapName
* the Hazelcast map name
*/
public static Builder newBuilder(String mapName) {
return new Builder(mapName);
}
/**
* Builder for a {@link HazelcastStateRepository}.
*/
public static class Builder {
private String mapName = "togglz";
private Config hazelcastConfig = null;
/**
* Creates a new builder for a {@link HazelcastStateRepository}.
*
*/
public Builder() {
}
/**
* Creates a new builder for a {@link HazelcastStateRepository}.
*
* @param mapName
* the Hazelcast map name to use for feature state store
*/
public Builder(String mapName) {
this.mapName = mapName;
}
/**
* Creates a new builder for a {@link HazelcastStateRepository}.
*
* @param hazelcastConfig
* the Hazelcast configuration {@link Config}
*/
public Builder(Config hazelcastConfig) {
this.hazelcastConfig = hazelcastConfig;
}
/**
* Sets the Hazelcast map name to use.
*
* @param mapName
* the Hazelcast map name to use for feature state store
*/
public Builder mapName(String mapName) {
this.mapName = mapName;
return this;
}
/**
* Sets the Hazelcast configuration.
*
* @param hazelcastConfig
* the Hazelcast configuration {@link Config}
*/
public Builder config(Config hazelcastConfig) {
this.hazelcastConfig = hazelcastConfig;
return this;
}
/**
* Creates a new {@link HazelcastStateRepository} using the current
* settings.
*/
public HazelcastStateRepository build() {
return new HazelcastStateRepository(this);
}
}
}