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.
This commit is contained in:
John Blum
2019-05-03 19:14:11 -07:00
parent f6f8be8882
commit a5f1650a35
2 changed files with 32 additions and 13 deletions

View File

@@ -15,11 +15,15 @@
*/
package org.springframework.geode.security.support;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.geode.security.AuthenticationFailedException;
import org.apache.geode.security.ResourcePermission;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.support.LazyWiringDeclarableSupport;
@@ -60,16 +64,21 @@ import org.springframework.util.Assert;
* @author John Blum
* @see org.apache.geode.security.ResourcePermission
* @see org.apache.geode.security.SecurityManager
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.beans.factory.BeanFactoryAware
* @see org.springframework.beans.factory.DisposableBean
* @see org.springframework.beans.factory.annotation.Autowired
* @see org.springframework.data.gemfire.support.LazyWiringDeclarableSupport
* @since 1.0.0
*/
@SuppressWarnings("unused")
public class SecurityManagerProxy extends LazyWiringDeclarableSupport
implements org.apache.geode.security.SecurityManager, DisposableBean {
implements org.apache.geode.security.SecurityManager, DisposableBean, BeanFactoryAware {
private static final AtomicReference<SecurityManagerProxy> INSTANCE = new AtomicReference<>();
private BeanFactory beanFactory;
private org.apache.geode.security.SecurityManager securityManager;
/**
@@ -92,16 +101,19 @@ public class SecurityManagerProxy extends LazyWiringDeclarableSupport
* security operations to a Spring managed {@link org.apache.geode.security.SecurityManager} bean.
*/
public SecurityManagerProxy() {
// TODO remove init() call when GEODE-2083 (https://issues.apache.org/jira/browse/GEODE-2083) is resolved!
// NOTE: the init(:Properties) call in the constructor is less than ideal since...
// 1) it allows the *this* reference to escape, and...
// 2) it is Geode's responsibility to identify Geode Declarable objects and invoke their init(:Properties) method
// However, the init(:Properties) method invocation in the constructor is necessary to enable this Proxy to be
// identified and auto-wired in a Spring context.
INSTANCE.compareAndSet(null, this);
init(new Properties());
}
/**
* Configures a reference to the current Spring {@link BeanFactory}.
*
* @param beanFactory reference to the current Spring {@link BeanFactory}.
* @throws BeansException if this operation fails to configure the reference to the {@link BeanFactory}.
* @see org.springframework.beans.factory.BeanFactory
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
/**
@@ -159,4 +171,11 @@ public class SecurityManagerProxy extends LazyWiringDeclarableSupport
super.destroy();
INSTANCE.set(null);
}
@Override
protected BeanFactory locateBeanFactory() {
return Optional.ofNullable(this.beanFactory)
.orElseGet(() -> super.locateBeanFactory());
}
}

View File

@@ -26,11 +26,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.config.annotation.EnableSecurity;
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.config.annotation.EnableSecurityManagerProxy;
import org.springframework.geode.core.util.ObjectUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
@@ -81,8 +81,8 @@ public class SecurityManagerProxyIntegrationTests extends IntegrationTestsSuppor
}
@EnableGemFireMockObjects
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL, useBeanFactoryLocator = true)
@EnableSecurity(securityManagerClassName = "org.springframework.geode.security.support.SecurityManagerProxy")
@EnableSecurityManagerProxy
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL)
static class TestConfiguration {
@Bean