KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > interceptors > JXPathNormalizerInterceptor


1 /*
2  * $Id: JXPathNormalizerInterceptor.java 3937 2006-11-20 16:04:25Z lajos $
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.interceptors;
12
13 import org.apache.commons.jxpath.JXPathContext;
14 import org.mule.impl.MuleMessage;
15 import org.mule.umo.Invocation;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.util.StringUtils;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * <code>JXPathNormalizerInterceptor</code> can be used as a simple pre/post
25  * message transformer for a given component. <p/> Users can set JXPath expressions
26  * to execute before and after the component reeives the event. The
27  * <i>beforeExpressions</i> can be a single expression or a comma separated list of
28  * expressions, each of which result in an object that will be used as an argument to
29  * the method called on the component. The <i>afterExpression</i> is a single
30  * expression that will be used to set a value on the orginal payload.
31  */

32 public class JXPathNormalizerInterceptor extends MessageNormalizerInterceptor
33 {
34     private List JavaDoc beforeExpressionsList;
35     private String JavaDoc beforeExpressions;
36     private String JavaDoc afterExpression;
37
38     /**
39      * This method is invoked before the event is processed
40      *
41      * @param invocation the message invocation being processed
42      */

43     public UMOMessage before(Invocation invocation) throws UMOException
44     {
45         if (beforeExpressions != null && beforeExpressionsList.size() > 0)
46         {
47             JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
48             Object JavaDoc[] result = new Object JavaDoc[beforeExpressionsList.size()];
49             for (int i = 0; i < result.length; i++)
50             {
51                 result[i] = ctx.getValue((String JavaDoc)beforeExpressionsList.get(i));
52             }
53             if (result.length == 1)
54             {
55                 return new MuleMessage(result[0], invocation.getMessage());
56             }
57             else
58             {
59                 return new MuleMessage(result, invocation.getMessage());
60             }
61         }
62         return null;
63     }
64
65     /**
66      * This method is invoked after the event has been processed
67      *
68      * @param invocation the message invocation being processed
69      */

70     public UMOMessage after(Invocation invocation) throws UMOException
71     {
72         if (afterExpression != null)
73         {
74             JXPathContext ctx = JXPathContext.newContext(getOriginalPayload());
75             ctx.setValue(afterExpression, invocation.getMessage().getPayload());
76             return new MuleMessage(getOriginalPayload(), invocation.getMessage());
77         }
78         return null;
79     }
80
81     public String JavaDoc getBeforeExpressions()
82     {
83         return beforeExpressions;
84     }
85
86     public void setBeforeExpressions(String JavaDoc beforeExpressions)
87     {
88         this.beforeExpressions = beforeExpressions;
89         String JavaDoc[] exp = StringUtils.splitAndTrim(beforeExpressions, ",");
90         this.beforeExpressionsList = new ArrayList JavaDoc(exp.length);
91         for (int i = 0; i < exp.length; i++)
92         {
93             this.beforeExpressionsList.add(exp[i]);
94
95         }
96     }
97
98     public String JavaDoc getAfterExpression()
99     {
100         return afterExpression;
101     }
102
103     public void setAfterExpression(String JavaDoc afterExpression)
104     {
105         this.afterExpression = afterExpression;
106     }
107 }
108
Popular Tags