KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > filters > OGNLFilter


1 /*
2  * $Id: OGNLFilter.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.filters;
12
13 import ognl.Ognl;
14 import ognl.OgnlException;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.umo.UMOFilter;
19 import org.mule.umo.UMOMessage;
20
21 public class OGNLFilter implements UMOFilter
22 {
23     private static final Log logger = LogFactory.getLog(OGNLFilter.class);
24
25     private String JavaDoc expression;
26
27     public String JavaDoc getExpression()
28     {
29         return expression;
30     }
31
32     public void setExpression(String JavaDoc expression)
33     {
34         this.expression = expression;
35     }
36
37     public boolean accept(UMOMessage message)
38     {
39         if (message == null)
40         {
41             return false;
42         }
43
44         Object JavaDoc candidate = message.getPayload();
45         if (candidate == null)
46         {
47             return false;
48         }
49
50         if (expression == null)
51         {
52             logger.warn("Expression for OGNLFilter is not set");
53             return false;
54         }
55
56         try
57         {
58             Object JavaDoc result = Ognl.getValue(expression, candidate);
59             if (result instanceof Boolean JavaDoc)
60             {
61                 return ((Boolean JavaDoc)result).booleanValue();
62             }
63         }
64
65         catch (OgnlException ex)
66         {
67             logger.error("Error evaluating OGNL expression.", ex);
68         }
69
70         // default: reject
71
return false;
72     }
73
74 }
75
Popular Tags