package io.cozmic.usher.pipeline;

import de.odysseus.el.util.SimpleContext;
import io.cozmic.usher.core.MessageMatcher;
import io.cozmic.usher.message.PipelinePack;

import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

/**
 * Created by chuck on 7/3/15.
 */
public class JuelMatcher implements MessageMatcher {

    private static ExpressionFactory factory = ExpressionFactory.newInstance();
    private SimpleContext context = new SimpleContext();
    private ValueExpression expression;

    public JuelMatcher(String expressionVal) {
        expression = factory.createValueExpression(context, expressionVal, boolean.class);
    }

    @Override
    public boolean matches(PipelinePack pipelinePack) {
        SimpleContext runtimeContext = new SimpleContext();
        final Object msg = pipelinePack.getMessage();
        factory.createValueExpression(runtimeContext, "${msgClassSimpleName}", String.class).setValue(runtimeContext, msg.getClass().getSimpleName());
        factory.createValueExpression(runtimeContext, "${msg}", msg.getClass()).setValue(runtimeContext, msg);
        return (boolean) expression.getValue(runtimeContext);
    }
}

@@ -825,24 +825,55 @@
 tMessage();%0A
+        if (msg != null) %7B%0A    
         fact
@@ -1011,32 +1011,36 @@
 ame());%0A        
+    
 factory.createVa
@@ -1126,16 +1126,26 @@
 , msg);%0A
+        %7D%0A
         

Fix messageMatcher in situations where message is null