From 6c647d29624938b8abe44c73ffdc0e7dedeb2402 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 23 May 2018 13:34:17 -0700 Subject: [PATCH] DATAGEODE-109 - Reformat source code. Use try multi-catch blocks for simpler (GemFire/Geode) Exception handling. --- .../data/gemfire/GemfireTemplate.java | 210 +++++++++--------- 1 file changed, 109 insertions(+), 101 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java index c01ce9f8..2154393b 100644 --- a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java +++ b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java @@ -70,8 +70,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation private Region regionProxy; - public GemfireTemplate() { - } + public GemfireTemplate() { } public GemfireTemplate(Region region) { setRegion(region); @@ -80,8 +79,10 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation @Override public void afterPropertiesSet() { + super.afterPropertiesSet(); - regionProxy = createRegionProxy(getRegion()); + + this.regionProxy = createRegionProxy(getRegion()); } /** @@ -145,11 +146,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public void create(K key, V value) { + try { getRegion().create(key, value); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -158,11 +160,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public V get(K key) { + try { return this.getRegion().get(key); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -171,11 +174,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public Map getAll(Collection keys) { + try { return this.getRegion().getAll(keys); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -184,11 +188,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public V put(K key, V value) { + try { return this.getRegion().put(key, value); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -197,11 +202,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public void putAll(Map map) { + try { this.getRegion().putAll(map); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -210,11 +216,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public V putIfAbsent(K key, V value) { + try { return this.getRegion().putIfAbsent(key, value); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -223,11 +230,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public V remove(K key) { + try { return this.getRegion().remove(key); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -236,11 +244,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public V replace(K key, V value) { + try { return this.getRegion().replace(key, value); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -249,11 +258,12 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public boolean replace(K key, V oldValue, V newValue) { + try { return this.getRegion().replace(key, oldValue, newValue); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } } @@ -263,29 +273,26 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public SelectResults query(String query) { + try { return this.getRegion().query(query); } - catch (IndexInvalidException e) { - throw convertGemFireQueryException(e); + catch (IndexInvalidException | QueryInvalidException cause) { + throw convertGemFireQueryException(cause); } - catch (QueryInvalidException e) { - throw convertGemFireQueryException(e); + catch (GemFireCheckedException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireCheckedException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); - } - catch (RuntimeException ex) { - // test for CqInvalidException (removed in 6.5) - if (GemfireCacheUtils.isCqInvalidException(ex)) { - throw GemfireCacheUtils.convertCqInvalidException(ex); + catch (RuntimeException cause) { + + if (GemfireCacheUtils.isCqInvalidException(cause)) { + throw GemfireCacheUtils.convertCqInvalidException(cause); } - // callback code threw application exception - throw ex; + throw cause; } } @@ -296,7 +303,9 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation @Override @SuppressWarnings("unchecked") public SelectResults find(String queryString, Object... params) throws InvalidDataAccessApiUsageException { + try { + QueryService queryService = resolveQueryService(getRegion()); Query query = queryService.newQuery(queryString); Object result = query.execute(params); @@ -310,23 +319,22 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation queryString, result)); } } - catch (IndexInvalidException | QueryInvalidException e) { - throw convertGemFireQueryException(e); + catch (IndexInvalidException | QueryInvalidException cause) { + throw convertGemFireQueryException(cause); } - catch (GemFireCheckedException e) { - throw convertGemFireAccessException(e); + catch (GemFireCheckedException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); + catch (GemFireException caue) { + throw convertGemFireAccessException(caue); } - catch (RuntimeException e) { - // test for CqInvalidException (removed in 6.5) - if (GemfireCacheUtils.isCqInvalidException(e)) { - throw GemfireCacheUtils.convertCqInvalidException(e); + catch (RuntimeException cause) { + + if (GemfireCacheUtils.isCqInvalidException(cause)) { + throw GemfireCacheUtils.convertCqInvalidException(cause); } - // callback code threw application exception - throw e; + throw cause; } } @@ -337,13 +345,17 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation @Override @SuppressWarnings("unchecked") public T findUnique(String queryString, Object... params) throws InvalidDataAccessApiUsageException { + try { + QueryService queryService = resolveQueryService(getRegion()); Query query = queryService.newQuery(queryString); Object result = query.execute(params); if (result instanceof SelectResults) { + SelectResults selectResults = (SelectResults) result; + List results = selectResults.asList(); if (results.size() == 1) { @@ -357,26 +369,22 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation return (T) result; } - catch (IndexInvalidException ex) { - throw convertGemFireQueryException(ex); + catch (IndexInvalidException | QueryInvalidException cause) { + throw convertGemFireQueryException(cause); } - catch (QueryInvalidException ex) { - throw convertGemFireQueryException(ex); + catch (GemFireCheckedException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireCheckedException e) { - throw convertGemFireAccessException(e); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireException e) { - throw convertGemFireAccessException(e); - } - catch (RuntimeException ex) { - // test for CqInvalidException (removed in 6.5) - if (GemfireCacheUtils.isCqInvalidException(ex)) { - throw GemfireCacheUtils.convertCqInvalidException(ex); + catch (RuntimeException cause) { + + if (GemfireCacheUtils.isCqInvalidException(cause)) { + throw GemfireCacheUtils.convertCqInvalidException(cause); } - // callback code threw application exception - throw ex; + throw cause; } } @@ -391,39 +399,37 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation * @see org.apache.geode.cache.client.ClientCache#getLocalQueryService() */ protected QueryService resolveQueryService(Region region) { - return (region.getRegionService() instanceof ClientCache ? resolveClientQueryService(region) + + return region.getRegionService() instanceof ClientCache + ? resolveClientQueryService(region) + : queryServiceFrom(region); + } + + QueryService resolveClientQueryService(Region region) { + + ClientCache clientCache = (ClientCache) region.getRegionService(); + + return requiresLocalQueryService(region) ? clientCache.getLocalQueryService() + : (requiresPooledQueryService(region) ? clientCache.getQueryService(poolNameFrom(region)) : queryServiceFrom(region)); } - /* (non-Javadoc) */ - QueryService resolveClientQueryService(Region region) { - ClientCache clientCache = (ClientCache) region.getRegionService(); - - return (requiresLocalQueryService(region) ? clientCache.getLocalQueryService() - : (requiresPooledQueryService(region) ? clientCache.getQueryService(poolNameFrom(region)) - : queryServiceFrom(region))); - } - - /* (non-Javadoc) */ boolean requiresLocalQueryService(Region region) { - return (Scope.LOCAL.equals(region.getAttributes().getScope()) && isLocalWithNoServerProxy(region)); + return Scope.LOCAL.equals(region.getAttributes().getScope()) && isLocalWithNoServerProxy(region); } - /* (non-Javadoc) */ boolean isLocalWithNoServerProxy(Region region) { - return (region instanceof LocalRegion && !((LocalRegion) region).hasServerProxy()); + return region instanceof LocalRegion && !((LocalRegion) region).hasServerProxy(); } boolean requiresPooledQueryService(Region region) { return StringUtils.hasText(poolNameFrom(region)); } - /* (non-Javadoc) */ QueryService queryServiceFrom(Region region) { return region.getRegionService().getQueryService(); } - /* (non-Javadoc) */ String poolNameFrom(Region region) { return region.getAttributes().getPoolName(); } @@ -443,33 +449,31 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @Override public T execute(GemfireCallback action, boolean exposeNativeRegion) throws DataAccessException { + Assert.notNull(action, "Callback object must not be null"); try { + Region regionArgument = (exposeNativeRegion ? getRegion() : regionProxy); return action.doInGemfire(regionArgument); } - catch (IndexInvalidException ex) { - throw convertGemFireQueryException(ex); + catch (IndexInvalidException | QueryInvalidException cause) { + throw convertGemFireQueryException(cause); } - catch (QueryInvalidException ex) { - throw convertGemFireQueryException(ex); + catch (GemFireCheckedException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireCheckedException ex) { - throw convertGemFireAccessException(ex); + catch (GemFireException cause) { + throw convertGemFireAccessException(cause); } - catch (GemFireException ex) { - throw convertGemFireAccessException(ex); - } - catch (RuntimeException ex) { - // test for CqInvalidException (removed in 6.5) - if (GemfireCacheUtils.isCqInvalidException(ex)) { - throw GemfireCacheUtils.convertCqInvalidException(ex); + catch (RuntimeException cause) { + + if (GemfireCacheUtils.isCqInvalidException(cause)) { + throw GemfireCacheUtils.convertCqInvalidException(cause); } - // callback code threw application exception - throw ex; + throw cause; } } @@ -486,6 +490,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation */ @SuppressWarnings("unchecked") protected Region createRegionProxy(Region region) { + Class regionType = region.getClass(); return (Region) Proxy.newProxyInstance(regionType.getClassLoader(), @@ -503,22 +508,25 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation private final Region target; - public RegionCloseSuppressingInvocationHandler(final Region target) { - Assert.notNull(target, "The Region to target must not be null."); + public RegionCloseSuppressingInvocationHandler(Region target) { + + Assert.notNull(target, "Target Region must not be null"); + this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - if (method.getName().equals("equals")) { + + if ("equals".equals(method.getName())) { // only consider equal when proxies are identical - return (proxy == args[0]); + return proxy == args[0]; } - else if (method.getName().equals("hashCode")) { + else if ("hashCode".equals(method.getName())) { // use hashCode of Region proxy return System.identityHashCode(proxy); } - else if (method.getName().equals("close")) { - // suppress Region.close() method call + else if ("close".equals(method.getName())) { + // suppress Region.close() return null; } else {