Polish
This commit is contained in:
@@ -47,71 +47,61 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@EnableConfigurationProperties(SampleDataGemFireProperties.class)
|
||||
public class SampleDataGemFireApplication {
|
||||
|
||||
protected static final String GEMSTONES_REGION_NAME = "Gemstones";
|
||||
private static final String GEMSTONES_REGION_NAME = "Gemstones";
|
||||
|
||||
private final SampleDataGemFireProperties applicationProperties;
|
||||
private final SampleDataGemFireProperties properties;
|
||||
|
||||
public SampleDataGemFireApplication(SampleDataGemFireProperties applicationProperties) {
|
||||
this.applicationProperties = applicationProperties;
|
||||
public SampleDataGemFireApplication(
|
||||
SampleDataGemFireProperties applicationProperties) {
|
||||
this.properties = applicationProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheFactoryBean gemfireCache() {
|
||||
CacheFactoryBean cache = new CacheFactoryBean();
|
||||
cache.setClose(true);
|
||||
cache.setProperties(getCacheProperties());
|
||||
return cache;
|
||||
}
|
||||
|
||||
private Properties getCacheProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("name",
|
||||
SampleDataGemFireApplication.class.getSimpleName());
|
||||
properties.setProperty("mcast-port", "0");
|
||||
properties.setProperty("locators", "");
|
||||
properties.setProperty("log-level", this.properties.getLogLevel());
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Bean(name = GEMSTONES_REGION_NAME)
|
||||
public ReplicatedRegionFactoryBean<Long, Gemstone> gemstonesRegion(Cache cache,
|
||||
RegionAttributes<Long, Gemstone> attributes) {
|
||||
ReplicatedRegionFactoryBean<Long, Gemstone> region = new ReplicatedRegionFactoryBean<Long, Gemstone>();
|
||||
region.setAttributes(attributes);
|
||||
region.setClose(false);
|
||||
region.setCache(cache);
|
||||
region.setName(GEMSTONES_REGION_NAME);
|
||||
region.setPersistent(false);
|
||||
return region;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings({ "unchecked", "deprecation" })
|
||||
public RegionAttributesFactoryBean gemstonesRegionAttributes() {
|
||||
RegionAttributesFactoryBean attributes = new RegionAttributesFactoryBean();
|
||||
attributes.setKeyConstraint(Long.class);
|
||||
attributes.setValueConstraint(Gemstone.class);
|
||||
return attributes;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GemfireTransactionManager gemfireTransactionManager(Cache gemfireCache) {
|
||||
return new GemfireTransactionManager(gemfireCache);
|
||||
}
|
||||
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(SampleDataGemFireApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
CacheFactoryBean gemfireCache() {
|
||||
CacheFactoryBean gemfireCache = new CacheFactoryBean();
|
||||
|
||||
gemfireCache.setClose(true);
|
||||
gemfireCache.setProperties(gemfireProperties());
|
||||
|
||||
return gemfireCache;
|
||||
}
|
||||
|
||||
private Properties gemfireProperties() {
|
||||
Properties gemfireProperties = new Properties();
|
||||
|
||||
gemfireProperties.setProperty("name", SampleDataGemFireApplication.class.getSimpleName());
|
||||
gemfireProperties.setProperty("mcast-port", "0");
|
||||
gemfireProperties.setProperty("locators", "");
|
||||
gemfireProperties.setProperty("log-level", this.applicationProperties.getLogLevel());
|
||||
|
||||
return gemfireProperties;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package sample.data.gemfire.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.gemfire.mapping.Region;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -39,11 +40,11 @@ public class Gemstone implements Serializable {
|
||||
public Gemstone() {
|
||||
}
|
||||
|
||||
public Gemstone(final Long id) {
|
||||
public Gemstone(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Gemstone(final Long id, final String name) {
|
||||
public Gemstone(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
@@ -52,7 +53,7 @@ public class Gemstone implements Serializable {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@@ -60,36 +61,32 @@ public class Gemstone implements Serializable {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof Gemstone)) {
|
||||
if (obj == null || !obj.getClass().equals(getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Gemstone that = (Gemstone) obj;
|
||||
|
||||
return ObjectUtils.nullSafeEquals(this.getName(), that.getName());
|
||||
return ObjectUtils.nullSafeEquals(this.getName(), ((Gemstone) obj).getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hashValue = 17;
|
||||
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getName());
|
||||
return hashValue;
|
||||
return ObjectUtils.nullSafeHashCode(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("{ @type = %1$s, id = %2$d, name = %3$s }",
|
||||
getClass().getName(), getId(), getName());
|
||||
ToStringCreator creator = new ToStringCreator(this);
|
||||
creator.append("id", this.id);
|
||||
creator.append("name", this.name);
|
||||
return creator.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,15 +16,14 @@
|
||||
|
||||
package sample.data.gemfire.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
@@ -39,14 +38,18 @@ import org.springframework.util.Assert;
|
||||
@Service("gemstoneService")
|
||||
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"));
|
||||
protected static final List<String> APPROVED_GEMS;
|
||||
|
||||
private final GemstoneRepository gemstoneRepository;
|
||||
static {
|
||||
APPROVED_GEMS = Collections.unmodifiableList(
|
||||
Arrays.asList("ALEXANDRITE,AQUAMARINE,DIAMOND,OPAL,PEARL,"
|
||||
+ "RUBY,SAPPHIRE,SPINEL,TOPAZ".split(",")));
|
||||
}
|
||||
|
||||
private final GemstoneRepository repository;
|
||||
|
||||
public GemstoneServiceImpl(GemstoneRepository gemstoneRepository) {
|
||||
this.gemstoneRepository = gemstoneRepository;
|
||||
this.repository = gemstoneRepository;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@@ -56,20 +59,16 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
|
||||
/**
|
||||
* Returns a count of the number of Gemstones in the GemFire Cache.
|
||||
* <p/>
|
||||
*
|
||||
* @return a long value indicating the number of Gemstones in the GemFire Cache.
|
||||
*/
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public long count() {
|
||||
return this.gemstoneRepository.count();
|
||||
return this.repository.count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Gemstone by ID.
|
||||
* <p/>
|
||||
*
|
||||
* @param id a long value indicating the identifier of the Gemstone.
|
||||
* @return a Gemstone with ID, or null if no Gemstone exists with ID.
|
||||
* @see sample.data.gemfire.domain.Gemstone
|
||||
@@ -77,13 +76,11 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Gemstone get(Long id) {
|
||||
return this.gemstoneRepository.findOne(id);
|
||||
return this.repository.findOne(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Gemstone by name.
|
||||
* <p/>
|
||||
*
|
||||
* @param name a String value indicating the name of the Gemstone.
|
||||
* @return a Gemstone with name, or null if no Gemstone exists with name.
|
||||
* @see sample.data.gemfire.domain.Gemstone
|
||||
@@ -91,13 +88,11 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Gemstone get(String name) {
|
||||
return this.gemstoneRepository.findByName(name);
|
||||
return this.repository.findByName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a listing of Gemstones currently stored in the GemFire Cache.
|
||||
* <p/>
|
||||
*
|
||||
* @return an Iterable object to iterate over the list of Gemstones currently stored
|
||||
* in the GemFire Cache.
|
||||
* @see java.lang.Iterable
|
||||
@@ -106,13 +101,11 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Iterable<Gemstone> list() {
|
||||
return this.gemstoneRepository.findAll();
|
||||
return this.repository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the specified Gemstone to the GemFire Cache.
|
||||
* <p/>
|
||||
*
|
||||
* @param gemstone the Gemstone to save in the GemFire Cache.
|
||||
* @return the saved Gemstone.
|
||||
* @see sample.data.gemfire.domain.Gemstone
|
||||
@@ -122,17 +115,13 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
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.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));
|
||||
|
||||
// NOTE deliberately (& naively) validate the Gemstone after mutating data access
|
||||
// in GemFire rather than before to demonstrate transactions in GemFire.
|
||||
Gemstone savedGemstone = validate(this.repository.save(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());
|
||||
|
||||
return gemstone;
|
||||
}
|
||||
|
||||
@@ -144,16 +133,7 @@ public class GemstoneServiceImpl implements GemstoneService {
|
||||
throw new IllegalGemstoneException(
|
||||
String.format("[%1$s] is not a valid Gemstone!", gemstone.getName()));
|
||||
}
|
||||
|
||||
return gemstone;
|
||||
}
|
||||
|
||||
public static final class IllegalGemstoneException extends IllegalArgumentException {
|
||||
|
||||
public IllegalGemstoneException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.service;
|
||||
|
||||
/**
|
||||
* Exception thrown from {@link GemstoneService}.
|
||||
*
|
||||
* @author John Blum
|
||||
*/
|
||||
public class IllegalGemstoneException extends IllegalArgumentException {
|
||||
|
||||
public IllegalGemstoneException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user