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