Skip to content

Commit 9bfdad7

Browse files
committed
Merge pull request #5444 from jxblum/polish-data-gemfire-starter-and-sample
* pr/5444: Polish contribution Polish Pivotal GemFire starter and sample
2 parents 63a3d00 + d4a1365 commit 9bfdad7

File tree

10 files changed

+191
-119
lines changed

10 files changed

+191
-119
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: 13 additions & 4 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>
@@ -21,12 +21,21 @@
2121
<dependencies>
2222
<dependency>
2323
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter</artifactId>
24+
<artifactId>spring-boot-starter-data-gemfire</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.shell</groupId>
29+
<artifactId>spring-shell</artifactId>
30+
<scope>runtime</scope>
2531
</dependency>
32+
2633
<dependency>
2734
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter-data-gemfire</artifactId>
35+
<artifactId>spring-boot-configuration-processor</artifactId>
36+
<optional>true</optional>
2937
</dependency>
38+
3039
<dependency>
3140
<groupId>org.springframework.boot</groupId>
3241
<artifactId>spring-boot-starter-test</artifactId>

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

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,21 @@
1616

1717
package sample.data.gemfire;
1818

19+
import java.util.Properties;
20+
21+
import com.gemstone.gemfire.cache.Cache;
22+
import com.gemstone.gemfire.cache.RegionAttributes;
23+
import sample.data.gemfire.config.SampleDataGemFireProperties;
24+
import sample.data.gemfire.domain.Gemstone;
25+
1926
import org.springframework.boot.SpringApplication;
2027
import org.springframework.boot.autoconfigure.SpringBootApplication;
21-
import org.springframework.context.annotation.ImportResource;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.data.gemfire.CacheFactoryBean;
31+
import org.springframework.data.gemfire.GemfireTransactionManager;
32+
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
33+
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
2234
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
2335
import org.springframework.transaction.annotation.EnableTransactionManagement;
2436

@@ -30,13 +42,76 @@
3042
* @author John Blum
3143
*/
3244
@SpringBootApplication
33-
@ImportResource("/spring-data-gemfire-cache.xml")
3445
@EnableGemfireRepositories
3546
@EnableTransactionManagement
47+
@EnableConfigurationProperties(SampleDataGemFireProperties.class)
3648
public class SampleDataGemFireApplication {
3749

50+
protected static final String GEMSTONES_REGION_NAME = "Gemstones";
51+
52+
private final SampleDataGemFireProperties applicationProperties;
53+
54+
public SampleDataGemFireApplication(SampleDataGemFireProperties applicationProperties) {
55+
this.applicationProperties = applicationProperties;
56+
}
57+
3858
public static void main(final String[] args) {
3959
SpringApplication.run(SampleDataGemFireApplication.class, args);
4060
}
4161

62+
63+
@Bean
64+
CacheFactoryBean gemfireCache() {
65+
CacheFactoryBean gemfireCache = new CacheFactoryBean();
66+
67+
gemfireCache.setClose(true);
68+
gemfireCache.setProperties(gemfireProperties());
69+
70+
return gemfireCache;
71+
}
72+
73+
private Properties gemfireProperties() {
74+
Properties gemfireProperties = new Properties();
75+
76+
gemfireProperties.setProperty("name", SampleDataGemFireApplication.class.getSimpleName());
77+
gemfireProperties.setProperty("mcast-port", "0");
78+
gemfireProperties.setProperty("locators", "");
79+
gemfireProperties.setProperty("log-level", this.applicationProperties.getLogLevel());
80+
81+
return gemfireProperties;
82+
}
83+
84+
@Bean(name = GEMSTONES_REGION_NAME)
85+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion(Cache gemfireCache,
86+
RegionAttributes<Long, Gemstone> gemstonesRegionAttributes) {
87+
88+
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion =
89+
new ReplicatedRegionFactoryBean<Long, Gemstone>();
90+
91+
gemstonesRegion.setAttributes(gemstonesRegionAttributes);
92+
gemstonesRegion.setClose(false);
93+
gemstonesRegion.setCache(gemfireCache);
94+
gemstonesRegion.setName(GEMSTONES_REGION_NAME);
95+
gemstonesRegion.setPersistent(false);
96+
97+
return gemstonesRegion;
98+
}
99+
100+
@Bean
101+
@SuppressWarnings("unchecked")
102+
RegionAttributesFactoryBean gemstonesRegionAttributes() {
103+
RegionAttributesFactoryBean gemstonesRegionAttributes =
104+
new RegionAttributesFactoryBean();
105+
106+
gemstonesRegionAttributes.setKeyConstraint(Long.class);
107+
gemstonesRegionAttributes.setValueConstraint(Gemstone.class);
108+
109+
return gemstonesRegionAttributes;
110+
}
111+
112+
@Bean
113+
GemfireTransactionManager gemfireTransactionManager(Cache gemfireCache) {
114+
return new GemfireTransactionManager(gemfireCache);
115+
}
116+
42117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2016 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+
21+
/**
22+
* Configuration properties for Gemfire sample.
23+
*
24+
* @author John Blum
25+
*/
26+
@ConfigurationProperties(prefix = "sample.data.gemfire")
27+
public class SampleDataGemFireProperties {
28+
29+
/**
30+
* Caching log level.
31+
*/
32+
private String logLevel = "config";
33+
34+
public String getLogLevel() {
35+
return this.logLevel;
36+
}
37+
38+
public void setLogLevel(String logLevel) {
39+
this.logLevel = logLevel;
40+
}
41+
42+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,14 +43,15 @@ public class GemstoneServiceImpl implements GemstoneService {
4343
Arrays.asList("ALEXANDRITE", "AQUAMARINE", "DIAMOND", "OPAL", "PEARL", "RUBY",
4444
"SAPPHIRE", "SPINEL", "TOPAZ"));
4545

46-
@Autowired
47-
private GemstoneRepository gemstoneRepo;
46+
private final GemstoneRepository gemstoneRepository;
47+
48+
public GemstoneServiceImpl(GemstoneRepository gemstoneRepository) {
49+
this.gemstoneRepository = gemstoneRepository;
50+
}
4851

4952
@PostConstruct
5053
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());
54+
System.out.printf("[%1$s] initialized!%n", getClass().getSimpleName());
5455
}
5556

5657
/**
@@ -62,7 +63,7 @@ public void init() {
6263
@Override
6364
@Transactional(readOnly = true)
6465
public long count() {
65-
return this.gemstoneRepo.count();
66+
return this.gemstoneRepository.count();
6667
}
6768

6869
/**
@@ -75,8 +76,8 @@ public long count() {
7576
*/
7677
@Override
7778
@Transactional(readOnly = true)
78-
public Gemstone get(final Long id) {
79-
return this.gemstoneRepo.findOne(id);
79+
public Gemstone get(Long id) {
80+
return this.gemstoneRepository.findOne(id);
8081
}
8182

8283
/**
@@ -89,8 +90,8 @@ public Gemstone get(final Long id) {
8990
*/
9091
@Override
9192
@Transactional(readOnly = true)
92-
public Gemstone get(final String name) {
93-
return this.gemstoneRepo.findByName(name);
93+
public Gemstone get(String name) {
94+
return this.gemstoneRepository.findByName(name);
9495
}
9596

9697
/**
@@ -105,7 +106,7 @@ public Gemstone get(final String name) {
105106
@Override
106107
@Transactional(readOnly = true)
107108
public Iterable<Gemstone> list() {
108-
return this.gemstoneRepo.findAll();
109+
return this.gemstoneRepository.findAll();
109110
}
110111

111112
/**
@@ -118,54 +119,41 @@ public Iterable<Gemstone> list() {
118119
*/
119120
@Override
120121
@Transactional
121-
public Gemstone save(final Gemstone gemstone) {
122+
public Gemstone save(Gemstone gemstone) {
122123
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
123124
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");
124125

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));
126+
// NOTE deliberately (& naively) validate the Gemstone after mutating data access in
127+
// GemFire rather than before to demonstrate transactions in GemFire.
128+
Gemstone savedGemstone = validate(this.gemstoneRepository.save(gemstone));
129129

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));
130+
Assert.state(savedGemstone.equals(get(gemstone.getId())), String.format(
131+
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
132+
gemstone));
134133

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

137136
return gemstone;
138137
}
139138

140-
private Gemstone validate(final Gemstone gemstone) {
139+
Gemstone validate(Gemstone gemstone) {
141140
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());
141+
// NOTE if the Gemstone is not valid, throw error...
142+
// Should cause transaction to rollback in GemFire!
143+
System.err.printf("Illegal Gemstone [%1$s]!%n", gemstone.getName());
145144
throw new IllegalGemstoneException(
146-
String.format("'%1$s' is not a valid Gemstone!", gemstone.getName()));
145+
String.format("[%1$s] is not a valid Gemstone!", gemstone.getName()));
147146
}
148147

149148
return gemstone;
150149
}
151150

152151
public static final class IllegalGemstoneException extends IllegalArgumentException {
153152

154-
public IllegalGemstoneException() {
155-
}
156-
157-
public IllegalGemstoneException(final String message) {
153+
public IllegalGemstoneException(String message) {
158154
super(message);
159155
}
160156

161-
public IllegalGemstoneException(final Throwable cause) {
162-
super(cause);
163-
}
164-
165-
public IllegalGemstoneException(final String message, final Throwable cause) {
166-
super(message, cause);
167-
}
168-
169157
}
170158

171159
}

0 commit comments

Comments
 (0)