Skip to content

Commit a5f1650

Browse files
committed
Refactor SecurityManagerProxy class.
Remove init(:Properties) method call from constructor. Implement the o.s.beans.factory.BeanFactoryAware interface. Override the locateBeanFactory() method. Switch the test configuration to use the new @EnableSecurityManagerProxy annotation.
1 parent f6f8be8 commit a5f1650

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

spring-geode/src/main/java/org/springframework/geode/security/support/SecurityManagerProxy.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package org.springframework.geode.security.support;
1717

18+
import java.util.Optional;
1819
import java.util.Properties;
1920
import java.util.concurrent.atomic.AtomicReference;
2021

2122
import org.apache.geode.security.AuthenticationFailedException;
2223
import org.apache.geode.security.ResourcePermission;
24+
import org.springframework.beans.BeansException;
25+
import org.springframework.beans.factory.BeanFactory;
26+
import org.springframework.beans.factory.BeanFactoryAware;
2327
import org.springframework.beans.factory.DisposableBean;
2428
import org.springframework.beans.factory.annotation.Autowired;
2529
import org.springframework.data.gemfire.support.LazyWiringDeclarableSupport;
@@ -60,16 +64,21 @@
6064
* @author John Blum
6165
* @see org.apache.geode.security.ResourcePermission
6266
* @see org.apache.geode.security.SecurityManager
67+
* @see org.springframework.beans.factory.BeanFactory
68+
* @see org.springframework.beans.factory.BeanFactoryAware
69+
* @see org.springframework.beans.factory.DisposableBean
6370
* @see org.springframework.beans.factory.annotation.Autowired
6471
* @see org.springframework.data.gemfire.support.LazyWiringDeclarableSupport
6572
* @since 1.0.0
6673
*/
6774
@SuppressWarnings("unused")
6875
public class SecurityManagerProxy extends LazyWiringDeclarableSupport
69-
implements org.apache.geode.security.SecurityManager, DisposableBean {
76+
implements org.apache.geode.security.SecurityManager, DisposableBean, BeanFactoryAware {
7077

7178
private static final AtomicReference<SecurityManagerProxy> INSTANCE = new AtomicReference<>();
7279

80+
private BeanFactory beanFactory;
81+
7382
private org.apache.geode.security.SecurityManager securityManager;
7483

7584
/**
@@ -92,16 +101,19 @@ public static SecurityManagerProxy getInstance() {
92101
* security operations to a Spring managed {@link org.apache.geode.security.SecurityManager} bean.
93102
*/
94103
public SecurityManagerProxy() {
95-
96-
// TODO remove init() call when GEODE-2083 (https://issues.apache.org/jira/browse/GEODE-2083) is resolved!
97-
// NOTE: the init(:Properties) call in the constructor is less than ideal since...
98-
// 1) it allows the *this* reference to escape, and...
99-
// 2) it is Geode's responsibility to identify Geode Declarable objects and invoke their init(:Properties) method
100-
// However, the init(:Properties) method invocation in the constructor is necessary to enable this Proxy to be
101-
// identified and auto-wired in a Spring context.
102-
103104
INSTANCE.compareAndSet(null, this);
104-
init(new Properties());
105+
}
106+
107+
/**
108+
* Configures a reference to the current Spring {@link BeanFactory}.
109+
*
110+
* @param beanFactory reference to the current Spring {@link BeanFactory}.
111+
* @throws BeansException if this operation fails to configure the reference to the {@link BeanFactory}.
112+
* @see org.springframework.beans.factory.BeanFactory
113+
*/
114+
@Override
115+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
116+
this.beanFactory = beanFactory;
105117
}
106118

107119
/**
@@ -159,4 +171,11 @@ public void destroy() throws Exception {
159171
super.destroy();
160172
INSTANCE.set(null);
161173
}
174+
175+
@Override
176+
protected BeanFactory locateBeanFactory() {
177+
178+
return Optional.ofNullable(this.beanFactory)
179+
.orElseGet(() -> super.locateBeanFactory());
180+
}
162181
}

spring-geode/src/test/java/org/springframework/geode/security/support/SecurityManagerProxyIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
import org.junit.runner.RunWith;
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.context.annotation.Bean;
29-
import org.springframework.data.gemfire.config.annotation.EnableSecurity;
3029
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
3130
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
3231
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
3332
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
33+
import org.springframework.geode.config.annotation.EnableSecurityManagerProxy;
3434
import org.springframework.geode.core.util.ObjectUtils;
3535
import org.springframework.test.annotation.DirtiesContext;
3636
import org.springframework.test.context.ContextConfiguration;
@@ -81,8 +81,8 @@ public void securityManagerProxyWasConfiguredWithMockSecurityManager() {
8181
}
8282

8383
@EnableGemFireMockObjects
84-
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL, useBeanFactoryLocator = true)
85-
@EnableSecurity(securityManagerClassName = "org.springframework.geode.security.support.SecurityManagerProxy")
84+
@EnableSecurityManagerProxy
85+
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL)
8686
static class TestConfiguration {
8787

8888
@Bean

0 commit comments

Comments
 (0)