Skip to content

Commit 281834b

Browse files
committed
Review, refactor and polish abstract base Region FactoryBeans.
1 parent 9c1e80a commit 281834b

File tree

7 files changed

+258
-78
lines changed

7 files changed

+258
-78
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/ConfigurableRegionFactoryBean.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package org.springframework.data.gemfire;
1717

18-
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
19-
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeCollection;
20-
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeIterable;
21-
2218
import java.util.Arrays;
2319
import java.util.Collections;
2420
import java.util.List;
@@ -29,6 +25,8 @@
2925
import org.springframework.beans.factory.FactoryBean;
3026
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
3127
import org.springframework.data.gemfire.config.annotation.RegionConfigurer;
28+
import org.springframework.data.gemfire.util.ArrayUtils;
29+
import org.springframework.data.gemfire.util.CollectionUtils;
3230

3331
/**
3432
* {@link ConfigurableRegionFactoryBean} is an abstract base class encapsulating functionality common
@@ -53,17 +51,28 @@ public abstract class ConfigurableRegionFactoryBean<K, V> extends ResolvableRegi
5351

5452
@Override
5553
public void configure(String beanName, ClientRegionFactoryBean<?, ?> bean) {
56-
nullSafeCollection(regionConfigurers)
54+
CollectionUtils.nullSafeCollection(regionConfigurers)
5755
.forEach(regionConfigurer -> regionConfigurer.configure(beanName, bean));
5856
}
5957

6058
@Override
6159
public void configure(String beanName, PeerRegionFactoryBean<?, ?> bean) {
62-
nullSafeCollection(regionConfigurers)
60+
CollectionUtils.nullSafeCollection(regionConfigurers)
6361
.forEach(regionConfigurer -> regionConfigurer.configure(beanName, bean));
6462
}
6563
};
6664

65+
/**
66+
* Applies all {@link RegionConfigurer RegionConfigurers}.
67+
*/
68+
@Override
69+
public void afterPropertiesSet() throws Exception {
70+
71+
applyRegionConfigurers(requireRegionName());
72+
73+
super.afterPropertiesSet();
74+
}
75+
6776
/**
6877
* Returns a reference to the Composite {@link RegionConfigurer} used to apply additional configuration
6978
* to this {@link ClientRegionFactoryBean} on Spring container initialization.
@@ -85,7 +94,7 @@ protected RegionConfigurer getCompositeRegionConfigurer() {
8594
* @see #setRegionConfigurers(List)
8695
*/
8796
public void setRegionConfigurers(RegionConfigurer... regionConfigurers) {
88-
setRegionConfigurers(Arrays.asList(nullSafeArray(regionConfigurers, RegionConfigurer.class)));
97+
setRegionConfigurers(Arrays.asList(ArrayUtils.nullSafeArray(regionConfigurers, RegionConfigurer.class)));
8998
}
9099

91100
/**
@@ -128,7 +137,7 @@ protected void applyRegionConfigurers(String regionName) {
128137
* @see #applyRegionConfigurers(String, Iterable)
129138
*/
130139
protected void applyRegionConfigurers(String regionName, RegionConfigurer... regionConfigurers) {
131-
applyRegionConfigurers(regionName, Arrays.asList(nullSafeArray(regionConfigurers, RegionConfigurer.class)));
140+
applyRegionConfigurers(regionName, Arrays.asList(ArrayUtils.nullSafeArray(regionConfigurers, RegionConfigurer.class)));
132141
}
133142

134143
/**
@@ -144,11 +153,11 @@ protected void applyRegionConfigurers(String regionName, RegionConfigurer... reg
144153
protected void applyRegionConfigurers(String regionName, Iterable<RegionConfigurer> regionConfigurers) {
145154

146155
if (this instanceof ClientRegionFactoryBean) {
147-
StreamSupport.stream(nullSafeIterable(regionConfigurers).spliterator(), false)
156+
StreamSupport.stream(CollectionUtils.nullSafeIterable(regionConfigurers).spliterator(), false)
148157
.forEach(regionConfigurer -> regionConfigurer.configure(regionName, (ClientRegionFactoryBean<K, V>) this));
149158
}
150159
else if (this instanceof PeerRegionFactoryBean) {
151-
StreamSupport.stream(nullSafeIterable(regionConfigurers).spliterator(), false)
160+
StreamSupport.stream(CollectionUtils.nullSafeIterable(regionConfigurers).spliterator(), false)
152161
.forEach(regionConfigurer -> regionConfigurer.configure(regionName, (PeerRegionFactoryBean<K, V>) this));
153162
}
154163
}

spring-data-geode/src/main/java/org/springframework/data/gemfire/PeerRegionFactoryBean.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ public abstract class PeerRegionFactoryBean<K, V> extends ConfigurableRegionFact
166166
@Override
167167
protected Region<K, V> createRegion(GemFireCache gemfireCache, String regionName) {
168168

169-
applyRegionConfigurers(regionName);
170-
171169
verifyLockGrantorEligibility(getAttributes(), getScope());
172170

173171
Cache cache = resolveCache(gemfireCache);

spring-data-geode/src/main/java/org/springframework/data/gemfire/ResolvableRegionFactoryBean.java

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.InputStream;
2121
import java.util.Optional;
22+
import java.util.function.Function;
2223

2324
import org.apache.geode.cache.GemFireCache;
2425
import org.apache.geode.cache.Region;
@@ -27,16 +28,21 @@
2728
import org.springframework.beans.factory.FactoryBean;
2829
import org.springframework.beans.factory.InitializingBean;
2930
import org.springframework.core.io.Resource;
31+
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
3032
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;
3137
import org.springframework.util.Assert;
3238
import org.springframework.util.StringUtils;
3339

3440
/**
35-
* Spring {@link FactoryBean} for looking up {@link Region Regions}.
41+
* Spring {@link FactoryBean} used to look up or create {@link Region Regions}.
3642
*
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}.
4046
*
4147
* @author Costin Leau
4248
* @author John Blum
@@ -50,6 +56,10 @@
5056
public abstract class ResolvableRegionFactoryBean<K, V> extends AbstractFactoryBeanSupport<Region<K, V>>
5157
implements InitializingBean {
5258

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+
5363
private Boolean lookupEnabled = false;
5464

5565
private GemFireCache cache;
@@ -80,25 +90,19 @@ public void afterPropertiesSet() throws Exception {
8090

8191
synchronized (cache) {
8292

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));
8894

8995
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());
9197
}
9298
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());
96100
setRegion(postProcess(loadSnapshot(createRegion(cache, regionName))));
97101
}
98102
}
99103
}
100104

101-
private GemFireCache requireCache() {
105+
private @NonNull GemFireCache requireCache() {
102106

103107
GemFireCache cache = getCache();
104108

@@ -107,7 +111,7 @@ private GemFireCache requireCache() {
107111
return cache;
108112
}
109113

110-
private String requireRegionName() {
114+
@NonNull String requireRegionName() {
111115

112116
String regionName = resolveRegionName();
113117

@@ -116,15 +120,29 @@ private String requireRegionName() {
116120
return regionName;
117121
}
118122

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+
119132
/**
120-
* Resolves the {@link String name} of the {@link Region}.
133+
* Resolves the configured {@link String name} of the {@link Region}.
121134
*
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}.
123136
* @see org.apache.geode.cache.Region#getName()
124137
*/
125138
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();
128146
}
129147

130148
/**
@@ -142,28 +160,32 @@ public String resolveRegionName() {
142160
* @see org.apache.geode.cache.Region
143161
*/
144162
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));
147164
}
148165

149166
/**
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}.
151168
*
152-
* @param region {@link Region} to load.
169+
* @param region {@link Region} to load; must not be {@literal null}.
153170
* @return the given {@link Region}.
154-
* @throws RuntimeException if the snapshot load fails.
171+
* @throws RuntimeException if loading the snapshot fails.
155172
* @see org.apache.geode.cache.Region#loadSnapshot(InputStream)
173+
* @see org.apache.geode.cache.Region
156174
*/
157-
protected Region<K, V> loadSnapshot(Region<K, V> region) {
175+
protected @NonNull Region<K, V> loadSnapshot(@NonNull Region<K, V> region) {
158176

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+
}
167189

168190
return region;
169191
}
@@ -198,15 +220,17 @@ public Region<K, V> getObject() throws Exception {
198220
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
199221
*/
200222
@Override
201-
@SuppressWarnings({ "rawtypes", "unchecked" })
202223
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;
204228
}
205229

206230
/**
207231
* Returns a reference to the {@link GemFireCache} used to create the {@link Region}.
208232
*
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}.
210234
* @see org.apache.geode.cache.GemFireCache
211235
*/
212236
public GemFireCache getCache() {
@@ -223,18 +247,18 @@ public void setCache(GemFireCache cache) {
223247
this.cache = cache;
224248
}
225249

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) {
231251
this.lookupEnabled = lookupEnabled;
232252
}
233253

234-
public Boolean getLookupEnabled() {
254+
public @Nullable Boolean getLookupEnabled() {
235255
return this.lookupEnabled;
236256
}
237257

258+
public boolean isLookupEnabled() {
259+
return Boolean.TRUE.equals(getLookupEnabled());
260+
}
261+
238262
/**
239263
* Sets the name of the cache {@link Region} based on the bean 'name' attribute. If no {@link Region} is found
240264
* 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() {
244268
* @see #setBeanName(String)
245269
* @see org.apache.geode.cache.Region#getFullPath()
246270
*/
247-
public void setName(String name) {
271+
public void setName(@NonNull String name) {
248272
this.name = name;
249273
}
250274

251275
/**
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}.
254278
*
255279
* @param parent reference to the parent {@link Region}.
256280
* @see org.apache.geode.cache.Region
257281
*/
258-
public void setParent(Region<?, ?> parent) {
282+
public void setParent(@Nullable Region<?, ?> parent) {
259283
this.parent = parent;
260284
}
261285

262286
/**
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}.
265289
*
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}
267291
* is not a {@link Region Sub-Region}.
268292
* @see org.apache.geode.cache.Region
269293
*/
270-
protected Region<?, ?> getParent() {
294+
protected @Nullable Region<?, ?> getParent() {
271295
return this.parent;
272296
}
273297

@@ -277,7 +301,7 @@ public void setParent(Region<?, ?> parent) {
277301
* @param region reference to the resolvable {@link Region}.
278302
* @see org.apache.geode.cache.Region
279303
*/
280-
protected void setRegion(Region<K, V> region) {
304+
protected void setRegion(@Nullable Region<K, V> region) {
281305
this.region = region;
282306
}
283307

@@ -288,7 +312,7 @@ protected void setRegion(Region<K, V> region) {
288312
* @return a reference to the {@link Region} resolved during lookup.
289313
* @see org.apache.geode.cache.Region
290314
*/
291-
public Region<K, V> getRegion() {
315+
public @Nullable Region<K, V> getRegion() {
292316
return this.region;
293317
}
294318

@@ -300,7 +324,7 @@ public Region<K, V> getRegion() {
300324
* @see #setName(String)
301325
* @see org.apache.geode.cache.Region#getName()
302326
*/
303-
public void setRegionName(String regionName) {
327+
public void setRegionName(@Nullable String regionName) {
304328
this.regionName = regionName;
305329
}
306330

@@ -312,7 +336,7 @@ public void setRegionName(String regionName) {
312336
* @see #setName(String)
313337
* @param snapshot the snapshot to set
314338
*/
315-
public void setSnapshot(Resource snapshot) {
339+
public void setSnapshot(@Nullable Resource snapshot) {
316340
this.snapshot = snapshot;
317341
}
318342
}

0 commit comments

Comments
 (0)