View Javadoc

1   /*
2    * $Id: FunctionExpressionEvaluator.java 21939 2011-05-18 13:32:09Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * This property extractor doesn't actually extract a property from the message, instead it allows for certain functions
28   * to be called and returns a result. The functions it supports are -
29   * <ul>
30   * <li>now - returns an {@link java.sql.Timestamp} with the current time.</li>
31   * <li>date - returns a {@link java.util.Date} with the current time.</li>
32   * <li>dateStamp - returns a {@link java.lang.String} that contains the current date formatted according to {@link #DEFAULT_DATE_FORMAT}.</li>
33   * <li>datestamp:dd-MM-yyyy - returns a {@link java.lang.String} that contains the current date formatted according to the format passed into the function.</li>
34   * <li>uuid - returns a globally unique identifier</li>
35   * <li>hostname - returns the hostname of the machine Mule is running on</li>
36   * <li>ip - returns the ip address of the machine Mule is running on</li>
37   * <li>count - returns a local count that will increment for each call.  If the server is re-started the counter will return to zero</li>
38   * <li>payloadClass - Returns a fuly qualified class name of the payload as a string</li>
39   * <li>shortPayloadClass - Returns just the class name of the payload as a string</li>
40   * </ul>
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       * A local counter that will increment for each call.  If the server is re-started the
50       * counter will return to zero
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      * Gts the name of the object
138      *
139      * @return the name of the object
140      */
141     public String getName()
142     {
143         return NAME;
144     }
145 
146     /**
147      * Sets the name of the object
148      *
149      * @param name the name of the object
150      */
151     public void setName(String name)
152     {
153         throw new UnsupportedOperationException();
154     }
155 }