diff --git a/src/main/java/org/springframework/data/gemfire/GemfireCacheUtils.java b/src/main/java/org/springframework/data/gemfire/GemfireCacheUtils.java
index 05b2cb9a..31b83a22 100644
--- a/src/main/java/org/springframework/data/gemfire/GemfireCacheUtils.java
+++ b/src/main/java/org/springframework/data/gemfire/GemfireCacheUtils.java
@@ -30,6 +30,7 @@ import org.apache.geode.NoSystemException;
import org.apache.geode.SystemConnectException;
import org.apache.geode.SystemIsRunningException;
import org.apache.geode.UnmodifiableException;
+import org.apache.geode.admin.AdminException;
import org.apache.geode.cache.CacheException;
import org.apache.geode.cache.CacheExistsException;
import org.apache.geode.cache.CacheLoaderException;
@@ -76,24 +77,24 @@ import org.springframework.dao.TypeMismatchDataAccessException;
import org.springframework.util.ClassUtils;
/**
- * Helper class featuring methods for Pivotal GemFire Cache or Region handling.
+ * Abstract utility class featuring methods for Apache Geode / Pivotal GemFire Cache or Region handling.
*
* @author Costin Leau
+ * @author John Blum
*/
public abstract class GemfireCacheUtils {
private static Class> CQ_EXCEPTION_CLASS;
static {
+
Class> type = null;
try {
type = ClassUtils.resolveClassName("org.apache.geode.cache.query.CqInvalidException",
GemfireCacheUtils.class.getClassLoader());
-
- }
- catch (IllegalArgumentException ignore) {
}
+ catch (IllegalArgumentException ignore) { }
CQ_EXCEPTION_CLASS = type;
}
@@ -103,149 +104,209 @@ public abstract class GemfireCacheUtils {
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
* org.springframework.dao hierarchy.
*
- * @param ex Gemfire unchecked exception
+ * @param cause Gemfire unchecked exception
* @return new the corresponding DataAccessException instance
*/
@SuppressWarnings("deprecation")
- public static DataAccessException convertGemfireAccessException(GemFireException ex) {
- if (ex instanceof CacheException) {
- if (ex instanceof CacheExistsException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
+ public static DataAccessException convertGemfireAccessException(GemFireException cause) {
+
+ if (cause instanceof CacheException) {
+ if (cause instanceof CacheExistsException) {
+ return new DataIntegrityViolationException(cause.getMessage(), cause);
}
- if (ex instanceof CommitConflictException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
+ if (cause instanceof CommitConflictException) {
+ return new DataIntegrityViolationException(cause.getMessage(), cause);
}
- if (ex instanceof CommitIncompleteException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
+ if (cause instanceof CommitIncompleteException) {
+ return new DataIntegrityViolationException(cause.getMessage(), cause);
}
- if (ex instanceof EntryExistsException) {
- return new DuplicateKeyException(ex.getMessage(), ex);
+ if (cause instanceof EntryExistsException) {
+ return new DuplicateKeyException(cause.getMessage(), cause);
}
- if (ex instanceof EntryNotFoundException) {
- return new DataRetrievalFailureException(ex.getMessage(), ex);
+ if (cause instanceof EntryNotFoundException) {
+ return new DataRetrievalFailureException(cause.getMessage(), cause);
}
- if (ex instanceof RegionExistsException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
+ if (cause instanceof RegionExistsException) {
+ return new DataIntegrityViolationException(cause.getMessage(), cause);
}
}
- if (ex instanceof CacheRuntimeException) {
- if (ex instanceof CacheXmlException) {
- return new GemfireSystemException(ex);
+
+ if (cause instanceof CacheRuntimeException) {
+ if (cause instanceof CacheXmlException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof CancelException) {
+ if (cause instanceof CancelException) {
// all cancellations go wrapped by this exception
- return new GemfireCancellationException((CancelException) ex);
+ return new GemfireCancellationException((CancelException) cause);
}
- if (ex instanceof CqClosedException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
+ if (cause instanceof CqClosedException) {
+ return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
}
- if (ex instanceof DiskAccessException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof DiskAccessException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof EntryDestroyedException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
+ if (cause instanceof EntryDestroyedException) {
+ return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
}
- if (ex instanceof FailedSynchronizationException) {
- return new PessimisticLockingFailureException(ex.getMessage(), ex);
+ if (cause instanceof FailedSynchronizationException) {
+ return new PessimisticLockingFailureException(cause.getMessage(), cause);
}
- if (ex instanceof IndexMaintenanceException) {
- return new GemfireIndexException((IndexMaintenanceException) ex);
+ if (cause instanceof IndexMaintenanceException) {
+ return new GemfireIndexException((IndexMaintenanceException) cause);
}
- if (ex instanceof OperationAbortedException) {
+ if (cause instanceof OperationAbortedException) {
// treat user exceptions first
- if (ex instanceof CacheLoaderException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof CacheLoaderException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof CacheWriterException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof CacheWriterException) {
+ return new GemfireSystemException(cause);
}
// the rest are treated as resource failures
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof PartitionedRegionDistributionException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof PartitionedRegionDistributionException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof PartitionedRegionStorageException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof PartitionedRegionStorageException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof QueryExecutionTimeoutException) {
- return new GemfireQueryException((QueryExecutionTimeoutException) ex);
+ if (cause instanceof QueryExecutionTimeoutException) {
+ return new GemfireQueryException((QueryExecutionTimeoutException) cause);
}
- if (ex instanceof RegionDestroyedException) {
- return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
+ if (cause instanceof RegionDestroyedException) {
+ return new InvalidDataAccessResourceUsageException(cause.getMessage(), cause);
}
- if (ex instanceof org.apache.geode.admin.RegionNotFoundException) {
- return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
+ if (cause instanceof org.apache.geode.admin.RegionNotFoundException) {
+ return new InvalidDataAccessResourceUsageException(cause.getMessage(), cause);
}
- if (ex instanceof ResourceException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof ResourceException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof RoleException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof RoleException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof StatisticsDisabledException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof StatisticsDisabledException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof SynchronizationCommitConflictException) {
- return new PessimisticLockingFailureException(ex.getMessage(), ex);
+ if (cause instanceof SynchronizationCommitConflictException) {
+ return new PessimisticLockingFailureException(cause.getMessage(), cause);
}
}
- if (ex instanceof CopyException) {
- return new GemfireSystemException(ex);
+
+ if (cause instanceof CopyException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof FunctionException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
+ if (cause instanceof FunctionException) {
+ return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
}
- if (ex instanceof GemFireCacheException) {
- return convertGemfireAccessException(((GemFireCacheException) ex).getCacheException());
+ if (cause instanceof GemFireCacheException) {
+ return convertGemfireAccessException(((GemFireCacheException) cause).getCacheException());
}
- if (ex instanceof GemFireConfigException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof GemFireConfigException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof GemFireIOException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof GemFireIOException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof GemFireSecurityException) {
- return new PermissionDeniedDataAccessException(ex.getMessage(), ex);
+ if (cause instanceof GemFireSecurityException) {
+ return new PermissionDeniedDataAccessException(cause.getMessage(), cause);
}
- if (ex instanceof IncompatibleSystemException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof IncompatibleSystemException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof InternalGemFireException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof InternalGemFireException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof InvalidValueException) {
- return new TypeMismatchDataAccessException(ex.getMessage(), ex);
+ if (cause instanceof InvalidValueException) {
+ return new TypeMismatchDataAccessException(cause.getMessage(), cause);
}
- if (ex instanceof LeaseExpiredException) {
- return new PessimisticLockingFailureException(ex.getMessage(), ex);
+ if (cause instanceof LeaseExpiredException) {
+ return new PessimisticLockingFailureException(cause.getMessage(), cause);
}
- if (ex instanceof NoSystemException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof NoSystemException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof org.apache.geode.admin.RuntimeAdminException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof org.apache.geode.admin.RuntimeAdminException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof ServerConnectivityException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof ServerConnectivityException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof SystemConnectException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
+ if (cause instanceof SystemConnectException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
}
- if (ex instanceof SystemIsRunningException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof SystemIsRunningException) {
+ return new GemfireSystemException(cause);
}
- if (ex instanceof UnmodifiableException) {
- return new GemfireSystemException(ex);
+ if (cause instanceof UnmodifiableException) {
+ return new GemfireSystemException(cause);
}
// for exceptions that had their parent changed in 6.5
- DataAccessException dae = convertQueryExceptions(ex);
- if (dae != null)
- return dae;
+ DataAccessException dataAccessException = convertQueryExceptions(cause);
- // fall back
- return new GemfireSystemException(ex);
+ if (dataAccessException != null) {
+ return dataAccessException;
+ }
+
+ return new GemfireSystemException(cause);
+ }
+
+ /**
+ * Converts the given (checked) Gemfire exception to an appropriate one from the
+ * org.springframework.dao hierarchy.
+ *
+ * @param cause Gemfire unchecked exception
+ * @return new the corresponding DataAccessException instance
+ */
+ @SuppressWarnings("deprecation")
+ public static DataAccessException convertGemfireAccessException(GemFireCheckedException cause) {
+
+ if (cause instanceof AdminException) {
+ return new GemfireSystemException(cause);
+ }
+
+ if (cause instanceof QueryException) {
+ return new GemfireQueryException((QueryException) cause);
+ }
+
+ if (cause instanceof VersionException) {
+ return new DataAccessResourceFailureException(cause.getMessage(), cause);
+ }
+
+ return new GemfireSystemException(cause);
+ }
+
+ /**
+ * Converts the given (unchecked) Gemfire exception to an appropriate one from the
+ * org.springframework.dao hierarchy. This method exists to handle backwards compatibility
+ * for exceptions that had their parents changed in Pivotal GemFire 6.5.
+ *
+ * @param cause Gemfire unchecked exception
+ * @return new the corresponding DataAccessException instance
+ */
+ public static DataAccessException convertGemfireAccessException(IndexInvalidException cause) {
+ return new GemfireIndexException(cause);
+ }
+
+ /**
+ * Converts the given (unchecked) Gemfire exception to an appropriate one from the
+ * org.springframework.dao hierarchy. This method exists to handle backwards compatibility
+ * for exceptions that had their parents changed in Pivotal GemFire 6.5.
+ *
+ * @param cause Gemfire unchecked exception
+ * @return new the corresponding DataAccessException instance
+ */
+ public static DataAccessException convertGemfireAccessException(QueryInvalidException cause) {
+ return new GemfireQueryException(cause);
+ }
+
+ /**
+ * Package protected method for detecting CqInvalidException which has been removed in Pivotal GemFire 6.5 GA.
+ */
+ static boolean isCqInvalidException(RuntimeException cause) {
+ return CQ_EXCEPTION_CLASS != null && CQ_EXCEPTION_CLASS.isInstance(cause);
}
/**
@@ -253,76 +314,21 @@ public abstract class GemfireCacheUtils {
* parent changed. This method exists to 'fool' the compiler type checks
* by loosening the type so the code compiles on both 6.5 (pre and current) branches.
*/
- static DataAccessException convertQueryExceptions(RuntimeException ex) {
- if (ex instanceof IndexInvalidException) {
- return convertGemfireAccessException((IndexInvalidException) ex);
- }
- if (ex instanceof QueryInvalidException) {
- return convertGemfireAccessException((QueryInvalidException) ex);
+ static DataAccessException convertQueryExceptions(RuntimeException cause) {
+
+ if (cause instanceof IndexInvalidException) {
+ return convertGemfireAccessException((IndexInvalidException) cause);
}
- if (isCqInvalidException(ex)) {
- return convertCqInvalidException(ex);
+ if (cause instanceof QueryInvalidException) {
+ return convertGemfireAccessException((QueryInvalidException) cause);
}
- // fall back
- return new GemfireSystemException(ex);
- }
-
- /**
- * Converts the given (checked) Gemfire exception to an appropriate one from the
- * org.springframework.dao hierarchy.
- *
- * @param ex Gemfire unchecked exception
- * @return new the corresponding DataAccessException instance
- */
- @SuppressWarnings("deprecation")
- public static DataAccessException convertGemfireAccessException(GemFireCheckedException ex) {
- // query exceptions
- if (ex instanceof QueryException) {
- return new GemfireQueryException((QueryException) ex);
+ if (isCqInvalidException(cause)) {
+ return convertCqInvalidException(cause);
}
- // version exception
- if (ex instanceof VersionException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
- }
- // admin exception
- if (ex instanceof org.apache.geode.admin.AdminException) {
- return new GemfireSystemException(ex);
- }
- // fall back
- return new GemfireSystemException(ex);
- }
- /**
- * Converts the given (unchecked) Gemfire exception to an appropriate one from the
- * org.springframework.dao hierarchy. This method exists to handle backwards compatibility
- * for exceptions that had their parents changed in Pivotal GemFire 6.5.
- *
- * @param ex Gemfire unchecked exception
- * @return new the corresponding DataAccessException instance
- */
- public static DataAccessException convertGemfireAccessException(IndexInvalidException ex) {
- return new GemfireIndexException(ex);
- }
-
- /**
- * Converts the given (unchecked) Gemfire exception to an appropriate one from the
- * org.springframework.dao hierarchy. This method exists to handle backwards compatibility
- * for exceptions that had their parents changed in Pivotal GemFire 6.5.
- *
- * @param ex Gemfire unchecked exception
- * @return new the corresponding DataAccessException instance
- */
- public static DataAccessException convertGemfireAccessException(QueryInvalidException ex) {
- return new GemfireQueryException(ex);
- }
-
- /**
- * Package protected method for detecting CqInvalidException which has been removed in Pivotal GemFire 6.5 GA.
- */
- static boolean isCqInvalidException(RuntimeException ex) {
- return (CQ_EXCEPTION_CLASS != null && CQ_EXCEPTION_CLASS.isAssignableFrom(ex.getClass()));
+ return new GemfireSystemException(cause);
}
/**
@@ -330,10 +336,10 @@ public abstract class GemfireCacheUtils {
* org.springframework.dao hierarchy. This method exists to handle backwards compatibility
* for exceptions that have been removed in 6.5.
*
- * @param ex Gemfire unchecked exception
+ * @param cause Gemfire unchecked exception
* @return new the corresponding DataAccessException instance
*/
- static DataAccessException convertCqInvalidException(RuntimeException ex) {
- return new GemfireQueryException(ex);
+ static DataAccessException convertCqInvalidException(RuntimeException cause) {
+ return new GemfireQueryException(cause);
}
}
diff --git a/src/main/java/org/springframework/data/gemfire/GemfireUtils.java b/src/main/java/org/springframework/data/gemfire/GemfireUtils.java
index 308502bd..655fe7f4 100644
--- a/src/main/java/org/springframework/data/gemfire/GemfireUtils.java
+++ b/src/main/java/org/springframework/data/gemfire/GemfireUtils.java
@@ -24,19 +24,20 @@ import org.springframework.util.ClassUtils;
import org.w3c.dom.Element;
/**
- * {@link GemfireUtils} is an abstract utility class encapsulating common functionality to access features
- * and capabilities of Pivotal GemFire based on version and other configuration meta-data.
+ * {@link GemfireUtils} is an abstract utility class encapsulating common functionality for accessing features
+ * and capabilities of Apache Geode or Pivotal GemFire based on version as well as other configuration meta-data.
*
* @author John Blum
* @see org.apache.geode.cache.CacheFactory
- * @see org.apache.geode.cache.Region
+ * @see org.apache.geode.internal.GemFireVersion
+ * @see org.springframework.data.gemfire.config.support.GemfireFeature
* @see org.springframework.data.gemfire.util.RegionUtils
* @since 1.3.3
*/
@SuppressWarnings("unused")
public abstract class GemfireUtils extends RegionUtils {
- public final static String APACHE_GEODE_NAME = "Aache Geode";
+ public final static String APACHE_GEODE_NAME = "Apache Geode";
public final static String GEMFIRE_NAME = apacheGeodeProductName();
public final static String GEMFIRE_VERSION = apacheGeodeVersion();
public final static String UNKNOWN = "unknown";
@@ -131,6 +132,6 @@ public abstract class GemfireUtils extends RegionUtils {
}
public static void main(final String... args) {
- System.out.printf("Pivotal GemFire Product Name [%1$s] Version [%2$s]%n", GEMFIRE_NAME, GEMFIRE_VERSION);
+ System.out.printf("Product Name [%1$s] Version [%2$s]%n", GEMFIRE_NAME, GEMFIRE_VERSION);
}
}
diff --git a/src/main/java/org/springframework/data/gemfire/support/SpringContextBootstrappingInitializer.java b/src/main/java/org/springframework/data/gemfire/support/SpringContextBootstrappingInitializer.java
index 17b3b725..fa114cac 100644
--- a/src/main/java/org/springframework/data/gemfire/support/SpringContextBootstrappingInitializer.java
+++ b/src/main/java/org/springframework/data/gemfire/support/SpringContextBootstrappingInitializer.java
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.data.gemfire.support;
import java.util.Arrays;
@@ -39,6 +38,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.data.gemfire.util.CacheUtils;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -95,6 +95,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see org.springframework.context.ConfigurableApplicationContext
*/
public static synchronized ConfigurableApplicationContext getApplicationContext() {
+
Assert.state(applicationContext != null,
"A Spring ApplicationContext was not configured and initialized properly");
@@ -111,7 +112,8 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see java.lang.ClassLoader
*/
public static void setBeanClassLoader(ClassLoader beanClassLoader) {
- if (applicationContext == null || !applicationContext.isActive()) {
+
+ if (isApplicationContextInitializable()) {
beanClassLoaderReference.set(beanClassLoader);
}
else {
@@ -132,6 +134,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see org.springframework.context.event.ContextRefreshedEvent
*/
protected static void notifyOnExistingContextRefreshedEvent(ApplicationListener listener) {
+
synchronized (applicationEventNotifier) {
if (contextRefreshedEvent != null) {
listener.onApplicationEvent(contextRefreshedEvent);
@@ -156,6 +159,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* #addApplicationListener(org.springframework.context.ApplicationListener)
*/
public static > T register(T listener) {
+
synchronized (applicationEventNotifier) {
applicationEventNotifier.addApplicationListener(listener);
notifyOnExistingContextRefreshedEvent(listener);
@@ -173,7 +177,9 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see #unregister(Class)
*/
public static boolean register(Class> annotatedClass) {
+
Assert.notNull(annotatedClass, "The Spring annotated class to register must not be null");
+
return registeredAnnotatedClasses.add(annotatedClass);
}
@@ -192,6 +198,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* #removeApplicationListener(org.springframework.context.ApplicationListener)
*/
public static > T unregister(T listener) {
+
synchronized (applicationEventNotifier) {
applicationEventNotifier.removeApplicationListener(listener);
}
@@ -222,11 +229,11 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
return LogFactory.getLog(getClass());
}
- /* (non-Javadoc) */
private boolean isConfigurable(Collection> annotatedClasses, String[] basePackages,
- String[] contextConfigLocations) {
+ String[] contextConfigLocations) {
- return !(CollectionUtils.isEmpty(annotatedClasses) && ObjectUtils.isEmpty(basePackages)
+ return !(CollectionUtils.isEmpty(annotatedClasses)
+ && ObjectUtils.isEmpty(basePackages)
&& ObjectUtils.isEmpty(contextConfigLocations));
}
@@ -255,21 +262,24 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see org.springframework.context.support.ClassPathXmlApplicationContext
*/
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages, String[] configLocations) {
- Assert.isTrue(isConfigurable(registeredAnnotatedClasses, basePackages, configLocations),
- "'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified in order to"
- + " construct and configure an instance of the ConfigurableApplicationContext");
- Class>[] annotatedClasses = registeredAnnotatedClasses.toArray(
- new Class>[registeredAnnotatedClasses.size()]);
+ String message = "'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
+ + " in order to construct and configure an instance of the ConfigurableApplicationContext";
- return scanBasePackages(registerAnnotatedClasses(createApplicationContext(configLocations),
- annotatedClasses), basePackages);
+ Assert.isTrue(isConfigurable(registeredAnnotatedClasses, basePackages, configLocations), message);
+
+ Class>[] annotatedClasses = registeredAnnotatedClasses.toArray(new Class>[0]);
+
+ ConfigurableApplicationContext applicationContext = createApplicationContext(configLocations);
+
+ return scanBasePackages(registerAnnotatedClasses(applicationContext, annotatedClasses), basePackages);
}
- /* (non-Javadoc) - used for testing purposes only */
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
- return (ObjectUtils.isEmpty(configLocations) ? new AnnotationConfigApplicationContext()
- : new ClassPathXmlApplicationContext(configLocations, false));
+
+ return ObjectUtils.isEmpty(configLocations)
+ ? new AnnotationConfigApplicationContext()
+ : new ClassPathXmlApplicationContext(configLocations, false);
}
/**
@@ -284,6 +294,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @throws java.lang.IllegalArgumentException if the ApplicationContext reference is null!
*/
protected ConfigurableApplicationContext initApplicationContext(ConfigurableApplicationContext applicationContext) {
+
Assert.notNull(applicationContext, "ConfigurableApplicationContext must not be null");
applicationContext.addApplicationListener(this);
@@ -302,6 +313,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @throws java.lang.IllegalArgumentException if the ApplicationContext reference is null!
*/
protected ConfigurableApplicationContext refreshApplicationContext(ConfigurableApplicationContext applicationContext) {
+
Assert.notNull(applicationContext, "ConfigurableApplicationContext must not be null");
applicationContext.refresh();
@@ -323,11 +335,15 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
ConfigurableApplicationContext registerAnnotatedClasses(ConfigurableApplicationContext applicationContext,
Class>[] annotatedClasses) {
- if (applicationContext instanceof AnnotationConfigApplicationContext
- && !ObjectUtils.isEmpty(annotatedClasses)) {
+ return applicationContext instanceof AnnotationConfigApplicationContext && !ObjectUtils.isEmpty(annotatedClasses)
+ ? doRegister(applicationContext, annotatedClasses)
+ : applicationContext;
+ }
- ((AnnotationConfigApplicationContext) applicationContext).register(annotatedClasses);
- }
+ ConfigurableApplicationContext doRegister(ConfigurableApplicationContext applicationContext,
+ Class>[] annotatedClasses) {
+
+ ((AnnotationConfigApplicationContext) applicationContext).register(annotatedClasses);
return applicationContext;
}
@@ -345,11 +361,14 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
ConfigurableApplicationContext scanBasePackages(ConfigurableApplicationContext applicationContext,
String[] basePackages) {
- if (applicationContext instanceof AnnotationConfigApplicationContext
- && !ObjectUtils.isEmpty(basePackages)) {
+ return applicationContext instanceof AnnotationConfigApplicationContext && !ObjectUtils.isEmpty(basePackages)
+ ? doScan(applicationContext, basePackages)
+ : applicationContext;
+ }
- ((AnnotationConfigApplicationContext) applicationContext).scan(basePackages);
- }
+ ConfigurableApplicationContext doScan(ConfigurableApplicationContext applicationContext, String[] basePackages) {
+
+ ((AnnotationConfigApplicationContext) applicationContext).scan(basePackages);
return applicationContext;
}
@@ -363,6 +382,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see java.lang.ClassLoader
*/
ConfigurableApplicationContext setClassLoader(ConfigurableApplicationContext applicationContext) {
+
ClassLoader beanClassLoader = beanClassLoaderReference.get();
if (applicationContext instanceof DefaultResourceLoader && beanClassLoader != null) {
@@ -372,25 +392,33 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
return applicationContext;
}
+ @Override
+ @SuppressWarnings("deprecation")
+ public void init(Properties parameters) {
+ init(CacheUtils.getCache(), parameters);
+ }
+
/**
- * Initializes a Spring ApplicationContext with the given parameters specified with a Pivotal GemFire <initializer>
- * block in cache.xml.
+ * Initializes a Spring {@link ApplicationContext} with the given parameters specified with an Apache Geode
+ * or Pivotal GemFire <initializer> block in {@literal cache.xml}.
*
- * @param parameters a Properties object containing the configuration parameters and settings defined in the
- * Pivotal GemFire cache.xml <initializer> block for the declared SpringContextBootstrappingInitializer
- * Pivotal GemFire Declarable object.
- * @throws org.springframework.context.ApplicationContextException if the Spring ApplicationContext could not be
- * successfully created, configured and initialized.
+ * @param parameters {@link Properties} object containing the configuration parameters and settings defined in the
+ * Apache Geode/Pivotal GemFire {@literal cache.xml} <initializer> block for the declared
+ * {@link SpringContextBootstrappingInitializer} Apache Geode/Pivotal GemFire {@link Declarable} object.
+ * @param cache reference to the peer {@link Cache}.
+ * @throws org.springframework.context.ApplicationContextException if the Spring {@link ApplicationContext}
+ * could not be successfully constructed, configured and initialized.
* @see #createApplicationContext(String[], String[])
* @see #initApplicationContext(org.springframework.context.ConfigurableApplicationContext)
* @see #refreshApplicationContext(org.springframework.context.ConfigurableApplicationContext)
* @see java.util.Properties
*/
- @Override
- public void init(Properties parameters) {
+ public void init(Cache cache, Properties parameters) {
+
try {
synchronized (SpringContextBootstrappingInitializer.class) {
- if (applicationContext == null || !applicationContext.isActive()) {
+ if (isApplicationContextInitializable()) {
+
String basePackages = parameters.getProperty(BASE_PACKAGES_PARAMETER);
String contextConfigLocations = parameters.getProperty(CONTEXT_CONFIG_LOCATIONS_PARAMETER);
@@ -419,6 +447,10 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
}
}
+ private static boolean isApplicationContextInitializable() {
+ return applicationContext == null || !applicationContext.isActive();
+ }
+
/**
* Null-safe operation used to get the ID of the Spring ApplicationContext.
*
@@ -427,7 +459,10 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
* @see org.springframework.context.ApplicationContext#getId()
*/
String nullSafeGetApplicationContextId(ApplicationContext applicationContext) {
- return (applicationContext != null ? applicationContext.getId() : null);
+
+ return applicationContext != null
+ ? applicationContext.getId()
+ : null;
}
/**
@@ -448,6 +483,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
*/
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
+
if (event instanceof ContextRefreshedEvent) {
synchronized (applicationEventNotifier) {
contextRefreshedEvent = (ContextRefreshedEvent) event;
diff --git a/src/main/java/org/springframework/data/gemfire/util/SpringUtils.java b/src/main/java/org/springframework/data/gemfire/util/SpringUtils.java
index 2274ad4d..1484b14f 100644
--- a/src/main/java/org/springframework/data/gemfire/util/SpringUtils.java
+++ b/src/main/java/org/springframework/data/gemfire/util/SpringUtils.java
@@ -27,6 +27,7 @@ import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
+import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -51,7 +52,7 @@ public abstract class SpringUtils {
Collections.addAll(dependsOnList, nullSafeArray(bean.getDependsOn(), String.class));
dependsOnList.addAll(Arrays.asList(nullSafeArray(beanNames, String.class)));
- bean.setDependsOn(dependsOnList.toArray(new String[dependsOnList.size()]));
+ bean.setDependsOn(dependsOnList.toArray(new String[0]));
return bean;
}
@@ -59,9 +60,9 @@ public abstract class SpringUtils {
public static Optional