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  
  * $Id: FunctionExpressionEvaluator.java 20321 2010-11-24 15:21:24Z dfeist $
 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  
 
 25  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
 26  
 
 27  
 /**
 28  
  * This property extractor doesn't actually extract a property from the message, instead it allows for certain functions
 29  
  * to be called and returns a result. The functions it supports are -
 30  
  * <ul>
 31  
  * <li>now - returns an {@link java.sql.Timestamp} with the current time.</li>
 32  
  * <li>date - returns a {@link java.util.Date} with the current time.</li>
 33  
  * <li>dateStamp - returns a {@link java.lang.String} that contains the current date formatted according to {@link #DEFAULT_DATE_FORMAT}.</li>
 34  
  * <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>
 35  
  * <li>uuid - returns a globally unique identifier</li>
 36  
  * <li>hostname - returns the hostname of the machine Mule is running on</li>
 37  
  * <li>ip - returns the ip address of the machine Mule is running on</li>
 38  
  * <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>
 39  
  * <li>payloadClass - Returns a fuly qualified class name of the payload as a string</li>
 40  
  * <li>shortPayloadClass - Returns just the class name of the payload as a string</li>
 41  
  * </ul>
 42  
  */
 43  0
 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  
      * A local counter that will increment for each call.  If the server is re-started the
 51  
      * counter will return to zero
 52  
      */
 53  0
     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  0
         if (name.equalsIgnoreCase(NOW_FUNCTION))
 69  
         {
 70  0
             return new Timestamp(System.currentTimeMillis());
 71  
         }
 72  0
         else if (name.equalsIgnoreCase(DATE_FUNCTION))
 73  
         {
 74  0
             return new Date(System.currentTimeMillis());
 75  
         }
 76  0
         else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION))
 77  
         {
 78  0
             String temp = name.substring(DATESTAMP_FUNCTION.length());
 79  0
             if (temp.length() == 0)
 80  
             {
 81  0
                 return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
 82  
             }
 83  
             else
 84  
             {
 85  0
                 temp = temp.substring(1);
 86  0
                 return DateUtils.getTimeStamp(temp);
 87  
             }
 88  
         }
 89  0
         else if (name.equalsIgnoreCase(UUID_FUNCTION))
 90  
         {
 91  0
             return UUID.getUUID();
 92  
         }
 93  0
         else if (name.equalsIgnoreCase(SYSTIME_FUNCTION))
 94  
         {
 95  0
             return System.currentTimeMillis();
 96  
         }
 97  0
         else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION))
 98  
         {
 99  
             try
 100  
             {
 101  0
                 return InetAddress.getLocalHost().getHostName();
 102  
             }
 103  0
             catch (UnknownHostException e)
 104  
             {
 105  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 106  
             }
 107  
         }
 108  0
         else if (name.equalsIgnoreCase(IP_FUNCTION))
 109  
         {
 110  
             try
 111  
             {
 112  0
                 return InetAddress.getLocalHost().getHostAddress();
 113  
             }
 114  0
             catch (UnknownHostException e)
 115  
             {
 116  0
                 throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(name), e);
 117  
             }
 118  
         }
 119  0
         else if (name.equalsIgnoreCase(COUNT_FUNCTION))
 120  
         {
 121  0
             return count.getAndIncrement();
 122  
         }
 123  0
         else if (name.equalsIgnoreCase(PAYLOAD_CLASS_FUNCTION))
 124  
         {
 125  0
             return message.getPayload().getClass().getName();
 126  
         }
 127  0
         else if (name.equalsIgnoreCase(SHORT_PAYLOAD_CLASS_FUNCTION))
 128  
         {
 129  0
             return ClassUtils.getClassName(message.getPayload().getClass());
 130  
         }
 131  
         else
 132  
         {
 133  0
             throw new IllegalArgumentException(name);
 134  
         }
 135  
     }    
 136  
 
 137  
     /**
 138  
      * Gts the name of the object
 139  
      *
 140  
      * @return the name of the object
 141  
      */
 142  
     public String getName()
 143  
     {
 144  0
         return NAME;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Sets the name of the object
 149  
      *
 150  
      * @param name the name of the object
 151  
      */
 152  
     public void setName(String name)
 153  
     {
 154  0
         throw new UnsupportedOperationException();
 155  
     }
 156  
 }