1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.cache.keygenerator; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
|
11 | |
import java.io.NotSerializableException; |
12 | |
import java.io.Serializable; |
13 | |
|
14 | |
import org.apache.commons.logging.Log; |
15 | |
import org.apache.commons.logging.LogFactory; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | 0 | public class ExpressionKeyGenerator implements KeyGenerator |
22 | |
{ |
23 | |
|
24 | 0 | protected Log logger = LogFactory.getLog(getClass()); |
25 | |
|
26 | |
private String expression; |
27 | |
|
28 | |
public Serializable generateKey(MuleEvent event) throws NotSerializableException |
29 | |
{ |
30 | 0 | Object key = event.getMuleContext().getExpressionManager().evaluate(expression, event.getMessage()); |
31 | |
|
32 | 0 | if (logger.isDebugEnabled()) |
33 | |
{ |
34 | 0 | logger.debug("Generated key for event: " + event + " key: " + key); |
35 | |
} |
36 | |
|
37 | 0 | if (key instanceof Serializable) |
38 | |
{ |
39 | 0 | return (Serializable) key; |
40 | |
} |
41 | |
else |
42 | |
{ |
43 | 0 | throw new NotSerializableException("Generated key must a serializable object but was " |
44 | |
+ (key != null ? key.getClass().getName() : "null")); |
45 | |
} |
46 | |
} |
47 | |
|
48 | |
public String getExpression() |
49 | |
{ |
50 | 0 | return expression; |
51 | |
} |
52 | |
|
53 | |
public void setExpression(String expression) |
54 | |
{ |
55 | 0 | this.expression = expression; |
56 | 0 | } |
57 | |
} |