Coverage Report - org.mule.util.expression.FunctionExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionExpressionEvaluator
59%
17/29
72%
13/18
9
 
 1  
 /*
 2  
  * $Id: FunctionExpressionEvaluator.java 11246 2008-03-07 12:27:48Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.util.expression;
 11  
 
 12  
 import org.mule.api.MuleRuntimeException;
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.util.DateUtils;
 15  
 import org.mule.util.UUID;
 16  
 
 17  
 import java.net.InetAddress;
 18  
 import java.net.UnknownHostException;
 19  
 import java.sql.Timestamp;
 20  
 import java.util.Date;
 21  
 
 22  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
 23  
 
 24  
 /**
 25  
  * This property extractor doesn't actually extract a property from the message, instead it allows for certain functions
 26  
  * to be called and returns a result. The functions it supports are -
 27  
  * <ul>
 28  
  * <li>now - returns an {@link java.sql.Timestamp} with the current time.</li>
 29  
  * <li>date - returns a {@link java.util.Date} with the current time.</li>
 30  
  * <li>dateStamp - returns a {@link java.lang.String} that contains the current date formatted according to {@link #DEFAULT_DATE_FORMAT}.</li>
 31  
  * <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>
 32  
  * <li>uuid - returns a globally unique identifier</li>
 33  
  * <li>hostname - returns the hostname of the machine Mule is running on</li>
 34  
  * <li>ip - returns the ip address of the machine Mule is running on</li>
 35  
  * <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>
 36  
  * </ul>
 37  
  */
 38  1148
 public class FunctionExpressionEvaluator implements ExpressionEvaluator
 39  
 {
 40  
     public static final String NAME = "function";
 41  
 
 42  
     public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
 43  
 
 44  
     /**
 45  
      * A local counter that will increment for each call.  If the server is re-started the
 46  
      * counter will return to zero
 47  
      */
 48  1148
     private final AtomicLong count = new AtomicLong(0);
 49  
 
 50  
     public static final String NOW_FUNCTION = "now";
 51  
     public static final String DATE_FUNCTION = "date";
 52  
     public static final String DATESTAMP_FUNCTION = "datestamp";
 53  
     public static final String SYSTIME_FUNCTION = "systime";
 54  
     public static final String UUID_FUNCTION = "uuid";
 55  
     public static final String HOSTNAME_FUNCTION = "hostname";
 56  
     public static final String IP_FUNCTION = "ip";
 57  
     public static final String COUNT_FUNCTION = "count";
 58  
 
 59  
     public Object evaluate(String name, Object message)
 60  
     {
 61  28
         if (name.equalsIgnoreCase(NOW_FUNCTION))
 62  
         {
 63  6
             return new Timestamp(System.currentTimeMillis());
 64  
         }
 65  22
         else if (name.equalsIgnoreCase(DATE_FUNCTION))
 66  
         {
 67  4
             return new Date(System.currentTimeMillis());
 68  
         }
 69  18
         else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION))
 70  
         {
 71  0
             String temp = name.substring(DATESTAMP_FUNCTION.length());
 72  0
             if (temp.length()==0)
 73  
             {
 74  0
                 return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
 75  
             }
 76  
             else
 77  
             {
 78  0
                 temp = temp.substring(1);
 79  0
                 return DateUtils.getTimeStamp(temp);
 80  
             }
 81  
         }
 82  18
         else if (name.equalsIgnoreCase(UUID_FUNCTION))
 83  
         {
 84  6
             return UUID.getUUID();
 85  
         }
 86  12
         else if (name.equalsIgnoreCase(SYSTIME_FUNCTION))
 87  
         {
 88  0
             return new Long(System.currentTimeMillis());
 89  
         }
 90  12
         else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION))
 91  
         {
 92  
             try
 93  
             {
 94  4
                 return InetAddress.getLocalHost().getHostName();
 95  
             }
 96  0
             catch (UnknownHostException e)
 97  
             {
 98  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 99  
             }
 100  
         }
 101  8
         else if (name.equalsIgnoreCase(IP_FUNCTION))
 102  
         {
 103  
             try
 104  
             {
 105  4
                 return InetAddress.getLocalHost().getHostAddress();
 106  
             }
 107  0
             catch (UnknownHostException e)
 108  
             {
 109  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 110  
             }
 111  
         }
 112  4
         else if (name.equalsIgnoreCase(COUNT_FUNCTION))
 113  
         {
 114  0
             return new Long(count.getAndIncrement());
 115  
         }
 116  
         else
 117  
         {
 118  4
             throw new IllegalArgumentException(name);
 119  
         }
 120  
     }
 121  
 
 122  
     /**
 123  
      * Gts the name of the object
 124  
      *
 125  
      * @return the name of the object
 126  
      */
 127  
     public String getName()
 128  
     {
 129  1146
         return NAME;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Sets the name of the object
 134  
      *
 135  
      * @param name the name of the object
 136  
      */
 137  
     public void setName(String name)
 138  
     {
 139  0
         throw new UnsupportedOperationException("setName");
 140  
     }
 141  
 }