Coverage Report - org.mule.expression.FunctionExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionExpressionEvaluator
0%
0/33
0%
0/22
10.333
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.expression;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.MuleRuntimeException;
 11  
 import org.mule.api.expression.ExpressionEvaluator;
 12  
 import org.mule.config.i18n.CoreMessages;
 13  
 import org.mule.util.ClassUtils;
 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  
  * <li>payloadClass - Returns a fuly qualified class name of the payload as a string</li>
 37  
  * <li>shortPayloadClass - Returns just the class name of the payload as a string</li>
 38  
  * </ul>
 39  
  */
 40  0
 public class FunctionExpressionEvaluator implements ExpressionEvaluator
 41  
 {
 42  
     public static final String NAME = "function";
 43  
 
 44  
     public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
 45  
 
 46  
     /**
 47  
      * A local counter that will increment for each call.  If the server is re-started the
 48  
      * counter will return to zero
 49  
      */
 50  0
     private final AtomicLong count = new AtomicLong(0);
 51  
 
 52  
     public static final String NOW_FUNCTION = "now";
 53  
     public static final String DATE_FUNCTION = "date";
 54  
     public static final String DATESTAMP_FUNCTION = "datestamp";
 55  
     public static final String SYSTIME_FUNCTION = "systime";
 56  
     public static final String UUID_FUNCTION = "uuid";
 57  
     public static final String HOSTNAME_FUNCTION = "hostname";
 58  
     public static final String IP_FUNCTION = "ip";
 59  
     public static final String COUNT_FUNCTION = "count";
 60  
     public static final String PAYLOAD_CLASS_FUNCTION = "payloadClass";
 61  
     public static final String SHORT_PAYLOAD_CLASS_FUNCTION = "shortPayloadClass";
 62  
 
 63  
     public Object evaluate(String name, MuleMessage message)
 64  
     {
 65  0
         if (name.equalsIgnoreCase(NOW_FUNCTION))
 66  
         {
 67  0
             return new Timestamp(System.currentTimeMillis());
 68  
         }
 69  0
         else if (name.equalsIgnoreCase(DATE_FUNCTION))
 70  
         {
 71  0
             return new Date(System.currentTimeMillis());
 72  
         }
 73  0
         else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION))
 74  
         {
 75  0
             String temp = name.substring(DATESTAMP_FUNCTION.length());
 76  0
             if (temp.length() == 0)
 77  
             {
 78  0
                 return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
 79  
             }
 80  
             else
 81  
             {
 82  0
                 temp = temp.substring(1);
 83  0
                 return DateUtils.getTimeStamp(temp);
 84  
             }
 85  
         }
 86  0
         else if (name.equalsIgnoreCase(UUID_FUNCTION))
 87  
         {
 88  0
             return UUID.getUUID();
 89  
         }
 90  0
         else if (name.equalsIgnoreCase(SYSTIME_FUNCTION))
 91  
         {
 92  0
             return System.currentTimeMillis();
 93  
         }
 94  0
         else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION))
 95  
         {
 96  
             try
 97  
             {
 98  0
                 return InetAddress.getLocalHost().getHostName();
 99  
             }
 100  0
             catch (UnknownHostException e)
 101  
             {
 102  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 103  
             }
 104  
         }
 105  0
         else if (name.equalsIgnoreCase(IP_FUNCTION))
 106  
         {
 107  
             try
 108  
             {
 109  0
                 return InetAddress.getLocalHost().getHostAddress();
 110  
             }
 111  0
             catch (UnknownHostException e)
 112  
             {
 113  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 114  
             }
 115  
         }
 116  0
         else if (name.equalsIgnoreCase(COUNT_FUNCTION))
 117  
         {
 118  0
             return count.getAndIncrement();
 119  
         }
 120  0
         else if (name.equalsIgnoreCase(PAYLOAD_CLASS_FUNCTION))
 121  
         {
 122  0
             return message.getPayload().getClass().getName();
 123  
         }
 124  0
         else if (name.equalsIgnoreCase(SHORT_PAYLOAD_CLASS_FUNCTION))
 125  
         {
 126  0
             return ClassUtils.getClassName(message.getPayload().getClass());
 127  
         }
 128  
         else
 129  
         {
 130  0
             throw new IllegalArgumentException(name);
 131  
         }
 132  
     }    
 133  
 
 134  
     /**
 135  
      * Gts the name of the object
 136  
      *
 137  
      * @return the name of the object
 138  
      */
 139  
     public String getName()
 140  
     {
 141  0
         return NAME;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Sets the name of the object
 146  
      *
 147  
      * @param name the name of the object
 148  
      */
 149  
     public void setName(String name)
 150  
     {
 151  0
         throw new UnsupportedOperationException();
 152  
     }
 153  
 }