KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > bsh > BSHFilter


1 package org.jacorb.notification.filter.bsh;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.Date JavaDoc;
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 JavaDoc;
36 import org.omg.CosNotifyFilter.ConstraintExp;
37 import org.omg.PortableServer.POA JavaDoc;
38
39 import bsh.EvalError;
40 import bsh.Interpreter;
41
42 /**
43  * @author Alphonse Bendt
44  * @version $Id: BSHFilter.java,v 1.2 2005/02/14 00:05:37 alphonse.bendt Exp $
45  */

46 public class BSHFilter extends AbstractFilter
47 {
48     public static final String JavaDoc CONSTRAINT_GRAMMAR = "BSH";
49
50     public BSHFilter(Configuration config, EvaluationContextFactory evaluationContextFactory,
51             MessageFactory messageFactory, ORB JavaDoc orb, POA JavaDoc poa) throws ConfigurationException
52     {
53         super(config, evaluationContextFactory, messageFactory, orb, poa);
54     }
55
56     public String JavaDoc 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 JavaDoc 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                 // TODO import useful stuff
83
// predefine useful functions?
84
_interpreter.eval("import org.omg.CORBA.*;");
85
86                 _interpreter.set("event", message.toAny());
87                 _interpreter.set("date", new Date JavaDoc());
88                 _interpreter.set("constraint", constraint_);
89                 Object JavaDoc _result = _interpreter.eval(constraint_);
90
91                 if (_result == null)
92                 {
93                     return EvaluationResult.BOOL_FALSE;
94                 }
95
96                 if (_result instanceof Boolean JavaDoc)
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 JavaDoc)
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