Refactor Echo client/server Region and template configuration into separate Spring @Configuration classes.

This commit is contained in:
John Blum
2018-05-29 13:30:57 -07:00
parent 46f8f46cb6
commit 0990c6462d
4 changed files with 156 additions and 77 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package example.echo.config;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.util.RegionUtils;
/**
* The {@link EchoClientConfiguration} class is a Spring {@link Configuration} class used to configure
* a {@link ClientCache} {@link Region} for echo messages.
*
* @author John Blum
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.client.ClientRegionFactoryBean
* @since 1.0.0
*/
@Configuration
@SuppressWarnings("unused")
public class EchoClientConfiguration {
protected static final String REGION_NAME = "Echo";
@Bean(REGION_NAME)
public ClientRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<String, String> echoRegion = new ClientRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setClose(false);
echoRegion.setShortcut(ClientRegionShortcut.PROXY);
return echoRegion;
}
@Bean
public GemfireTemplate echoTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion(RegionUtils.toRegionPath(REGION_NAME)));
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package example.echo.config;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.util.RegionUtils;
import example.geode.cache.EchoCacheLoader;
/**
* The {@link EchoClientConfiguration} class is a Spring {@link Configuration} class used to configure
* a peer {@link Cache} {@link Region} for echo messages.
*
* @author John Blum
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.Region
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
* @see example.geode.cache.EchoCacheLoader
* @since 1.0.0
*/
@Configuration
@SuppressWarnings("unused")
public class EchoServerConfiguration {
@Bean(EchoClientConfiguration.REGION_NAME)
public PartitionedRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
PartitionedRegionFactoryBean<String, String> echoRegion = new PartitionedRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setCacheLoader(EchoCacheLoader.INSTANCE);
echoRegion.setClose(false);
echoRegion.setPersistent(false);
return echoRegion;
}
@Bean
public GemfireTemplate echoTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion(
RegionUtils.toRegionPath(EchoClientConfiguration.REGION_NAME)));
}
}

View File

@@ -29,23 +29,22 @@ import java.util.Arrays;
import java.util.Optional;
import java.util.Properties;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.security.AuthenticationFailedException;
import org.apache.geode.security.ResourcePermission;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import example.geode.cache.EchoCacheLoader;
import example.echo.config.EchoClientConfiguration;
import example.echo.config.EchoServerConfiguration;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
@@ -81,41 +80,14 @@ public abstract class AbstractAutoConfiguredSecurityContextIntegrationTests
assertThat(this.echoTemplate.<String, String>get("Good-Bye")).isEqualTo("Good-Bye");
}
protected static abstract class BaseGemFireClientConfiguration {
@Bean("Echo")
public ClientRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<String, String> echoRegion = new ClientRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setClose(false);
echoRegion.setShortcut(ClientRegionShortcut.PROXY);
return echoRegion;
}
@Bean
GemfireTemplate echoTemplate(GemFireCache gemfireCache) {
return new GemfireTemplate(gemfireCache.getRegion("/Echo"));
}
}
@Configuration
@Import(EchoClientConfiguration.class)
protected static abstract class BaseGemFireClientConfiguration { }
@Configuration
@Import(EchoServerConfiguration.class)
protected static abstract class BaseGemFireServerConfiguration {
@Bean("Echo")
public PartitionedRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
PartitionedRegionFactoryBean<String, String> echoRegion = new PartitionedRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setCacheLoader(EchoCacheLoader.INSTANCE);
echoRegion.setClose(false);
echoRegion.setPersistent(false);
return echoRegion;
}
@Bean
TestSecurityManager testSecurityManager(Environment environment) {
return new TestSecurityManager(environment);

View File

@@ -22,19 +22,16 @@ import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.context.annotation.Import;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
import org.springframework.data.gemfire.config.annotation.EnableLogging;
import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport;
@@ -42,7 +39,8 @@ import org.springframework.data.gemfire.tests.integration.config.ClientServerInt
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import example.geode.cache.EchoCacheLoader;
import example.echo.config.EchoClientConfiguration;
import example.echo.config.EchoServerConfiguration;
/**
* Integration tests testing the auto-configuration of Apache Geode/Pivotal GemFire SSL.
@@ -91,54 +89,30 @@ public class AutoConfiguredSslIntegrationTests extends ForkingClientServerIntegr
sslSystemProperties.forEach(System::clearProperty);
}
@javax.annotation.Resource(name = "Echo")
private Region<String, String> echo;
@Autowired
private GemfireTemplate echoTemplate;
@Test
public void clientServerCommunicationsSuccessful() {
assertThat(this.echo).isNotNull();
assertThat(this.echo.get("Hello")).isEqualTo("Hello");
assertThat(this.echo.get("Test")).isEqualTo("Test");
assertThat(this.echo.get("Good-Bye")).isEqualTo("Good-Bye");
assertThat(this.echoTemplate).isNotNull();
assertThat(this.echoTemplate.<String, String>get("Hello")).isEqualTo("Hello");
assertThat(this.echoTemplate.<String, String>get("Test")).isEqualTo("Test");
assertThat(this.echoTemplate.<String, String>get("Good-Bye")).isEqualTo("Good-Bye");
}
@SpringBootApplication
@EnableLogging(logLevel = GEMFIRE_LOG_LEVEL)
static class GemFireClientConfiguration extends ClientServerIntegrationTestsConfiguration {
@Bean("Echo")
public ClientRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<String, String> echoRegion = new ClientRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setClose(false);
echoRegion.setShortcut(ClientRegionShortcut.PROXY);
return echoRegion;
}
}
@Import(EchoClientConfiguration.class)
static class GemFireClientConfiguration extends ClientServerIntegrationTestsConfiguration { }
@SpringBootApplication
@CacheServerApplication(name = "AutoConfiguredSslIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL)
@Import(EchoServerConfiguration.class)
static class GemFireServerConfiguration {
public static void main(String[] args) {
SpringApplication.run(GemFireServerConfiguration.class, args);
}
@Bean("Echo")
public PartitionedRegionFactoryBean<String, String> echoRegion(GemFireCache gemfireCache) {
PartitionedRegionFactoryBean<String, String> echoRegion = new PartitionedRegionFactoryBean<>();
echoRegion.setCache(gemfireCache);
echoRegion.setCacheLoader(EchoCacheLoader.INSTANCE);
echoRegion.setClose(false);
echoRegion.setPersistent(false);
return echoRegion;
}
}
}