19
19
20
20
import java .io .InputStream ;
21
21
import java .util .Optional ;
22
+ import java .util .function .Function ;
22
23
23
24
import org .apache .geode .cache .GemFireCache ;
24
25
import org .apache .geode .cache .Region ;
27
28
import org .springframework .beans .factory .FactoryBean ;
28
29
import org .springframework .beans .factory .InitializingBean ;
29
30
import org .springframework .core .io .Resource ;
31
+ import org .springframework .data .gemfire .client .ClientRegionFactoryBean ;
30
32
import org .springframework .data .gemfire .support .AbstractFactoryBeanSupport ;
33
+ import org .springframework .data .gemfire .support .GemfireFunctions ;
34
+ import org .springframework .data .gemfire .util .SpringUtils ;
35
+ import org .springframework .lang .NonNull ;
36
+ import org .springframework .lang .Nullable ;
31
37
import org .springframework .util .Assert ;
32
38
import org .springframework .util .StringUtils ;
33
39
34
40
/**
35
- * Spring {@link FactoryBean} for looking up {@link Region Regions}.
41
+ * Spring {@link FactoryBean} used to look up or create {@link Region Regions}.
36
42
*
37
- * If lookups are disabled or the {@link Region} does not exist, an exception is thrown .
38
- *
39
- * For declaring and configuring new Regions, see {@link PeerRegionFactoryBean }.
43
+ * For declaring and configuring new {@literal client} {@link Region Regions}, see {@link ClientRegionFactoryBean} .
44
+ * For declaring and configuring new {@literal peer} {@link Region Regions}, see {@link PeerRegionFactoryBean}
45
+ * and {@link Class subclasses }.
40
46
*
41
47
* @author Costin Leau
42
48
* @author John Blum
50
56
public abstract class ResolvableRegionFactoryBean <K , V > extends AbstractFactoryBeanSupport <Region <K , V >>
51
57
implements InitializingBean {
52
58
59
+ protected static final String CREATING_REGION_LOG_MESSAGE = "Creating Region [%1$s] in Cache [%2$s]" ;
60
+ protected static final String REGION_FOUND_LOG_MESSAGE = "Found Region [%1$s] in Cache [%2$s]" ;
61
+ protected static final String REGION_NOT_FOUND_ERROR_MESSAGE = "Region [%1$s] in Cache [%2$s] not found" ;
62
+
53
63
private Boolean lookupEnabled = false ;
54
64
55
65
private GemFireCache cache ;
@@ -80,25 +90,19 @@ public void afterPropertiesSet() throws Exception {
80
90
81
91
synchronized (cache ) {
82
92
83
- setRegion (isLookupEnabled ()
84
- ? Optional .ofNullable (getParent ())
85
- .map (parentRegion -> parentRegion .<K , V >getSubregion (regionName ))
86
- .orElseGet (() -> cache .<K , V >getRegion (regionName ))
87
- : null );
93
+ setRegion (resolveRegion (cache , regionName ));
88
94
89
95
if (getRegion () != null ) {
90
- logInfo ("Found Region [%1$s] in Cache [%2$s]" , regionName , cache .getName ());
96
+ logInfo (REGION_FOUND_LOG_MESSAGE , regionName , cache .getName ());
91
97
}
92
98
else {
93
- logInfo ("Falling back to creating Region [%1$s] in Cache [%2$s]" ,
94
- regionName , cache .getName ());
95
-
99
+ logInfo (CREATING_REGION_LOG_MESSAGE , regionName , cache .getName ());
96
100
setRegion (postProcess (loadSnapshot (createRegion (cache , regionName ))));
97
101
}
98
102
}
99
103
}
100
104
101
- private GemFireCache requireCache () {
105
+ private @ NonNull GemFireCache requireCache () {
102
106
103
107
GemFireCache cache = getCache ();
104
108
@@ -107,7 +111,7 @@ private GemFireCache requireCache() {
107
111
return cache ;
108
112
}
109
113
110
- private String requireRegionName () {
114
+ @ NonNull String requireRegionName () {
111
115
112
116
String regionName = resolveRegionName ();
113
117
@@ -116,15 +120,29 @@ private String requireRegionName() {
116
120
return regionName ;
117
121
}
118
122
123
+ private @ Nullable Region <K , V > resolveRegion (@ NonNull GemFireCache cache , @ NonNull String regionName ) {
124
+
125
+ return isLookupEnabled ()
126
+ ? Optional .ofNullable (getParent ())
127
+ .<Region <K , V >>map (GemfireFunctions .getSubregionFromRegion (regionName ))
128
+ .orElseGet (GemfireFunctions .getRegionFromCache (cache , regionName ))
129
+ : null ;
130
+ }
131
+
119
132
/**
120
- * Resolves the {@link String name} of the {@link Region}.
133
+ * Resolves the configured {@link String name} of the {@link Region}.
121
134
*
122
- * @return a {@link String} containing the name of the {@link Region}.
135
+ * @return a {@link String} containing the {@literal name} of the {@link Region}.
123
136
* @see org.apache.geode.cache.Region#getName()
124
137
*/
125
138
public String resolveRegionName () {
126
- return StringUtils .hasText (this .regionName ) ? this .regionName
127
- : (StringUtils .hasText (this .name ) ? this .name : getBeanName ());
139
+
140
+ String name = this .name ;
141
+ String regionName = this .regionName ;
142
+
143
+ return StringUtils .hasText (regionName ) ? regionName
144
+ : StringUtils .hasText (name ) ? name
145
+ : getBeanName ();
128
146
}
129
147
130
148
/**
@@ -142,28 +160,32 @@ public String resolveRegionName() {
142
160
* @see org.apache.geode.cache.Region
143
161
*/
144
162
protected Region <K , V > createRegion (GemFireCache cache , String regionName ) throws Exception {
145
- throw new BeanInitializationException (
146
- String .format ("Region [%1$s] in Cache [%2$s] not found" , regionName , cache ));
163
+ throw new BeanInitializationException (String .format (REGION_NOT_FOUND_ERROR_MESSAGE , regionName , cache ));
147
164
}
148
165
149
166
/**
150
- * Loads the configured data {@link Resource snapshot} into the given {@link Region}.
167
+ * Loads data from the configured {@link Resource snapshot} into the given {@link Region}.
151
168
*
152
- * @param region {@link Region} to load.
169
+ * @param region {@link Region} to load; must not be {@literal null} .
153
170
* @return the given {@link Region}.
154
- * @throws RuntimeException if the snapshot load fails.
171
+ * @throws RuntimeException if loading the snapshot fails.
155
172
* @see org.apache.geode.cache.Region#loadSnapshot(InputStream)
173
+ * @see org.apache.geode.cache.Region
156
174
*/
157
- protected Region <K , V > loadSnapshot (Region <K , V > region ) {
175
+ protected @ NonNull Region <K , V > loadSnapshot (@ NonNull Region <K , V > region ) {
158
176
159
- Optional .ofNullable (this .snapshot ).ifPresent (snapshot -> {
160
- try {
161
- region .loadSnapshot (snapshot .getInputStream ());
162
- }
163
- catch (Exception cause ) {
164
- throw newRuntimeException (cause , "Failed to load snapshot [%s]" , snapshot );
165
- }
166
- });
177
+ Resource snapshot = this .snapshot ;
178
+
179
+ if (snapshot != null ) {
180
+
181
+ SpringUtils .VoidReturningThrowableOperation operation =
182
+ () -> region .loadSnapshot (snapshot .getInputStream ());
183
+
184
+ Function <Throwable , RuntimeException > exceptionHandler =
185
+ cause -> newRuntimeException (cause , "Failed to load snapshot [%s]" , snapshot );
186
+
187
+ SpringUtils .safeRunOperation (operation , exceptionHandler );
188
+ }
167
189
168
190
return region ;
169
191
}
@@ -198,15 +220,17 @@ public Region<K, V> getObject() throws Exception {
198
220
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
199
221
*/
200
222
@ Override
201
- @ SuppressWarnings ({ "rawtypes" , "unchecked" })
202
223
public Class <?> getObjectType () {
203
- return Optional .ofNullable (getRegion ()).map (Region ::getClass ).orElse ((Class ) Region .class );
224
+
225
+ Region <?, ?> region = getRegion ();
226
+
227
+ return region != null ? region .getClass () : Region .class ;
204
228
}
205
229
206
230
/**
207
231
* Returns a reference to the {@link GemFireCache} used to create the {@link Region}.
208
232
*
209
- * @return a reference to the {@link GemFireCache} used to create the {@link Region}..
233
+ * @return a reference to the {@link GemFireCache} used to create the {@link Region}.
210
234
* @see org.apache.geode.cache.GemFireCache
211
235
*/
212
236
public GemFireCache getCache () {
@@ -223,18 +247,18 @@ public void setCache(GemFireCache cache) {
223
247
this .cache = cache ;
224
248
}
225
249
226
- public boolean isLookupEnabled () {
227
- return Boolean .TRUE .equals (getLookupEnabled ());
228
- }
229
-
230
- public void setLookupEnabled (Boolean lookupEnabled ) {
250
+ public void setLookupEnabled (@ Nullable Boolean lookupEnabled ) {
231
251
this .lookupEnabled = lookupEnabled ;
232
252
}
233
253
234
- public Boolean getLookupEnabled () {
254
+ public @ Nullable Boolean getLookupEnabled () {
235
255
return this .lookupEnabled ;
236
256
}
237
257
258
+ public boolean isLookupEnabled () {
259
+ return Boolean .TRUE .equals (getLookupEnabled ());
260
+ }
261
+
238
262
/**
239
263
* Sets the name of the cache {@link Region} based on the bean 'name' attribute. If no {@link Region} is found
240
264
* with the given name, a new one will be created. If no name is given, the value of the 'beanName' property
@@ -244,30 +268,30 @@ public Boolean getLookupEnabled() {
244
268
* @see #setBeanName(String)
245
269
* @see org.apache.geode.cache.Region#getFullPath()
246
270
*/
247
- public void setName (String name ) {
271
+ public void setName (@ NonNull String name ) {
248
272
this .name = name ;
249
273
}
250
274
251
275
/**
252
- * Sets a reference to the parent {@link Region} to indicated this {@link FactoryBean} represents a GemFire cache
253
- * {@link Region Sub-Region}.
276
+ * Sets a reference to the parent {@link Region} making this {@link FactoryBean}
277
+ * represent a cache {@link Region Sub-Region}.
254
278
*
255
279
* @param parent reference to the parent {@link Region}.
256
280
* @see org.apache.geode.cache.Region
257
281
*/
258
- public void setParent (Region <?, ?> parent ) {
282
+ public void setParent (@ Nullable Region <?, ?> parent ) {
259
283
this .parent = parent ;
260
284
}
261
285
262
286
/**
263
- * Returns a reference to the parent {@link Region} indicating this {@link FactoryBean} represents a GemFire cache
264
- * {@link Region Sub-Region}.
287
+ * Returns a reference to the parent {@link Region} making this {@link FactoryBean}
288
+ * represent a cache {@link Region Sub-Region}.
265
289
*
266
- * @return a reference to the parent {@link Region} or {@literal null} if this {@link Region}
290
+ * @return a reference to the parent {@link Region}, or {@literal null} if this {@link Region}
267
291
* is not a {@link Region Sub-Region}.
268
292
* @see org.apache.geode.cache.Region
269
293
*/
270
- protected Region <?, ?> getParent () {
294
+ protected @ Nullable Region <?, ?> getParent () {
271
295
return this .parent ;
272
296
}
273
297
@@ -277,7 +301,7 @@ public void setParent(Region<?, ?> parent) {
277
301
* @param region reference to the resolvable {@link Region}.
278
302
* @see org.apache.geode.cache.Region
279
303
*/
280
- protected void setRegion (Region <K , V > region ) {
304
+ protected void setRegion (@ Nullable Region <K , V > region ) {
281
305
this .region = region ;
282
306
}
283
307
@@ -288,7 +312,7 @@ protected void setRegion(Region<K, V> region) {
288
312
* @return a reference to the {@link Region} resolved during lookup.
289
313
* @see org.apache.geode.cache.Region
290
314
*/
291
- public Region <K , V > getRegion () {
315
+ public @ Nullable Region <K , V > getRegion () {
292
316
return this .region ;
293
317
}
294
318
@@ -300,7 +324,7 @@ public Region<K, V> getRegion() {
300
324
* @see #setName(String)
301
325
* @see org.apache.geode.cache.Region#getName()
302
326
*/
303
- public void setRegionName (String regionName ) {
327
+ public void setRegionName (@ Nullable String regionName ) {
304
328
this .regionName = regionName ;
305
329
}
306
330
@@ -312,7 +336,7 @@ public void setRegionName(String regionName) {
312
336
* @see #setName(String)
313
337
* @param snapshot the snapshot to set
314
338
*/
315
- public void setSnapshot (Resource snapshot ) {
339
+ public void setSnapshot (@ Nullable Resource snapshot ) {
316
340
this .snapshot = snapshot ;
317
341
}
318
342
}
0 commit comments