org.springframework.data.gemfire.support.JSONRegionAdvice Maven / Gradle / Ivy
/*
* Copyright 2002-2013 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 org.springframework.data.gemfire.support;
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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.util.CollectionUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.internal.ResultsBag;
import com.gemstone.gemfire.pdx.JSONFormatter;
import com.gemstone.gemfire.pdx.PdxInstance;
/**
* @author David Turanski
*/
@Aspect
@SuppressWarnings("unused")
public class JSONRegionAdvice {
private boolean convertReturnedCollections = true;
private boolean prettyPrint = false;
private List includedRegions;
protected final Log log = LogFactory.getLog(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) {
this.includedRegions = new ArrayList();
for (Region region : regions) {
includedRegions.add(region.getName());
}
}
/**
* 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 = 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(* com.gemstone.gemfire.cache.Region.put(..)) || "
+ "execution(* com.gemstone.gemfire.cache.Region.create(..)) ||"
+ "execution(* com.gemstone.gemfire.cache.Region.putIfAbsent(..)) ||"
+ "execution(* com.gemstone.gemfire.cache.Region.replace(..))")
public Object put(ProceedingJoinPoint pjp) {
boolean JSONRegion = isIncludedSONRegion(pjp.getTarget());
Object returnValue = null;
try {
if (JSONRegion) {
Object[] newArgs = Arrays.copyOf(pjp.getArgs(), pjp.getArgs().length);
Object val = newArgs[1];
newArgs[1] = convertArgumentToPdxInstance(val);
returnValue = pjp.proceed(newArgs);
log.debug("converting " + returnValue + " to JSON string");
returnValue = convertPdxInstanceToJSONString(returnValue);
}
else {
returnValue = pjp.proceed();
}
}
catch (Throwable t) {
handleThrowable(t);
}
return returnValue;
}
@Around("execution(* com.gemstone.gemfire.cache.Region.putAll(..))")
public Object putAll(ProceedingJoinPoint pjp) {
boolean JSONRegion = isIncludedSONRegion(pjp.getTarget());
Object returnValue = null;
try {
if (JSONRegion) {
Object[] newArgs = Arrays.copyOf(pjp.getArgs(), pjp.getArgs().length);
Map val = (Map) newArgs[0];
Map