001: /*
002: * $Id: FunctionExpressionEvaluator.java 11246 2008-03-07 12:27:48Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.util.expression;
011:
012: import org.mule.api.MuleRuntimeException;
013: import org.mule.config.i18n.CoreMessages;
014: import org.mule.util.DateUtils;
015: import org.mule.util.UUID;
016:
017: import java.net.InetAddress;
018: import java.net.UnknownHostException;
019: import java.sql.Timestamp;
020: import java.util.Date;
021:
022: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
023:
024: /**
025: * This property extractor doesn't actually extract a property from the message, instead it allows for certain functions
026: * to be called and returns a result. The functions it supports are -
027: * <ul>
028: * <li>now - returns an {@link java.sql.Timestamp} with the current time.</li>
029: * <li>date - returns a {@link java.util.Date} with the current time.</li>
030: * <li>dateStamp - returns a {@link java.lang.String} that contains the current date formatted according to {@link #DEFAULT_DATE_FORMAT}.</li>
031: * <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>
032: * <li>uuid - returns a globally unique identifier</li>
033: * <li>hostname - returns the hostname of the machine Mule is running on</li>
034: * <li>ip - returns the ip address of the machine Mule is running on</li>
035: * <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>
036: * </ul>
037: */
038: public class FunctionExpressionEvaluator implements ExpressionEvaluator {
039: public static final String NAME = "function";
040:
041: public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
042:
043: /**
044: * A local counter that will increment for each call. If the server is re-started the
045: * counter will return to zero
046: */
047: private final AtomicLong count = new AtomicLong(0);
048:
049: public static final String NOW_FUNCTION = "now";
050: public static final String DATE_FUNCTION = "date";
051: public static final String DATESTAMP_FUNCTION = "datestamp";
052: public static final String SYSTIME_FUNCTION = "systime";
053: public static final String UUID_FUNCTION = "uuid";
054: public static final String HOSTNAME_FUNCTION = "hostname";
055: public static final String IP_FUNCTION = "ip";
056: public static final String COUNT_FUNCTION = "count";
057:
058: public Object evaluate(String name, Object message) {
059: if (name.equalsIgnoreCase(NOW_FUNCTION)) {
060: return new Timestamp(System.currentTimeMillis());
061: } else if (name.equalsIgnoreCase(DATE_FUNCTION)) {
062: return new Date(System.currentTimeMillis());
063: } else if (name.toLowerCase().startsWith(DATESTAMP_FUNCTION)) {
064: String temp = name.substring(DATESTAMP_FUNCTION.length());
065: if (temp.length() == 0) {
066: return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
067: } else {
068: temp = temp.substring(1);
069: return DateUtils.getTimeStamp(temp);
070: }
071: } else if (name.equalsIgnoreCase(UUID_FUNCTION)) {
072: return UUID.getUUID();
073: } else if (name.equalsIgnoreCase(SYSTIME_FUNCTION)) {
074: return new Long(System.currentTimeMillis());
075: } else if (name.equalsIgnoreCase(HOSTNAME_FUNCTION)) {
076: try {
077: return InetAddress.getLocalHost().getHostName();
078: } catch (UnknownHostException e) {
079: throw new MuleRuntimeException(CoreMessages
080: .failedToProcessExtractorFunction(name), e);
081: }
082: } else if (name.equalsIgnoreCase(IP_FUNCTION)) {
083: try {
084: return InetAddress.getLocalHost().getHostAddress();
085: } catch (UnknownHostException e) {
086: throw new MuleRuntimeException(CoreMessages
087: .failedToProcessExtractorFunction(name), e);
088: }
089: } else if (name.equalsIgnoreCase(COUNT_FUNCTION)) {
090: return new Long(count.getAndIncrement());
091: } else {
092: throw new IllegalArgumentException(name);
093: }
094: }
095:
096: /**
097: * Gts the name of the object
098: *
099: * @return the name of the object
100: */
101: public String getName() {
102: return NAME;
103: }
104:
105: /**
106: * Sets the name of the object
107: *
108: * @param name the name of the object
109: */
110: public void setName(String name) {
111: throw new UnsupportedOperationException("setName");
112: }
113: }
|