1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.expression; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.MuleRuntimeException; |
14 | |
import org.mule.api.expression.ExpressionEvaluator; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.util.ClassUtils; |
17 | |
import org.mule.util.DateUtils; |
18 | |
import org.mule.util.UUID; |
19 | |
|
20 | |
import java.net.InetAddress; |
21 | |
import java.net.UnknownHostException; |
22 | |
import java.sql.Timestamp; |
23 | |
import java.util.Date; |
24 | |
|
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class FunctionExpressionEvaluator implements ExpressionEvaluator |
44 | |
{ |
45 | |
public static final String NAME = "function"; |
46 | |
|
47 | |
public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS"; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | private final AtomicLong count = new AtomicLong(0); |
54 | |
|
55 | |
public static final String NOW_FUNCTION = "now"; |
56 | |
public static final String DATE_FUNCTION = "date"; |
57 | |
public static final String DATESTAMP_FUNCTION = "datestamp"; |
58 | |
public static final String SYSTIME_FUNCTION = "systime"; |
59 | |
public static final String UUID_FUNCTION = "uuid"; |
60 | |
public static final String HOSTNAME_FUNCTION = "hostname"; |
61 | |
public static final String IP_FUNCTION = "ip"; |
62 | |
public static final String COUNT_FUNCTION = "count"; |
63 | |
public static final String PAYLOAD_CLASS_FUNCTION = "payloadClass"; |
64 | |
public static final String SHORT_PAYLOAD_CLASS_FUNCTION = "shortPayloadClass"; |
65 | |
|
66 | |
public Object evaluate(String name, MuleMessage message) |
67 | |
{ |
68 | 0 | if (name.equalsIgnoreCase(NOW_FUNCTION)) |
69 | |
{ |
70 | 0 | return new Timestamp(System.currentTimeMillis()); |
71 | |
} |
72 | 0 | else if (name.equalsIgnoreCase(DATE_FUNCTION)) |
73 | |
{ |
74 | 0 | return new Date(System.currentTimeMillis()); |
75 | |
} |
76 | 0 | else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION)) |
77 | |
{ |
78 | 0 | String temp = name.substring(DATESTAMP_FUNCTION.length()); |
79 | 0 | if (temp.length() == 0) |
80 | |
{ |
81 | 0 | return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT); |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | temp = temp.substring(1); |
86 | 0 | return DateUtils.getTimeStamp(temp); |
87 | |
} |
88 | |
} |
89 | 0 | else if (name.equalsIgnoreCase(UUID_FUNCTION)) |
90 | |
{ |
91 | 0 | return UUID.getUUID(); |
92 | |
} |
93 | 0 | else if (name.equalsIgnoreCase(SYSTIME_FUNCTION)) |
94 | |
{ |
95 | 0 | return System.currentTimeMillis(); |
96 | |
} |
97 | 0 | else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION)) |
98 | |
{ |
99 | |
try |
100 | |
{ |
101 | 0 | return InetAddress.getLocalHost().getHostName(); |
102 | |
} |
103 | 0 | catch (UnknownHostException e) |
104 | |
{ |
105 | 0 | throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e); |
106 | |
} |
107 | |
} |
108 | 0 | else if (name.equalsIgnoreCase(IP_FUNCTION)) |
109 | |
{ |
110 | |
try |
111 | |
{ |
112 | 0 | return InetAddress.getLocalHost().getHostAddress(); |
113 | |
} |
114 | 0 | catch (UnknownHostException e) |
115 | |
{ |
116 | 0 | throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e); |
117 | |
} |
118 | |
} |
119 | 0 | else if (name.equalsIgnoreCase(COUNT_FUNCTION)) |
120 | |
{ |
121 | 0 | return count.getAndIncrement(); |
122 | |
} |
123 | 0 | else if (name.equalsIgnoreCase(PAYLOAD_CLASS_FUNCTION)) |
124 | |
{ |
125 | 0 | return message.getPayload().getClass().getName(); |
126 | |
} |
127 | 0 | else if (name.equalsIgnoreCase(SHORT_PAYLOAD_CLASS_FUNCTION)) |
128 | |
{ |
129 | 0 | return ClassUtils.getClassName(message.getPayload().getClass()); |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | throw new IllegalArgumentException(name); |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public String getName() |
143 | |
{ |
144 | 0 | return NAME; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void setName(String name) |
153 | |
{ |
154 | 0 | throw new UnsupportedOperationException(); |
155 | |
} |
156 | |
} |