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 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 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 if (name.equalsIgnoreCase(NOW_FUNCTION))
69 {
70 return new Timestamp(System.currentTimeMillis());
71 }
72 else if (name.equalsIgnoreCase(DATE_FUNCTION))
73 {
74 return new Date(System.currentTimeMillis());
75 }
76 else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION))
77 {
78 String temp = name.substring(DATESTAMP_FUNCTION.length());
79 if (temp.length() == 0)
80 {
81 return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
82 }
83 else
84 {
85 temp = temp.substring(1);
86 return DateUtils.getTimeStamp(temp);
87 }
88 }
89 else if (name.equalsIgnoreCase(UUID_FUNCTION))
90 {
91 return UUID.getUUID();
92 }
93 else if (name.equalsIgnoreCase(SYSTIME_FUNCTION))
94 {
95 return System.currentTimeMillis();
96 }
97 else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION))
98 {
99 try
100 {
101 return InetAddress.getLocalHost().getHostName();
102 }
103 catch (UnknownHostException e)
104 {
105 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
106 }
107 }
108 else if (name.equalsIgnoreCase(IP_FUNCTION))
109 {
110 try
111 {
112 return InetAddress.getLocalHost().getHostAddress();
113 }
114 catch (UnknownHostException e)
115 {
116 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
117 }
118 }
119 else if (name.equalsIgnoreCase(COUNT_FUNCTION))
120 {
121 return count.getAndIncrement();
122 }
123 else if (name.equalsIgnoreCase(PAYLOAD_CLASS_FUNCTION))
124 {
125 return message.getPayload().getClass().getName();
126 }
127 else if (name.equalsIgnoreCase(SHORT_PAYLOAD_CLASS_FUNCTION))
128 {
129 return ClassUtils.getClassName(message.getPayload().getClass());
130 }
131 else
132 {
133 throw new IllegalArgumentException(name);
134 }
135 }
136
137
138
139
140
141
142 public String getName()
143 {
144 return NAME;
145 }
146
147
148
149
150
151
152 public void setName(String name)
153 {
154 throw new UnsupportedOperationException();
155 }
156 }