Search in sources :

Example 1 with OgnlContext

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);
}
Also used : NamedAssociation(org.qi4j.api.association.NamedAssociation) OgnlContext(ognl.OgnlContext) OgnlException(ognl.OgnlException) ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) NamedAssociation(org.qi4j.api.association.NamedAssociation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Collection(java.util.Collection) Property(org.qi4j.api.property.Property)

Example 2 with OgnlContext

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);
    }
}
Also used : OgnlException(ognl.OgnlException) ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException) OgnlContext(ognl.OgnlContext)

Aggregations

OgnlContext (ognl.OgnlContext)2 OgnlException (ognl.OgnlException)2 Collection (java.util.Collection)1 ExpressionEvaluationException (org.apache.camel.ExpressionEvaluationException)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 NamedAssociation (org.qi4j.api.association.NamedAssociation)1 ConstraintViolation (org.qi4j.api.constraint.ConstraintViolation)1 ConstraintViolationException (org.qi4j.api.constraint.ConstraintViolationException)1 Property (org.qi4j.api.property.Property)1