Skip to content

Polish Pivotal GemFire starter and sample. #5444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions spring-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<flyway.version>3.2.1</flyway.version>
<freemarker.version>2.3.23</freemarker.version>
<elasticsearch.version>1.7.5</elasticsearch.version>
<gemfire.version>8.2.0</gemfire.version>
<glassfish-el.version>3.0.0</glassfish-el.version>
<gradle.version>1.12</gradle.version>
<groovy.version>2.4.6</groovy.version>
Expand Down Expand Up @@ -151,6 +150,7 @@
<spring-security-jwt.version>1.0.3.RELEASE</spring-security-jwt.version>
<spring-security-oauth.version>2.0.9.RELEASE</spring-security-oauth.version>
<spring-session.version>1.1.0.RELEASE</spring-session.version>
<spring-shell.version>1.1.0.RELEASE</spring-shell.version>
<spring-social.version>1.1.4.RELEASE</spring-social.version>
<spring-social-facebook.version>2.0.3.RELEASE</spring-social-facebook.version>
<spring-social-linkedin.version>1.0.2.RELEASE</spring-social-linkedin.version>
Expand Down Expand Up @@ -663,17 +663,6 @@
<artifactId>jackson-module-parameter-names</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.gemstone.gemfire</groupId>
<artifactId>gemfire</artifactId>
<version>${gemfire.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gemfire</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down Expand Up @@ -2042,6 +2031,11 @@
<artifactId>spring-session-data-redis</artifactId>
<version>${spring-session.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<version>${spring-shell.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
Expand Down
9 changes: 7 additions & 2 deletions spring-boot-samples/spring-boot-sample-data-gemfire/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-data-gemfire</artifactId>
<name>Spring Boot Data Gemfire Sample</name>
<description>Spring Boot Data Gemfire Sample</description>
<name>Spring Boot Data GemFire Sample</name>
<description>Spring Boot Data GemFire Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
Expand All @@ -32,6 +32,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@

package sample.data.gemfire;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.GemfireTransactionManager;
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.RegionAttributes;

import sample.data.gemfire.config.SampleDataGemFireApplicationProperties;
import sample.data.gemfire.domain.Gemstone;

/**
* The GemstoneAppConfiguration class for allowing Spring Boot to pick up additional
* application Spring configuration meta-data for GemFire, which must be specified in
Expand All @@ -30,13 +44,73 @@
* @author John Blum
*/
@SpringBootApplication
@ImportResource("/spring-data-gemfire-cache.xml")
@EnableGemfireRepositories
@EnableTransactionManagement
@EnableConfigurationProperties(SampleDataGemFireApplicationProperties.class)
@SuppressWarnings("unused")
public class SampleDataGemFireApplication {

protected static final String GEMSTONES_REGION_NAME = "Gemstones";

public static void main(final String[] args) {
SpringApplication.run(SampleDataGemFireApplication.class, args);
}

@Autowired
SampleDataGemFireApplicationProperties applicationProperties;

Properties gemfireProperties() {
Properties gemfireProperties = new Properties();

gemfireProperties.setProperty("name", SampleDataGemFireApplication.class.getSimpleName());
gemfireProperties.setProperty("mcast-port", "0");
gemfireProperties.setProperty("locators", "");
gemfireProperties.setProperty("log-level", applicationProperties.getLogLevel());

return gemfireProperties;
}

@Bean
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();

gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());

return gemfireCache;
}

@Bean(name = GEMSTONES_REGION_NAME)
ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion(Cache gemfireCache,
RegionAttributes<Long, Gemstone> gemstonesRegionAttributes) {

ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion =
new ReplicatedRegionFactoryBean<Long, Gemstone>();

gemstonesRegion.setAttributes(gemstonesRegionAttributes);
gemstonesRegion.setClose(false);
gemstonesRegion.setCache(gemfireCache);
gemstonesRegion.setName(GEMSTONES_REGION_NAME);
gemstonesRegion.setPersistent(false);

return gemstonesRegion;
}

@Bean
@SuppressWarnings("unchecked")
RegionAttributesFactoryBean gemstonesRegionAttributes() {
RegionAttributesFactoryBean gemstonesRegionAttributes =
new RegionAttributesFactoryBean();

gemstonesRegionAttributes.setKeyConstraint(Long.class);
gemstonesRegionAttributes.setValueConstraint(Gemstone.class);

return gemstonesRegionAttributes;
}

@Bean
GemfireTransactionManager gemfireTransactionManager(Cache gemfireCache) {
return new GemfireTransactionManager(gemfireCache);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2013 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 sample.data.gemfire.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

/**
* The SampleDataGemFireApplicationProperties class...
*
* @author John Blum
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "sample.data.gemfire")
public class SampleDataGemFireApplicationProperties {

protected static final String DEFAULT_LOG_LEVEL = "config";

private String logLevel = DEFAULT_LOG_LEVEL;

protected String defaultIfEmpty(String value, String defaultValue) {
return (StringUtils.hasText(value) ? value : defaultValue);
}

public String getLogLevel() {
return defaultIfEmpty(this.logLevel, DEFAULT_LOG_LEVEL);
}

public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int hashCode() {
@Override
public String toString() {
return String.format("{ @type = %1$s, id = %2$d, name = %3$s }",
getClass().getName(), getId(), getName());
getClass().getName(), getId(), getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;

import sample.data.gemfire.domain.Gemstone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import sample.data.gemfire.domain.Gemstone;

/**
* The GemstoneServiceImpl class is a Service object implementing the GemstoneService
* interface containing business logic and rules in addition to data services for
Expand All @@ -40,17 +39,17 @@
public class GemstoneServiceImpl implements GemstoneService {

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

@Autowired
private GemstoneRepository gemstoneRepo;
@SuppressWarnings("unused")
private GemstoneRepository gemstoneRepository;

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

/**
Expand All @@ -62,7 +61,7 @@ public void init() {
@Override
@Transactional(readOnly = true)
public long count() {
return this.gemstoneRepo.count();
return this.gemstoneRepository.count();
}

/**
Expand All @@ -75,8 +74,8 @@ public long count() {
*/
@Override
@Transactional(readOnly = true)
public Gemstone get(final Long id) {
return this.gemstoneRepo.findOne(id);
public Gemstone get(Long id) {
return this.gemstoneRepository.findOne(id);
}

/**
Expand All @@ -89,8 +88,8 @@ public Gemstone get(final Long id) {
*/
@Override
@Transactional(readOnly = true)
public Gemstone get(final String name) {
return this.gemstoneRepo.findByName(name);
public Gemstone get(String name) {
return this.gemstoneRepository.findByName(name);
}

/**
Expand All @@ -105,7 +104,7 @@ public Gemstone get(final String name) {
@Override
@Transactional(readOnly = true)
public Iterable<Gemstone> list() {
return this.gemstoneRepo.findAll();
return this.gemstoneRepository.findAll();
}

/**
Expand All @@ -118,37 +117,36 @@ public Iterable<Gemstone> list() {
*/
@Override
@Transactional
public Gemstone save(final Gemstone gemstone) {
public Gemstone save(Gemstone gemstone) {
Assert.notNull(gemstone, "The Gemstone to save must not be null!");
Assert.notNull(gemstone.getName(), "The name of the Gemstone must be specified!");

// NOTE deliberately (naively) validate the Gemstone after mutating data access in
// GemFire rather than before
// to demonstrate transactions in GemFire.
Gemstone savedGemstone = validate(this.gemstoneRepo.save(gemstone));
// NOTE deliberately (& naively) validate the Gemstone after mutating data access in
// GemFire rather than before to demonstrate transactions in GemFire.
Gemstone savedGemstone = validate(this.gemstoneRepository.save(gemstone));

Assert.state(savedGemstone.equals(get(gemstone.getId())),
String.format(
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
gemstone));
Assert.state(savedGemstone.equals(get(gemstone.getId())), String.format(
"Failed to find Gemstone (%1$s) in GemFire's Cache Region 'Gemstones'!",
gemstone));

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

return gemstone;
}

private Gemstone validate(final Gemstone gemstone) {
Gemstone validate(Gemstone gemstone) {
if (!APPROVED_GEMS.contains(gemstone.getName().toUpperCase())) {
// NOTE if the Gemstone is not valid, blow chunks (should cause transaction to
// rollback in GemFire)!
System.err.printf("Illegal Gemstone (%1$s)!%n", gemstone.getName());
// NOTE if the Gemstone is not valid, throw error...
// Should cause transaction to rollback in GemFire!
System.err.printf("Illegal Gemstone [%1$s]!%n", gemstone.getName());
throw new IllegalGemstoneException(
String.format("'%1$s' is not a valid Gemstone!", gemstone.getName()));
String.format("[%1$s] is not a valid Gemstone!", gemstone.getName()));
}

return gemstone;
}

@SuppressWarnings("unused")
public static final class IllegalGemstoneException extends IllegalArgumentException {

public IllegalGemstoneException() {
Expand Down

This file was deleted.

Loading