Skip to content

Commit 67f92bf

Browse files
jxblumsnicoll
authored andcommitted
Polish Pivotal GemFire starter and sample
Closes gh-5439
1 parent 63a3d00 commit 67f92bf

File tree

10 files changed

+197
-105
lines changed

10 files changed

+197
-105
lines changed

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
<flyway.version>3.2.1</flyway.version>
7272
<freemarker.version>2.3.23</freemarker.version>
7373
<elasticsearch.version>2.2.0</elasticsearch.version>
74-
<gemfire.version>8.2.0</gemfire.version>
7574
<glassfish-el.version>3.0.0</glassfish-el.version>
7675
<gradle.version>1.12</gradle.version>
7776
<groovy.version>2.4.6</groovy.version>
@@ -152,6 +151,7 @@
152151
<spring-security-jwt.version>1.0.3.RELEASE</spring-security-jwt.version>
153152
<spring-security-oauth.version>2.0.9.RELEASE</spring-security-oauth.version>
154153
<spring-session.version>1.1.0.RELEASE</spring-session.version>
154+
<spring-shell.version>1.1.0.RELEASE</spring-shell.version>
155155
<spring-social.version>1.1.4.RELEASE</spring-social.version>
156156
<spring-social-facebook.version>2.0.3.RELEASE</spring-social-facebook.version>
157157
<spring-social-linkedin.version>1.0.2.RELEASE</spring-social-linkedin.version>
@@ -669,17 +669,6 @@
669669
<artifactId>jackson-module-parameter-names</artifactId>
670670
<version>${jackson.version}</version>
671671
</dependency>
672-
<dependency>
673-
<groupId>com.gemstone.gemfire</groupId>
674-
<artifactId>gemfire</artifactId>
675-
<version>${gemfire.version}</version>
676-
<exclusions>
677-
<exclusion>
678-
<groupId>org.springframework.data</groupId>
679-
<artifactId>spring-data-gemfire</artifactId>
680-
</exclusion>
681-
</exclusions>
682-
</dependency>
683672
<dependency>
684673
<groupId>com.github.ben-manes.caffeine</groupId>
685674
<artifactId>caffeine</artifactId>
@@ -2053,6 +2042,11 @@
20532042
<artifactId>spring-session-data-redis</artifactId>
20542043
<version>${spring-session.version}</version>
20552044
</dependency>
2045+
<dependency>
2046+
<groupId>org.springframework.shell</groupId>
2047+
<artifactId>spring-shell</artifactId>
2048+
<version>${spring-shell.version}</version>
2049+
</dependency>
20562050
<dependency>
20572051
<groupId>org.springframework.social</groupId>
20582052
<artifactId>spring-social-config</artifactId>

spring-boot-samples/spring-boot-sample-data-gemfire/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<version>1.4.0.BUILD-SNAPSHOT</version>
99
</parent>
1010
<artifactId>spring-boot-sample-data-gemfire</artifactId>
11-
<name>Spring Boot Data Gemfire Sample</name>
12-
<description>Spring Boot Data Gemfire Sample</description>
11+
<name>Spring Boot Data GemFire Sample</name>
12+
<description>Spring Boot Data GemFire Sample</description>
1313
<url>http://projects.spring.io/spring-boot/</url>
1414
<organization>
1515
<name>Pivotal Software, Inc.</name>
@@ -32,6 +32,11 @@
3232
<artifactId>spring-boot-starter-test</artifactId>
3333
<scope>test</scope>
3434
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.shell</groupId>
37+
<artifactId>spring-shell</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
3540
</dependencies>
3641
<build>
3742
<plugins>

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/SampleDataGemFireApplication.java

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@
1616

1717
package sample.data.gemfire;
1818

19+
import java.util.Properties;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
1922
import org.springframework.boot.SpringApplication;
2023
import org.springframework.boot.autoconfigure.SpringBootApplication;
21-
import org.springframework.context.annotation.ImportResource;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.data.gemfire.CacheFactoryBean;
27+
import org.springframework.data.gemfire.GemfireTransactionManager;
28+
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
29+
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
2230
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
2331
import org.springframework.transaction.annotation.EnableTransactionManagement;
2432

33+
import com.gemstone.gemfire.cache.Cache;
34+
import com.gemstone.gemfire.cache.RegionAttributes;
35+
36+
import sample.data.gemfire.config.SampleDataGemFireApplicationProperties;
37+
import sample.data.gemfire.domain.Gemstone;
38+
2539
/**
2640
* The GemstoneAppConfiguration class for allowing Spring Boot to pick up additional
2741
* application Spring configuration meta-data for GemFire, which must be specified in
@@ -30,13 +44,73 @@
3044
* @author John Blum
3145
*/
3246
@SpringBootApplication
33-
@ImportResource("/spring-data-gemfire-cache.xml")
3447
@EnableGemfireRepositories
3548
@EnableTransactionManagement
49+
@EnableConfigurationProperties(SampleDataGemFireApplicationProperties.class)
50+
@SuppressWarnings("unused")
3651
public class SampleDataGemFireApplication {
3752

53+
protected static final String GEMSTONES_REGION_NAME = "Gemstones";
54+
3855
public static void main(final String[] args) {
3956
SpringApplication.run(SampleDataGemFireApplication.class, args);
4057
}
4158

59+
@Autowired
60+
SampleDataGemFireApplicationProperties applicationProperties;
61+
62+
Properties gemfireProperties() {
63+
Properties gemfireProperties = new Properties();
64+
65+
gemfireProperties.setProperty("name", SampleDataGemFireApplication.class.getSimpleName());
66+
gemfireProperties.setProperty("mcast-port", "0");
67+
gemfireProperties.setProperty("locators", "");
68+
gemfireProperties.setProperty("log-level", applicationProperties.getLogLevel());
69+
70+
return gemfireProperties;
71+
}
72+
73+
@Bean
74+
CacheFactoryBean gemfireCache() {
75+
CacheFactoryBean gemfireCache = new CacheFactoryBean();
76+
77+
gemfireCache.setClose(true);
78+
gemfireCache.setProperties(gemfireProperties());
79+
80+
return gemfireCache;
81+
}
82+
83+
@Bean(name = GEMSTONES_REGION_NAME)
84+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion(Cache gemfireCache,
85+
RegionAttributes<Long, Gemstone> gemstonesRegionAttributes) {
86+
87+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion =
88+
new ReplicatedRegionFactoryBean<Long, Gemstone>();
89+
90+
gemstonesRegion.setAttributes(gemstonesRegionAttributes);
91+
gemstonesRegion.setClose(false);
92+
gemstonesRegion.setCache(gemfireCache);
93+
gemstonesRegion.setName(GEMSTONES_REGION_NAME);
94+
gemstonesRegion.setPersistent(false);
95+
96+
return gemstonesRegion;
97+
}
98+
99+
@Bean
100+
@SuppressWarnings("unchecked")
101+
RegionAttributesFactoryBean gemstonesRegionAttributes() {
102+
RegionAttributesFactoryBean gemstonesRegionAttributes =
103+
new RegionAttributesFactoryBean();
104+
105+
gemstonesRegionAttributes.setKeyConstraint(Long.class);
106+
gemstonesRegionAttributes.setValueConstraint(Gemstone.class);
107+
108+
return gemstonesRegionAttributes;
109+
}
110+
111+
@Bean
112+
GemfireTransactionManager gemfireTransactionManager(Cache gemfireCache) {
113+
return new GemfireTransactionManager(gemfireCache);
114+
}
115+
42116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2010-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.data.gemfire.config;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.util.StringUtils;
21+
22+
/**
23+
* The SampleDataGemFireApplicationProperties class...
24+
*
25+
* @author John Blum
26+
* @since 1.0.0
27+
*/
28+
@ConfigurationProperties(prefix = "sample.data.gemfire")
29+
public class SampleDataGemFireApplicationProperties {
30+
31+
protected static final String DEFAULT_LOG_LEVEL = "config";
32+
33+
private String logLevel = DEFAULT_LOG_LEVEL;
34+
35+
protected String defaultIfEmpty(String value, String defaultValue) {
36+
return (StringUtils.hasText(value) ? value : defaultValue);
37+
}
38+
39+
public String getLogLevel() {
40+
return defaultIfEmpty(this.logLevel, DEFAULT_LOG_LEVEL);
41+
}
42+
43+
public void setLogLevel(String logLevel) {
44+
this.logLevel = logLevel;
45+
}
46+
47+
}

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/domain/Gemstone.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public int hashCode() {
8989
@Override
9090
public String toString() {
9191
return String.format("{ @type = %1$s, id = %2$d, name = %3$s }",
92-
getClass().getName(), getId(), getName());
92+
getClass().getName(), getId(), getName());
9393
}
9494

9595
}

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/java/sample/data/gemfire/service/GemstoneServiceImpl.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22-
2322
import javax.annotation.PostConstruct;
2423

25-
import sample.data.gemfire.domain.Gemstone;
26-
2724
import org.springframework.beans.factory.annotation.Autowired;
2825
import org.springframework.stereotype.Service;
2926
import org.springframework.transaction.annotation.Transactional;
3027
import org.springframework.util.Assert;
3128

29+
import sample.data.gemfire.domain.Gemstone;
30+
3231
/**
3332
* The GemstoneServiceImpl class is a Service object implementing the GemstoneService
3433
* interface containing business logic and rules in addition to data services for
@@ -40,17 +39,17 @@
4039
public class GemstoneServiceImpl implements GemstoneService {
4140

4241
protected static final List<String> APPROVED_GEMS = new ArrayList<String>(
43-
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY",
44-
"SAPPHIRE", "SPINEL", "TOPAZ"));
42+
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY",
43+
"SAPPHIRE", "SPINEL", "TOPAZ"));
4544

4645
@Autowired
47-
private GemstoneRepository gemstoneRepo;
46+
@SuppressWarnings("unused")
47+
private GemstoneRepository gemstoneRepository;
4848

4949
@PostConstruct
5050
public void init() {
51-
Assert.notNull(this.gemstoneRepo,
52-
"A reference to the 'GemstoneRepository' was not properly configured!");
53-
System.out.printf("%1$s initialized!%n", getClass().getSimpleName());
51+
Assert.notNull(gemstoneRepository, "'gemstoneRepository' was not properly initialized");
52+
System.out.printf("[%1$s] initialized!%n", getClass().getSimpleName());
5453
}
5554

5655
/**
@@ -62,7 +61,7 @@ public void init() {
6261
@Override
6362
@Transactional(readOnly = true)
6463
public long count() {
65-
return this.gemstoneRepo.count();
64+
return this.gemstoneRepository.count();
6665
}
6766

6867
/**
@@ -75,8 +74,8 @@ public long count() {
7574
*/
7675
@Override
7776
@Transactional(readOnly = true)
78-
public Gemstone get(final Long id) {
79-
return this.gemstoneRepo.findOne(id);
77+
public Gemstone get(Long id) {
78+
return this.gemstoneRepository.findOne(id);
8079
}
8180

8281
/**
@@ -89,8 +88,8 @@ public Gemstone get(final Long id) {
8988
*/
9089
@Override
9190
@Transactional(readOnly = true)
92-
public Gemstone get(final String name) {
93-
return this.gemstoneRepo.findByName(name);
91+
public Gemstone get(String name) {
92+
return this.gemstoneRepository.findByName(name);
9493
}
9594

9695
/**
@@ -105,7 +104,7 @@ public Gemstone get(final String name) {
105104
@Override
106105
@Transactional(readOnly = true)
107106
public Iterable<Gemstone> list() {
108-
return this.gemstoneRepo.findAll();
107+
return this.gemstoneRepository.findAll();
109108
}
110109

111110
/**
@@ -118,37 +117,36 @@ public Iterable<Gemstone> list() {
118117
*/
119118
@Override
120119
@Transactional
121-
public Gemstone save(final Gemstone gemstone) {
120+
public Gemstone save(Gemstone gemstone) {
122121
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
123122
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");
124123

125-
// NOTE deliberately (naively) validate the Gemstone after mutating data access in
126-
// GemFire rather than before
127-
// to demonstrate transactions in GemFire.
128-
Gemstone savedGemstone = validate(this.gemstoneRepo.save(gemstone));
124+
// NOTE deliberately (& naively) validate the Gemstone after mutating data access in
125+
// GemFire rather than before to demonstrate transactions in GemFire.
126+
Gemstone savedGemstone = validate(this.gemstoneRepository.save(gemstone));
129127

130-
Assert.state(savedGemstone.equals(get(gemstone.getId())),
131-
String.format(
132-
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
133-
gemstone));
128+
Assert.state(savedGemstone.equals(get(gemstone.getId())), String.format(
129+
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
130+
gemstone));
134131

135-
System.out.printf("Saved Gemstone (%1$s)%n", savedGemstone.getName());
132+
System.out.printf("Saved Gemstone [%1$s]%n", savedGemstone.getName());
136133

137134
return gemstone;
138135
}
139136

140-
private Gemstone validate(final Gemstone gemstone) {
137+
Gemstone validate(Gemstone gemstone) {
141138
if (!APPROVED_GEMS.contains(gemstone.getName().toUpperCase())) {
142-
// NOTE if the Gemstone is not valid, blow chunks (should cause transaction to
143-
// rollback in GemFire)!
144-
System.err.printf("Illegal Gemstone (%1$s)!%n", gemstone.getName());
139+
// NOTE if the Gemstone is not valid, throw error...
140+
// Should cause transaction to rollback in GemFire!
141+
System.err.printf("Illegal Gemstone [%1$s]!%n", gemstone.getName());
145142
throw new IllegalGemstoneException(
146-
String.format("'%1$s' is not a valid Gemstone!", gemstone.getName()));
143+
String.format("[%1$s] is not a valid Gemstone!", gemstone.getName()));
147144
}
148145

149146
return gemstone;
150147
}
151148

149+
@SuppressWarnings("unused")
152150
public static final class IllegalGemstoneException extends IllegalArgumentException {
153151

154152
public IllegalGemstoneException() {

spring-boot-samples/spring-boot-sample-data-gemfire/src/main/resources/spring-data-gemfire-cache.xml

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)