INT-3850: Fix SpEL usage in the CacheWritingMH
JIRA: https://jira.spring.io/browse/INT-3850 **Cherry-pick to 4.1.x, 4.0.x, 3.0.x**
This commit is contained in:
committed by
Gary Russell
parent
b6467e1329
commit
bbfde40694
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -21,37 +21,46 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.gemstone.gemfire.GemFireCheckedException;
|
||||
import com.gemstone.gemfire.GemFireException;
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
import org.springframework.data.gemfire.GemfireCallback;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.expression.ExpressionUtils;
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.gemstone.gemfire.GemFireCheckedException;
|
||||
import com.gemstone.gemfire.GemFireException;
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* A {@link MessageHandler} implementation that writes to a GemFire Region. The
|
||||
* Message's payload must be an instance of java.util.Map.
|
||||
* Message's payload must be an instance of {@link Map} or {@link #cacheEntryExpressions}
|
||||
* must be provided.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author David Turanski
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public class CacheWritingMessageHandler extends AbstractMessageHandler {
|
||||
|
||||
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
|
||||
|
||||
private final Map<Expression, Expression> cacheEntryExpressions = new LinkedHashMap<Expression, Expression>();
|
||||
|
||||
private final GemfireTemplate gemfireTemplate = new GemfireTemplate();
|
||||
|
||||
private volatile EvaluationContext evaluationContext;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public CacheWritingMessageHandler(Region region) {
|
||||
Assert.notNull(region, "region must not be null");
|
||||
this.gemfireTemplate.setRegion(region);
|
||||
this.gemfireTemplate.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,52 +68,70 @@ public class CacheWritingMessageHandler extends AbstractMessageHandler {
|
||||
return "gemfire:outbound-channel-adapter";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void handleMessageInternal(Message<?> message) {
|
||||
protected void onInit() throws Exception {
|
||||
super.onInit();
|
||||
this.gemfireTemplate.afterPropertiesSet();
|
||||
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
protected void handleMessageInternal(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
Map<?, ?> cacheValues = (cacheEntryExpressions.size() > 0) ? parseCacheEntries(message) : null;
|
||||
Map<?, ?> cacheValues = (this.cacheEntryExpressions.size() > 0) ? evaluateCacheEntries(message) : null;
|
||||
|
||||
if (cacheValues == null) {
|
||||
Assert.isTrue(payload instanceof Map,
|
||||
Assert.state(payload instanceof Map,
|
||||
"If cache entry expressions are not configured, then payload must be a Map");
|
||||
cacheValues = (Map<?, ?>) payload;
|
||||
cacheValues = (Map) payload;
|
||||
}
|
||||
|
||||
final Map<?, ?> map = cacheValues;
|
||||
|
||||
this.gemfireTemplate.execute(new GemfireCallback<Object>() {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public Object doInGemfire(Region region) throws GemFireCheckedException, GemFireException {
|
||||
region.putAll(map);
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private Map<Object, Object> parseCacheEntries(Message<?> message) {
|
||||
if (cacheEntryExpressions.size() == 0) {
|
||||
private Map<Object, Object> evaluateCacheEntries(Message<?> message) {
|
||||
if (this.cacheEntryExpressions.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
Map<Object, Object> cacheValues = new HashMap<Object, Object>();
|
||||
for (Entry<Expression, Expression> expressionEntry : cacheEntryExpressions.entrySet()) {
|
||||
cacheValues.put(expressionEntry.getKey().getValue(message), expressionEntry.getValue().getValue(message));
|
||||
for (Entry<Expression, Expression> expressionEntry : this.cacheEntryExpressions.entrySet()) {
|
||||
cacheValues.put(expressionEntry.getKey().getValue(this.evaluationContext, message),
|
||||
expressionEntry.getValue().getValue(this.evaluationContext, message));
|
||||
}
|
||||
return cacheValues;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCacheEntries(Map<String, String> cacheEntries) {
|
||||
|
||||
if (cacheEntryExpressions.size() > 0) {
|
||||
cacheEntryExpressions.clear();
|
||||
Assert.notNull(cacheEntries, "'cacheEntries' must not be null");
|
||||
if (this.cacheEntryExpressions.size() > 0) {
|
||||
this.cacheEntryExpressions.clear();
|
||||
}
|
||||
|
||||
for (Entry<String, String> cacheEntry : cacheEntries.entrySet()) {
|
||||
this.cacheEntryExpressions.put(new SpelExpressionParser().parseExpression(cacheEntry.getKey()),
|
||||
new SpelExpressionParser().parseExpression(cacheEntry.getValue()));
|
||||
this.cacheEntryExpressions.put(PARSER.parseExpression(cacheEntry.getKey()),
|
||||
PARSER.parseExpression(cacheEntry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public void setCacheEntryExpressions(Map<Expression, Expression> cacheEntryExpressions) {
|
||||
Assert.notNull(cacheEntryExpressions, "'cacheEntryExpressions' must not be null");
|
||||
if (this.cacheEntryExpressions.size() > 0) {
|
||||
this.cacheEntryExpressions.clear();
|
||||
}
|
||||
this.cacheEntryExpressions.putAll(cacheEntryExpressions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user