use of ognl.OgnlContext in project qi4j-sdk by Qi4j.
the class Qi4jPropertyAccessor method setProperty.
@Override
@SuppressWarnings("unchecked")
public final void setProperty(Map aContext, Object aTarget, Object aPropertyName, Object aPropertyValue) throws OgnlException {
String fieldName = aPropertyName.toString();
Object qi4jField = getQi4jField(aContext, aTarget, fieldName);
if (qi4jField != null) {
Class<?> memberClass = qi4jField.getClass();
if (Property.class.isAssignableFrom(memberClass)) {
Property property = (Property) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class<?> propertyType = (Class) api.propertyDescriptorFor(property).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, propertyType);
try {
property.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, convertedValue, violations);
}
return;
} else if (Association.class.isAssignableFrom(memberClass)) {
Association association = (Association) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class<?> associationType = (Class) api.associationDescriptorFor(association).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, associationType);
if (convertedValue == OgnlRuntime.NoConversionPossible) {
throw new OgnlException("Could not convert value to association type");
}
try {
association.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, aPropertyValue, violations);
}
return;
} else if (ManyAssociation.class.isAssignableFrom(memberClass)) {
throw new OgnlException("Setting many association [" + fieldName + "] is impossible.");
} else if (NamedAssociation.class.isAssignableFrom(memberClass)) {
throw new OgnlException("Setting named association [" + fieldName + "] is impossible.");
}
}
super.setProperty(aContext, aTarget, aPropertyName, aPropertyValue);
}
use of ognl.OgnlContext in project camel by apache.
the class OgnlExpression method evaluate.
public <T> T evaluate(Exchange exchange, Class<T> tClass) {
OgnlContext oglContext = new OgnlContext();
// setup the class resolver from camel
oglContext.setClassResolver(new CamelClassResolver(exchange.getContext().getClassResolver()));
try {
Object value = Ognl.getValue(expression, oglContext, new RootObject(exchange));
return exchange.getContext().getTypeConverter().convertTo(tClass, value);
} catch (OgnlException e) {
throw new ExpressionEvaluationException(this, exchange, e);
}
}
Aggregations