1 package org.jacorb.notification.filter.bsh; 2 3 22 23 import java.util.Date ; 24 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.jacorb.notification.MessageFactory; 28 import org.jacorb.notification.filter.AbstractFilter; 29 import org.jacorb.notification.filter.EvaluationContext; 30 import org.jacorb.notification.filter.EvaluationException; 31 import org.jacorb.notification.filter.EvaluationResult; 32 import org.jacorb.notification.filter.FilterConstraint; 33 import org.jacorb.notification.interfaces.EvaluationContextFactory; 34 import org.jacorb.notification.interfaces.Message; 35 import org.omg.CORBA.ORB ; 36 import org.omg.CosNotifyFilter.ConstraintExp; 37 import org.omg.PortableServer.POA ; 38 39 import bsh.EvalError; 40 import bsh.Interpreter; 41 42 46 public class BSHFilter extends AbstractFilter 47 { 48 public static final String CONSTRAINT_GRAMMAR = "BSH"; 49 50 public BSHFilter(Configuration config, EvaluationContextFactory evaluationContextFactory, 51 MessageFactory messageFactory, ORB orb, POA poa) throws ConfigurationException 52 { 53 super(config, evaluationContextFactory, messageFactory, orb, poa); 54 } 55 56 public String constraint_grammar() 57 { 58 return CONSTRAINT_GRAMMAR; 59 } 60 61 public FilterConstraint newFilterConstraint(ConstraintExp constraintExp) 62 { 63 return new BSHFilterConstraint(constraintExp); 64 } 65 66 private static class BSHFilterConstraint implements FilterConstraint 67 { 68 private final String constraint_; 69 70 BSHFilterConstraint(ConstraintExp constraintExp) 71 { 72 constraint_ = constraintExp.constraint_expr; 73 } 74 75 public EvaluationResult evaluate(EvaluationContext context, Message message) 76 throws EvaluationException 77 { 78 try 79 { 80 Interpreter _interpreter = new Interpreter(); 81 82 _interpreter.eval("import org.omg.CORBA.*;"); 85 86 _interpreter.set("event", message.toAny()); 87 _interpreter.set("date", new Date ()); 88 _interpreter.set("constraint", constraint_); 89 Object _result = _interpreter.eval(constraint_); 90 91 if (_result == null) 92 { 93 return EvaluationResult.BOOL_FALSE; 94 } 95 96 if (_result instanceof Boolean ) 97 { 98 if (_result.equals(Boolean.TRUE)) 99 { 100 return EvaluationResult.BOOL_TRUE; 101 } 102 103 return EvaluationResult.BOOL_FALSE; 104 } 105 106 if (_result instanceof String ) 107 { 108 if ("".equals(_result)) 109 { 110 return EvaluationResult.BOOL_FALSE; 111 } 112 return EvaluationResult.BOOL_TRUE; 113 } 114 115 return EvaluationResult.BOOL_TRUE; 116 } catch (EvalError e) 117 { 118 throw new EvaluationException(e); 119 } 120 } 121 } 122 } | Popular Tags |