public void registerEvaluator(ExpressionEvaluator evaluator) { if (evaluator == null) { throw new IllegalArgumentException(CoreMessages.objectIsNull("evaluator").getMessage()); } final String name = evaluator.getName(); // TODO MULE-3809 Eliminate duplicate evaluators registration if (logger.isDebugEnabled()) { logger.debug( "Evaluators already contain an object named '" + name + "'. The previous object will be overwritten."); } evaluators.put(evaluator.getName(), evaluator); }
/** * Evaluates the given expression. The expression should be a single expression definition with or * without enclosing braces. i.e. "mule:serviceName" and "#[mule:serviceName]" are both valid. For * situations where one or more expressions need to be parsed within a single text, the {@link * org.mule.api.expression.ExpressionManager#parse(String,org.mule.api.MuleMessage,boolean)} * method should be used since it will iterate through all expressions in a string. * * @param expression a single expression i.e. xpath://foo * @param evaluator the evaluator to use when executing the expression * @param message the current message to process. The expression will evaluata on the message. * @param failIfNull determines if an exception should be thrown if expression could not be * evaluated or returns null or if an exception should be thrown if an empty collection is * returned. * @return the result of the evaluation. Expressions that return collection will return an empty * collection, not null. * @throws ExpressionRuntimeException if the expression is invalid, or a null is found for the * expression and 'failIfNull is set to true. */ public Object evaluate( String expression, String evaluator, MuleMessage message, boolean failIfNull) throws ExpressionRuntimeException { ExpressionEvaluator extractor = (ExpressionEvaluator) evaluators.get(evaluator); if (extractor == null) { throw new IllegalArgumentException( CoreMessages.expressionEvaluatorNotRegistered(evaluator).getMessage()); } Object result = extractor.evaluate(expression, message); // TODO Handle empty collections || (result instanceof Collection && // ((Collection)result).size()==0) if (failIfNull && (result == null)) { throw new ExpressionRuntimeException( CoreMessages.expressionEvaluatorReturnedNull(evaluator, expression)); } if (logger.isDebugEnabled()) { logger.debug( MessageFormat.format( "Result of expression: {0}:{1} is: {2}", evaluator, expression, result)); } return result; }