Search in sources :

Example 1 with OgnlException

use of ognl.OgnlException in project pentaho-kettle by pentaho.

the class PropertySetter method setOgnlProperty.

private Object setOgnlProperty(String[] expression, String value) throws KettleConfigException {
    if (expression.length >= 2) {
        OgnlExpression expr = ognlExpressions.get(value);
        if (expr == null) {
            synchronized (ognlExpressions) {
                try {
                    expr = new OgnlExpression(expression[1]);
                    ognlExpressions.put(value, expr);
                } catch (OgnlException e) {
                    throw new KettleConfigException("Unable to parse expression [" + expression[1] + "] with Ognl", e);
                }
            }
        }
        // evaluate
        try {
            return expr.getValue(octx, this);
        } catch (OgnlException e) {
            throw new KettleConfigException("Unable to get value for expression [" + expression[1] + "] with Ognl", e);
        }
    } else {
        throw new KettleConfigException("the ognl, directive need at least 2 parameters: ongl and the expression but " + expression.length + " parameters were found in [" + value + "]");
    }
}
Also used : OgnlException(ognl.OgnlException) KettleConfigException(org.pentaho.di.core.exception.KettleConfigException)

Example 2 with OgnlException

use of ognl.OgnlException in project stdlib by petergeneric.

the class RulesEngineImpl method run.

@Override
public void run(final Rules rules, boolean ignoreMethodErrors) throws OgnlException {
    Map<String, Object> vars = prepare(rules);
    for (RuleSet rs : rules.ruleSets) {
        try {
            log.debug("Assessing input for ruleset : " + rs.id);
            OgnlContext rsContext = createContext(vars);
            rs.runInput(rsContext);
            for (Rule rule : rs.rules) {
                if (StringUtils.isEmpty(rule.id)) {
                    throw new IllegalArgumentException("Rule with condition " + rule.condition + " has no id!");
                }
                final RuleProcessTask task = new RuleProcessTask(this, rs, rule, rsContext);
                synchronized (activeTasks) {
                    // if not currently being processed
                    if (!activeTasks.containsKey(rule.id)) {
                        try {
                            activeTasks.put(rule.id, task);
                            task.submit(executorService);
                        } catch (Exception e) {
                            log.error("Error submitting rule for execution ", e);
                            activeTasks.remove(rule.id);
                        }
                    }
                }
            }
        } catch (MethodFailedException mfe) {
            if (!ignoreMethodErrors) {
                throw mfe;
            }
            log.warn("Method failed for ruleset " + rs.id, mfe);
        }
    }
}
Also used : RuleSet(com.peterphi.rules.types.RuleSet) Rule(com.peterphi.rules.types.Rule) MethodFailedException(ognl.MethodFailedException) MethodFailedException(ognl.MethodFailedException) OgnlException(ognl.OgnlException) OgnlContext(ognl.OgnlContext)

Example 3 with OgnlException

use of ognl.OgnlException in project thymeleaf by thymeleaf.

the class OGNLContextPropertyAccessor method getProperty.

public Object getProperty(final Map ognlContext, final Object target, final Object name) throws OgnlException {
    if (!(target instanceof IContext)) {
        throw new IllegalStateException("Wrong target type. This property accessor is only usable for " + IContext.class.getName() + " implementations, and " + "in this case the target object is " + (target == null ? "null" : ("of class " + target.getClass().getName())));
    }
    if (REQUEST_PARAMETERS_RESTRICTED_VARIABLE_NAME.equals(name) && ognlContext != null && ognlContext.containsKey(RESTRICT_REQUEST_PARAMETERS)) {
        throw new OgnlException("Access to variable \"" + name + "\" is forbidden in this context. Note some restrictions apply to " + "variable access. For example, direct access to request parameters is forbidden in preprocessing and " + "unescaped expressions, in TEXT template mode, in fragment insertion specifications and " + "in some specific attribute processors.");
    }
    final String propertyName = (name == null ? null : name.toString());
    // 'execInfo' translation from context variable to expression object - deprecated and to be removed in 3.1
    final Object execInfoResult = checkExecInfo(propertyName, ognlContext);
    if (execInfoResult != null) {
        return execInfoResult;
    }
    /*
         * NOTE we do not check here whether we are being asked for the 'locale', 'request', 'response', etc.
         * because there already are specific expression objects for the most important of them, which should
         * be used instead: #locale, #httpServletRequest, #httpSession, etc.
         * The variables maps should just be used as a map, without exposure of its more-internal methods...
         */
    final IContext context = (IContext) target;
    return context.getVariable(propertyName);
}
Also used : OgnlException(ognl.OgnlException) IContext(org.thymeleaf.context.IContext)

Example 4 with OgnlException

use of ognl.OgnlException in project thymeleaf by thymeleaf.

the class OGNLShortcutExpression method evaluate.

Object evaluate(final IEngineConfiguration configuration, final Map<String, Object> context, final Object root) throws Exception {
    final ICacheManager cacheManager = configuration.getCacheManager();
    final ICache<ExpressionCacheKey, Object> expressionCache = (cacheManager == null ? null : cacheManager.getExpressionCache());
    Object target = root;
    for (final String propertyName : this.expressionLevels) {
        // If target is null, we will mimic what OGNL does in these cases...
        if (target == null) {
            throw new OgnlException("source is null for getProperty(null, \"" + propertyName + "\")");
        }
        // For the best integration possible, we will ask OGNL which property accessor it would use for
        // this target object, and then depending on the result apply our equivalent or just default to
        // OGNL evaluation if it is a custom property accessor we do not implement.
        final Class<?> targetClass = OgnlRuntime.getTargetClass(target);
        final PropertyAccessor ognlPropertyAccessor = OgnlRuntime.getPropertyAccessor(targetClass);
        // Depending on the returned OGNL property accessor, we will try to apply ours
        if (target instanceof Class<?>) {
            // Because of the way OGNL works, the "OgnlRuntime.getTargetClass(...)" of a Class object is the class
            // object itself, so we might be trying to apply a PropertyAccessor to a Class instead of a real object,
            // something we avoid by means of this shortcut
            target = getObjectProperty(expressionCache, propertyName, target);
        } else if (OGNLContextPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getContextProperty(propertyName, context, target);
        } else if (ObjectPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getObjectProperty(expressionCache, propertyName, target);
        } else if (MapPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getMapProperty(propertyName, (Map<?, ?>) target);
        } else if (ListPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getListProperty(expressionCache, propertyName, (List<?>) target);
        } else if (SetPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getSetProperty(expressionCache, propertyName, (Set<?>) target);
        } else if (IteratorPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getIteratorProperty(expressionCache, propertyName, (Iterator<?>) target);
        } else if (EnumerationPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getEnumerationProperty(expressionCache, propertyName, (Enumeration<?>) target);
        } else if (ArrayPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getArrayProperty(expressionCache, propertyName, (Object[]) target);
        } else {
            // default to normal OGNL evaluation.
            throw new OGNLShortcutExpressionNotApplicableException();
        }
    }
    return target;
}
Also used : EnumerationPropertyAccessor(ognl.EnumerationPropertyAccessor) PropertyAccessor(ognl.PropertyAccessor) ListPropertyAccessor(ognl.ListPropertyAccessor) ObjectPropertyAccessor(ognl.ObjectPropertyAccessor) MapPropertyAccessor(ognl.MapPropertyAccessor) ArrayPropertyAccessor(ognl.ArrayPropertyAccessor) EnumerationPropertyAccessor(ognl.EnumerationPropertyAccessor) IteratorPropertyAccessor(ognl.IteratorPropertyAccessor) SetPropertyAccessor(ognl.SetPropertyAccessor) ExpressionCacheKey(org.thymeleaf.cache.ExpressionCacheKey) MapPropertyAccessor(ognl.MapPropertyAccessor) OgnlException(ognl.OgnlException) Iterator(java.util.Iterator) List(java.util.List) SetPropertyAccessor(ognl.SetPropertyAccessor) ICacheManager(org.thymeleaf.cache.ICacheManager)

Example 5 with OgnlException

use of ognl.OgnlException 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

OgnlException (ognl.OgnlException)6 OgnlContext (ognl.OgnlContext)3 AttributeFieldError (com.agiletec.aps.system.common.entity.model.AttributeFieldError)1 Rule (com.peterphi.rules.types.Rule)1 RuleSet (com.peterphi.rules.types.RuleSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ArrayPropertyAccessor (ognl.ArrayPropertyAccessor)1 EnumerationPropertyAccessor (ognl.EnumerationPropertyAccessor)1 IteratorPropertyAccessor (ognl.IteratorPropertyAccessor)1 ListPropertyAccessor (ognl.ListPropertyAccessor)1 MapPropertyAccessor (ognl.MapPropertyAccessor)1 MethodFailedException (ognl.MethodFailedException)1 ObjectPropertyAccessor (ognl.ObjectPropertyAccessor)1 PropertyAccessor (ognl.PropertyAccessor)1 SetPropertyAccessor (ognl.SetPropertyAccessor)1 ExpressionEvaluationException (org.apache.camel.ExpressionEvaluationException)1 KettleConfigException (org.pentaho.di.core.exception.KettleConfigException)1 ExpressionCacheKey (org.thymeleaf.cache.ExpressionCacheKey)1 ICacheManager (org.thymeleaf.cache.ICacheManager)1