com.github.aidensuen.mongo.mapping.ParameterHolder Maven / Gradle / Ivy
package com.github.aidensuen.mongo.mapping;
import com.github.aidensuen.mongo.session.Configuration;
import com.github.aidensuen.util.BeanWrapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.context.expression.EnvironmentAccessor;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.HashMap;
import java.util.Map;
public class ParameterHolder {
private static Logger LOGGER = LoggerFactory.getLogger(ParameterHolder.class);
public static final String PARAMETER_OBJECT_KEY = "@value";
private final BeanWrapper objectWrapper;
private final Map additionalParameters;
private final Configuration configuration;
private final EvaluationContext context;
private final EvaluationContext enviromentContext;
public ParameterHolder(Configuration configuration, Object parameterObject) {
this.objectWrapper = BeanWrapperUtil.initBeanWrapper(parameterObject);
this.configuration = configuration;
this.additionalParameters = new HashMap<>();
this.additionalParameters.put(PARAMETER_OBJECT_KEY, parameterObject);
this.context = new StandardEvaluationContext(parameterObject);
((StandardEvaluationContext) this.context).addPropertyAccessor(new MapAccessor());
this.enviromentContext = new StandardEvaluationContext(configuration.getEnvironment());
((StandardEvaluationContext) enviromentContext).addPropertyAccessor(new EnvironmentAccessor());
}
public BeanWrapper getObjectWrapper() {
return objectWrapper;
}
public Map getAdditionalParameters() {
return additionalParameters;
}
public Configuration getConfiguration() {
return configuration;
}
public EvaluationContext getContext() {
return context;
}
public boolean hasAdditionalParameter(String name) {
return this.additionalParameters.containsKey(name);
}
public void setAdditionalParameter(String name, Object value) {
this.additionalParameters.put(name, value);
}
public Object getAdditionalParameter(String name) {
return this.additionalParameters.get(name);
}
public void setContextVariable(String name, Object value) {
this.context.setVariable(name, value);
}
public Object getPropertyValue(String propertyName) throws Exception {
if (objectWrapper.isReadableProperty(propertyName)) {
return objectWrapper.getPropertyValue(propertyName);
}
Object result = this.getAdditionalParameter(propertyName);
if (result != null || this.additionalParameters.containsKey(propertyName)) {
return result;
}
LOGGER.debug("execute SpEL parse expression {}", propertyName);
ExpressionParser parser = configuration.getParser();
if (parser == null) {
parser = new SpelExpressionParser();
}
ParserContext parserContext = configuration.getParserContext();
Exception exception = null;
if (parserContext != null && propertyName.startsWith(parserContext.getExpressionPrefix()) && propertyName.endsWith(parserContext.getExpressionSuffix())) {
try {
result = parser.parseExpression(propertyName, parserContext).getValue(context);
} catch (Exception e) {
exception = e;
}
try {
result = parser.parseExpression(propertyName, parserContext).getValue(enviromentContext);
} catch (Exception e) {
exception = e;
}
} else {
try {
result = parser.parseExpression(propertyName).getValue(context);
} catch (Exception e) {
exception = e;
}
try {
result = parser.parseExpression(propertyName).getValue(enviromentContext);
} catch (Exception e) {
exception = e;
}
}
if (result == null) {
throw exception;
}
return result;
}
}