Skip to content

Commit 14bc839

Browse files
committed
Add getOptionalCache():Optional<GemFireCache> method.
Refactor the CacheFactoryInitializer @FunctionalInterface interface to extend java.util.function.Function. Edit Javadoc. Apply Spring's @nonnull and @nullable annotations to the API. Resolves spring-projectsgh-493.
1 parent 10a01b1 commit 14bc839

File tree

1 file changed

+107
-23
lines changed

1 file changed

+107
-23
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/AbstractBasicCacheFactoryBean.java

+107-23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Objects;
2626
import java.util.Optional;
2727
import java.util.Properties;
28+
import java.util.function.Function;
2829

2930
import org.apache.geode.GemFireCheckedException;
3031
import org.apache.geode.GemFireException;
@@ -57,7 +58,7 @@
5758
import org.springframework.util.Assert;
5859

5960
/**
60-
* Abstract base class for {@link CacheFactoryBean} and {@link ClientCacheFactoryBean} classes
61+
* Abstract base class for {@link CacheFactoryBean} and {@link ClientCacheFactoryBean} classes,
6162
* used to create Apache Geode peer {@link Cache} and {@link ClientCache} instances, respectively.
6263
*
6364
* This class implements Spring's {@link PersistenceExceptionTranslator} interface and is auto-detected by Spring's
@@ -66,10 +67,10 @@
6667
* the presence of this class automatically enables a {@link PersistenceExceptionTranslationPostProcessor}
6768
* to translate Apache Geode {@link RuntimeException RuntimeExceptions} appropriately.
6869
*
69-
* Importantly, this class encapsulates configure applicable to tuning Apache Geode in response to JVM Heap memory.
70-
* Since Apache Geode stores data in-memory, on the JVM Heap, it is important that Aapche Geode be tuned to monitor
71-
* the JVM Heap and respond accordingly to memory pressure, by evicting data and issuing warnings when the JVM Heap
72-
* reaches critical mass.
70+
* Importantly, this abstract class encapsulates configuration applicable to tuning Apache Geode to efficiently use JVM
71+
* Heap memory. Since Apache Geode stores data in-memory, on the JVM Heap, it is important that Apache Geode be tuned
72+
* to monitor the JVM Heap and respond accordingly to memory pressure, by evicting data and issuing warnings when the
73+
* JVM Heap reaches critical mass.
7374
*
7475
* @author John Blum
7576
* @see java.io.File
@@ -170,6 +171,20 @@ protected void setCache(@Nullable GemFireCache cache) {
170171
return (T) this.cache;
171172
}
172173

174+
/**
175+
* Returns an {@link Optional} reference to the constructed, configured and initialized {@link GemFireCache}
176+
* instance created by this cache {@link FactoryBean}.
177+
*
178+
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
179+
* @return an {@link Optional} reference to the {@link GemFireCache} created by this {@link FactoryBean}.
180+
* @see org.apache.geode.cache.GemFireCache
181+
* @see java.util.Optional
182+
* @see #getCache()
183+
*/
184+
public <T extends GemFireCache> Optional<T> getOptionalCache() {
185+
return Optional.ofNullable(getCache());
186+
}
187+
173188
/**
174189
* Set the {@link CacheFactoryInitializer} called by this {@link FactoryBean} to initialize the Apache Geode
175190
* cache factory used to create the cache constructed by this {@link FactoryBean}.
@@ -592,7 +607,20 @@ private boolean isHeapPercentageValid(@NonNull Float heapPercentage) {
592607
return heapPercentage >= 0.0f && heapPercentage <= 100.0f;
593608
}
594609

595-
protected GemFireCache configureHeapPercentages(GemFireCache cache) {
610+
/**
611+
* Configures the {@link GemFireCache} critical and eviction heap thresholds as percentages.
612+
*
613+
* @param cache {@link GemFireCache} to configure the critical and eviction heap thresholds;
614+
* must not be {@literal null}.
615+
* @return the given {@link GemFireCache}.
616+
* @throws IllegalArgumentException if the critical or eviction heap thresholds are not valid percentages.
617+
* @see org.apache.geode.cache.control.ResourceManager#setCriticalHeapPercentage(float)
618+
* @see org.apache.geode.cache.control.ResourceManager#setEvictionHeapPercentage(float)
619+
* @see org.apache.geode.cache.control.ResourceManager
620+
* @see org.apache.geode.cache.GemFireCache#getResourceManager()
621+
* @see org.apache.geode.cache.GemFireCache
622+
*/
623+
protected @NonNull GemFireCache configureHeapPercentages(@NonNull GemFireCache cache) {
596624

597625
Optional.ofNullable(getCriticalHeapPercentage()).ifPresent(criticalHeapPercentage -> {
598626

@@ -615,7 +643,20 @@ protected GemFireCache configureHeapPercentages(GemFireCache cache) {
615643
return cache;
616644
}
617645

618-
protected GemFireCache configureOffHeapPercentages(GemFireCache cache) {
646+
/**
647+
* Configures the {@link GemFireCache} critical and eviction off-heap thresholds as percentages.
648+
*
649+
* @param cache {@link GemFireCache} to configure the critical and eviction off-heap thresholds;
650+
* must not be {@literal null}.
651+
* @return the given {@link GemFireCache}.
652+
* @throws IllegalArgumentException if the critical or eviction off-heap thresholds are not valid percentages.
653+
* @see org.apache.geode.cache.control.ResourceManager#setCriticalOffHeapPercentage(float)
654+
* @see org.apache.geode.cache.control.ResourceManager#setEvictionOffHeapPercentage(float)
655+
* @see org.apache.geode.cache.control.ResourceManager
656+
* @see org.apache.geode.cache.GemFireCache#getResourceManager()
657+
* @see org.apache.geode.cache.GemFireCache
658+
*/
659+
protected @NonNull GemFireCache configureOffHeapPercentages(@NonNull GemFireCache cache) {
619660

620661
Optional.ofNullable(getCriticalOffHeapPercentage()).ifPresent(criticalOffHeapPercentage -> {
621662

@@ -697,15 +738,17 @@ protected Object initializeFactory(Object factory) {
697738
}
698739

699740
/**
700-
* Loads the configured {@literal cache.xml} to initialize the cache.
741+
* Loads the configured {@literal cache.xml} to initialize the {@link GemFireCache}.
701742
*
702743
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
703-
* @param cache cache instance to initialized with {@literal cache.xml}.
704-
* @return the given cache instance.
705-
* @throws RuntimeException if the configured {@literal cache.xml} file could not be loaded.
744+
* @param cache {@link GemFireCache} instance to initialize with {@literal cache.xml}; must not be {@literal null}.
745+
* @return the given {@link GemFireCache}.
746+
* @throws RuntimeException if the configured {@literal cache.xml} file could not be loaded
747+
* into the {@link GemFireCache}.
706748
* @see org.apache.geode.cache.GemFireCache#loadCacheXml(InputStream)
749+
* @see org.apache.geode.cache.GemFireCache
707750
*/
708-
protected <T extends GemFireCache> T loadCacheXml(T cache) {
751+
protected @NonNull <T extends GemFireCache> T loadCacheXml(@NonNull T cache) {
709752

710753
// Load the cache.xml file (Resource) and initialize the cache
711754
Optional.ofNullable(getCacheXml()).ifPresent(cacheXml -> {
@@ -721,7 +764,21 @@ protected <T extends GemFireCache> T loadCacheXml(T cache) {
721764
return cache;
722765
}
723766

724-
protected GemFireCache registerTransactionListeners(GemFireCache cache) {
767+
/**
768+
* Registers configured, application-defined {@link TransactionListener TransactionListeners} with the cache
769+
* (transaction manager) to listen for and receive transaction events when a (cache) transaction is processed
770+
* (e.g. committed or rolled back).
771+
*
772+
* @param cache {@link GemFireCache} used to register the configured, application-defined
773+
* {@link TransactionListener TransactionListeners}; must not be {@literal null}.
774+
* @return the given {@link GemFireCache}.
775+
* @see org.apache.geode.cache.GemFireCache#getCacheTransactionManager()
776+
* @see org.apache.geode.cache.CacheTransactionManager#addListener(TransactionListener)
777+
* @see org.apache.geode.cache.CacheTransactionManager
778+
* @see org.apache.geode.cache.TransactionListener
779+
* @see org.apache.geode.cache.GemFireCache
780+
*/
781+
protected @NonNull GemFireCache registerTransactionListeners(@NonNull GemFireCache cache) {
725782

726783
CollectionUtils.nullSafeCollection(getTransactionListeners()).stream()
727784
.filter(Objects::nonNull)
@@ -730,7 +787,20 @@ protected GemFireCache registerTransactionListeners(GemFireCache cache) {
730787
return cache;
731788
}
732789

733-
protected GemFireCache registerTransactionWriter(GemFireCache cache) {
790+
/**
791+
* Registers the configured, application-defined {@link TransactionWriter} with the cache (transaction manager)
792+
* to receive transaction events with the intent to alter the transaction outcome (e.g. veto).
793+
*
794+
* @param cache {@link GemFireCache} used to register the configured, application-defined {@link TransactionWriter},
795+
* must not be {@literal null}.
796+
* @return the given {@link GemFireCache}.
797+
* @see org.apache.geode.cache.GemFireCache#getCacheTransactionManager()
798+
* @see org.apache.geode.cache.CacheTransactionManager#setWriter(TransactionWriter)
799+
* @see org.apache.geode.cache.CacheTransactionManager
800+
* @see org.apache.geode.cache.TransactionWriter
801+
* @see org.apache.geode.cache.GemFireCache
802+
*/
803+
protected @NonNull GemFireCache registerTransactionWriter(@NonNull GemFireCache cache) {
734804

735805
Optional.ofNullable(getTransactionWriter())
736806
.ifPresent(transactionWriter -> cache.getCacheTransactionManager().setWriter(transactionWriter));
@@ -739,20 +809,21 @@ protected GemFireCache registerTransactionWriter(GemFireCache cache) {
739809
}
740810

741811
/**
742-
* Resolves the Apache Geode {@link Properties} used to configure the {@link Cache}.
812+
* Resolves the Apache Geode {@link Properties} used to configure the {@link GemFireCache}.
743813
*
744-
* @return the resolved Apache Geode {@link Properties} used to configure the {@link Cache}.
814+
* @return the resolved Apache Geode {@link Properties} used to configure the {@link GemFireCache}.
745815
* @see #setAndGetProperties(Properties)
746816
* @see #getProperties()
817+
* @see java.util.Properties
747818
*/
748-
protected Properties resolveProperties() {
819+
protected @NonNull Properties resolveProperties() {
749820

750821
return Optional.ofNullable(getProperties())
751822
.orElseGet(() -> setAndGetProperties(new Properties()));
752823
}
753824

754825
/**
755-
* Translates the thrown Apache Geode {@link RuntimeException} to a corresponding {@link Exception} from Spring's
826+
* Translates the thrown Apache Geode {@link RuntimeException} into a corresponding {@link Exception} from Spring's
756827
* generic {@link DataAccessException} hierarchy if possible.
757828
*
758829
* @param exception the Apache Geode {@link RuntimeException} to translate.
@@ -762,7 +833,7 @@ protected Properties resolveProperties() {
762833
* @see org.springframework.dao.DataAccessException
763834
*/
764835
@Override
765-
public DataAccessException translateExceptionIfPossible(@Nullable RuntimeException exception) {
836+
public @Nullable DataAccessException translateExceptionIfPossible(@Nullable RuntimeException exception) {
766837

767838
if (exception instanceof IllegalArgumentException) {
768839

@@ -790,22 +861,35 @@ public DataAccessException translateExceptionIfPossible(@Nullable RuntimeExcepti
790861
}
791862

792863
/**
793-
* Callback interface for initializing either a {@link CacheFactory} or a {@link ClientCacheFactory} instance,
864+
* Callback interface for initializing a {@link CacheFactory} or a {@link ClientCacheFactory} instance,
794865
* which is used to create an instance of {@link GemFireCache}.
795866
*
796-
* @see org.apache.geode.cache.CacheFactory
797867
* @see org.apache.geode.cache.client.ClientCacheFactory
868+
* @see org.apache.geode.cache.CacheFactory
869+
* @see java.util.function.Function
798870
*/
799871
@FunctionalInterface
800-
public interface CacheFactoryInitializer<T> {
872+
public interface CacheFactoryInitializer<T> extends Function<T, T> {
873+
874+
/**
875+
* Alias for {@link #initialize(Object)}.
876+
*
877+
* @param t cache factory to initialize.
878+
* @return the initialized cache factory.
879+
* @see #initialize(Object)
880+
*/
881+
@Override
882+
default T apply(T t) {
883+
return initialize(t);
884+
}
801885

802886
/**
803887
* Initialize the given cache factory.
804888
*
805889
* @param cacheFactory cache factory to initialize.
806890
* @return the given cache factory.
807-
* @see org.apache.geode.cache.CacheFactory
808891
* @see org.apache.geode.cache.client.ClientCacheFactory
892+
* @see org.apache.geode.cache.CacheFactory
809893
*/
810894
T initialize(T cacheFactory);
811895

0 commit comments

Comments
 (0)