org.springframework.data.gemfire.serialization.json.JSONRegionAdvice Maven / Gradle / Ivy
/*
* Copyright 2016-2020 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
*
* https://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 org.springframework.data.gemfire.serialization.json;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeList;
import static org.springframework.data.gemfire.util.RegionUtils.toRegionName;
import static org.springframework.data.gemfire.util.RegionUtils.toRegionPath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.query.SelectResults;
import org.apache.geode.cache.query.internal.ResultsBag;
import org.apache.geode.pdx.JSONFormatter;
import org.apache.geode.pdx.PdxInstance;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.util.CollectionUtils;
/**
* Spring/AspectJ AOP Aspect adapting a {@link Region} to handle JSON data.
*
* @author David Turanski
* @author John Blum
* @see org.apache.geode.cache.Region
* @see org.apache.geode.pdx.JSONFormatter
* @see org.apache.geode.pdx.PdxInstance
* @see org.aspectj.lang.annotation.Aspect
* @see org.aspectj.lang.annotation.Around
*/
@Aspect
@SuppressWarnings("unused")
public class JSONRegionAdvice {
private boolean convertReturnedCollections = true;
private boolean prettyPrint = false;
private List includedRegions = new ArrayList<>();
protected final Logger logger = LoggerFactory.getLogger(JSONRegionAdvice.class);
/**
* Flag to convert collections returned from cache from @{link PdxInstance} to JSON String. If the returned
* collections are very large, overhead will be incurred to covert all the values from from
* Region.getAll() and Region.values()
*
* @param convertReturnedCollections true by default
*/
public void setConvertReturnedCollections(boolean convertReturnedCollections) {
this.convertReturnedCollections = convertReturnedCollections;
}
/**
* Sets regions to be included for JSON conversion. By default, all regions will be included
*
* @param regions a List of region names to include
*/
public void setIncludedRegions(List> regions) {
nullSafeList(regions).forEach(region -> this.includedRegions.add(toRegionName(region)));
}
/**
* Sets names of regions to be included for JSON conversion. By default, all regions will be included
*
* @param regionNames a List of region names to include
*/
public void setIncludedRegionNames(List regionNames) {
this.includedRegions = nullSafeList(regionNames);
}
/**
* Flag to print JSON Strings with proper indentation, etc.
*
* @param prettyPrint false be default
*/
public void setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
}
@Around("execution(* org.apache.geode.cache.Region.get(..))"
+ " || execution(* org.apache.geode.cache.Region.remove(..))"
+ " || execution(* org.apache.geode.cache.Region.selectValue(..))")
public Object get(ProceedingJoinPoint pjp) {
Object returnValue = null;
try {
if (isIncludedJsonRegion(pjp.getTarget())) {
returnValue = pjp.proceed();
logger.debug("converting {} to JSON string", returnValue);
returnValue = convertToJson(returnValue);
}
else {
returnValue = pjp.proceed();
}
}
catch (Throwable cause) {
handleThrowable(cause);
}
return returnValue;
}
@SuppressWarnings("unchecked")
@Around("execution(* org.apache.geode.cache.Region.getAll(..))")
public Map