Skip to content

Commit 9dba7bb

Browse files
committed
Enables Spring Locator-based applications configured and bootstrapped with SDG to use the GemfireBeanFactoryLocator to autowire Apache Geode components.
Closes spring-projects#620.
1 parent dbcf4ba commit 9dba7bb

File tree

7 files changed

+198
-55
lines changed

7 files changed

+198
-55
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +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.nullSafeIterable;
20-
2118
import java.util.ArrayList;
2219
import java.util.Arrays;
2320
import java.util.List;
@@ -32,8 +29,10 @@
3229
import org.springframework.beans.factory.InitializingBean;
3330
import org.springframework.data.gemfire.config.annotation.LocatorConfigurer;
3431
import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport;
32+
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
33+
import org.springframework.data.gemfire.util.ArrayUtils;
3534
import org.springframework.data.gemfire.util.CollectionUtils;
36-
import org.springframework.lang.Nullable;
35+
import org.springframework.lang.NonNull;
3736
import org.springframework.util.Assert;
3837
import org.springframework.util.StringUtils;
3938

@@ -57,8 +56,10 @@ public class LocatorFactoryBean extends AbstractFactoryBeanSupport<Locator> impl
5756
public static final int DEFAULT_PORT = 10334;
5857

5958
public static final String DEFAULT_LOG_LEVEL = GemFireProperties.LOG_LEVEL.getDefaultValueAsString();
60-
private static final String LOCATORS_PROPERTY = GemFireProperties.LOCATORS.toString();
61-
public static final String LOG_LEVEL_PROPERTY = GemFireProperties.LOG_LEVEL.toString();
59+
private static final String LOCATORS_PROPERTY = GemFireProperties.LOCATORS.getName();
60+
public static final String LOG_LEVEL_PROPERTY = GemFireProperties.LOG_LEVEL.getName();
61+
62+
private boolean useBeanFactoryLocator = false;
6263

6364
private Integer port = DEFAULT_PORT;
6465

@@ -82,16 +83,22 @@ public class LocatorFactoryBean extends AbstractFactoryBeanSupport<Locator> impl
8283

8384
@Override
8485
public void afterPropertiesSet() throws Exception {
85-
applyLocatorConfigurers(getCompositeLocatorConfigurer());
86+
87+
applyLocatorConfigurers();
88+
initializeBeanFactoryLocator();
8689
init();
8790
}
8891

92+
protected void applyLocatorConfigurers() {
93+
applyLocatorConfigurers(getCompositeLocatorConfigurer());
94+
}
95+
8996
protected void applyLocatorConfigurers(LocatorConfigurer... locatorConfigurers) {
90-
applyLocatorConfigurers(Arrays.asList(nullSafeArray(locatorConfigurers, LocatorConfigurer.class)));
97+
applyLocatorConfigurers(Arrays.asList(ArrayUtils.nullSafeArray(locatorConfigurers, LocatorConfigurer.class)));
9198
}
9299

93100
protected void applyLocatorConfigurers(Iterable<LocatorConfigurer> locatorConfigurers) {
94-
StreamSupport.stream(nullSafeIterable(locatorConfigurers).spliterator(), false)
101+
StreamSupport.stream(CollectionUtils.nullSafeIterable(locatorConfigurers).spliterator(), false)
95102
.forEach(locatorConfigurer -> locatorConfigurer.configure(getBeanName(), this));
96103
}
97104

@@ -142,6 +149,13 @@ protected LocatorLauncher.Builder configureGemfireProperties(LocatorLauncher.Bui
142149
return locatorBuilder;
143150
}
144151

152+
protected void initializeBeanFactoryLocator() {
153+
154+
if (isUseBeanFactoryLocator()) {
155+
GemfireBeanFactoryLocator.newBeanFactoryLocator(getBeanFactory(), getBeanName());
156+
}
157+
}
158+
145159
protected LocatorLauncher.Builder newLocatorLauncherBuilder() {
146160
return new LocatorLauncher.Builder();
147161
}
@@ -162,8 +176,8 @@ public LocatorLauncher getLocatorLauncher() {
162176
return this.locatorLauncher;
163177
}
164178

165-
@Nullable @Override
166-
public Locator getObject() throws Exception {
179+
@Override
180+
public @NonNull Locator getObject() throws Exception {
167181

168182
Locator locator = getLocator();
169183

@@ -172,8 +186,8 @@ public Locator getObject() throws Exception {
172186
return locator;
173187
}
174188

175-
@Nullable @Override
176-
public Class<?> getObjectType() {
189+
@Override
190+
public @NonNull Class<?> getObjectType() {
177191

178192
Locator locator = getLocator();
179193

@@ -190,15 +204,15 @@ public Optional<String> getBindAddress() {
190204
.filter(StringUtils::hasText);
191205
}
192206

193-
public LocatorConfigurer getCompositeLocatorConfigurer() {
207+
public @NonNull LocatorConfigurer getCompositeLocatorConfigurer() {
194208
return this.compositeLocatorConfigurer;
195209
}
196210

197211
public void setGemFireProperties(Properties gemfireProperties) {
198212
this.gemfireProperties = gemfireProperties;
199213
}
200214

201-
public Properties getGemFireProperties() {
215+
public @NonNull Properties getGemFireProperties() {
202216

203217
if (this.gemfireProperties == null) {
204218
this.gemfireProperties = new Properties();
@@ -218,7 +232,7 @@ public Optional<String> getHostnameForClients() {
218232
}
219233

220234
public void setLocatorConfigurers(LocatorConfigurer... locatorConfigurers) {
221-
setLocatorConfigurers(Arrays.asList(nullSafeArray(locatorConfigurers, LocatorConfigurer.class)));
235+
setLocatorConfigurers(Arrays.asList(ArrayUtils.nullSafeArray(locatorConfigurers, LocatorConfigurer.class)));
222236
}
223237

224238
public void setLocatorConfigurers(List<LocatorConfigurer> locatorConfigurers) {
@@ -263,4 +277,12 @@ public void setPort(Integer port) {
263277
public Integer getPort() {
264278
return this.port;
265279
}
280+
281+
public void setUseBeanFactoryLocator(boolean useBeanFactoryLocator) {
282+
this.useBeanFactoryLocator = useBeanFactoryLocator;
283+
}
284+
285+
public boolean isUseBeanFactoryLocator() {
286+
return this.useBeanFactoryLocator;
287+
}
266288
}

spring-data-geode/src/main/java/org/springframework/data/gemfire/config/annotation/BeanFactoryLocatorConfiguration.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.data.gemfire.CacheFactoryBean;
23+
import org.springframework.data.gemfire.LocatorFactoryBean;
2324
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
2425
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
26+
import org.springframework.lang.NonNull;
2527

2628
/**
2729
* The {@link BeanFactoryLocatorConfiguration} class extends the Spring application configuration by enabling
@@ -33,8 +35,10 @@
3335
* @see org.springframework.context.annotation.Bean
3436
* @see org.springframework.context.annotation.Configuration
3537
* @see org.springframework.data.gemfire.CacheFactoryBean
38+
* @see org.springframework.data.gemfire.LocatorFactoryBean
3639
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
3740
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer
41+
* @see org.springframework.data.gemfire.config.annotation.LocatorConfigurer
3842
* @see org.springframework.data.gemfire.config.annotation.PeerCacheConfigurer
3943
* @see org.springframework.data.gemfire.config.annotation.EnableBeanFactoryLocator
4044
* @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator
@@ -64,6 +68,10 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
6468
((CacheFactoryBean) bean).setUseBeanFactoryLocator(true);
6569
}
6670

71+
if (bean instanceof LocatorFactoryBean) {
72+
((LocatorFactoryBean) bean).setUseBeanFactoryLocator(true);
73+
}
74+
6775
return bean;
6876
}
6977
};
@@ -73,25 +81,38 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
7381
* Declares and registers a {@link ClientCacheConfigurer} bean to configure a {@link ClientCacheFactoryBean}
7482
* by setting the {@literal useBeanFactoryLocator} property to {@literal true}.
7583
*
76-
* @return a {@link ClientCacheConfigurer} used to configure and set the SDG {@link ClientCacheFactoryBean}'s
84+
* @return a {@link ClientCacheConfigurer} used to configure and set the SDG {@link ClientCacheFactoryBean}
7785
* {@literal useBeanFactoryLocator} property to {@literal true}.
7886
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer
7987
*/
8088
@Bean
81-
public ClientCacheConfigurer useBeanFactoryLocatorClientCacheConfigurer() {
89+
public @NonNull ClientCacheConfigurer useBeanFactoryLocatorClientCacheConfigurer() {
90+
return (beanName, bean) -> bean.setUseBeanFactoryLocator(true);
91+
}
92+
93+
/**
94+
* Declares and registers a {@link LocatorConfigurer} bean to configure a {@link LocatorFactoryBean}
95+
* by setting the {@literal useBeanFactoryLocator} property to {@literal true}.
96+
*
97+
* @return a {@link LocatorConfigurer} used to configure and set the SDG {@link LocatorFactoryBean}
98+
* {@literal useBeanFactoryLocator} property to {@literal true}.
99+
* @see org.springframework.data.gemfire.config.annotation.LocatorConfigurer
100+
*/
101+
@Bean
102+
public @NonNull LocatorConfigurer useBeanFactoryLocatorLocatorConfigurer() {
82103
return (beanName, bean) -> bean.setUseBeanFactoryLocator(true);
83104
}
84105

85106
/**
86107
* Declares and registers a {@link PeerCacheConfigurer} bean to configure a {@link CacheFactoryBean}
87108
* by setting the {@literal useBeanFactoryLocator} property to {@literal true}.
88109
*
89-
* @return a {@link PeerCacheConfigurer} used to configure and set the SDG {@link CacheFactoryBean}'s
110+
* @return a {@link PeerCacheConfigurer} used to configure and set the SDG {@link CacheFactoryBean}
90111
* {@literal useBeanFactoryLocator} property to {@literal true}.
91112
* @see org.springframework.data.gemfire.config.annotation.PeerCacheConfigurer
92113
*/
93114
@Bean
94-
public PeerCacheConfigurer useBeanFactoryLocatorPeerCacheConfigurer() {
115+
public @NonNull PeerCacheConfigurer useBeanFactoryLocatorPeerCacheConfigurer() {
95116
return (beanName, bean) -> bean.setUseBeanFactoryLocator(true);
96117
}
97118
}

spring-data-geode/src/main/java/org/springframework/data/gemfire/config/annotation/LocatorApplication.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
import org.apache.geode.distributed.Locator;
2727

28+
import org.springframework.beans.factory.BeanFactory;
2829
import org.springframework.context.annotation.Configuration;
2930
import org.springframework.context.annotation.Import;
31+
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
3032

3133
/**
3234
* The {@link LocatorApplication} {@link Annotation} enables a Spring Data for Apache Geode & Pivotal GemFire
@@ -90,7 +92,7 @@
9092
String logLevel() default LocatorApplicationConfiguration.DEFAULT_LOG_LEVEL;
9193

9294
/**
93-
* Configures the name of the {@link Locator} application.
95+
* Configures the {@link String name} of the {@link Locator} application.
9496
*
9597
* Defaults to {@literal SpringBasedLocatorApplication}.
9698
*
@@ -110,4 +112,15 @@
110112
*/
111113
int port() default LocatorApplicationConfiguration.DEFAULT_PORT;
112114

115+
/**
116+
* Configures the {@link Locator} application with the {@link GemfireBeanFactoryLocator} in order to look up
117+
* the Spring {@link BeanFactory} used to auto-wire, configure and initialize Apache Geode components
118+
* created in a non-Spring managed, Apache Geode context (for example: {@literal cache.xml}).
119+
*
120+
* Defaults to {@literal false}.
121+
*
122+
* Use {@literal spring.data.gemfire.use-bean-factory-locator} property in {@literal application.properties}.
123+
*/
124+
boolean useBeanFactoryLocator() default LocatorApplicationConfiguration.DEFAULT_USE_BEAN_FACTORY_LOCATOR;
125+
113126
}

0 commit comments

Comments
 (0)