001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.functions;
020:
021: import java.io.Serializable;
022: import java.text.SimpleDateFormat;
023: import java.util.Collection;
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.jmeter.engine.util.CompoundVariable;
031: import org.apache.jmeter.samplers.SampleResult;
032: import org.apache.jmeter.samplers.Sampler;
033: import org.apache.jmeter.threads.JMeterVariables;
034: import org.apache.jmeter.util.JMeterUtils;
035:
036: // See org.apache.jmeter.functions.TestTimeFunction for unit tests
037:
038: /**
039: * __time() function - returns the current time in milliseconds
040: */
041: public class TimeFunction extends AbstractFunction implements
042: Serializable {
043:
044: private static final long serialVersionUID = 1L;
045:
046: private static final String KEY = "__time"; // $NON-NLS-1$
047:
048: private static final List desc = new LinkedList();
049:
050: private static final Map aliases = new HashMap();
051:
052: static {
053: desc.add(JMeterUtils.getResString("time_format")); //$NON-NLS-1$
054: desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
055: aliases.put("YMD", //$NON-NLS-1$
056: JMeterUtils.getPropDefault("time.YMD", //$NON-NLS-1$
057: "yyyyMMdd")); //$NON-NLS-1$
058: aliases.put("HMS", //$NON-NLS-1$
059: JMeterUtils.getPropDefault("time.HMS", //$NON-NLS-1$
060: "HHmmss")); //$NON-NLS-1$
061: aliases.put("YMDHMS", //$NON-NLS-1$
062: JMeterUtils.getPropDefault("time.YMDHMS", //$NON-NLS-1$
063: "yyyyMMdd-HHmmss")); //$NON-NLS-1$
064: aliases.put("USER1", //$NON-NLS-1$
065: JMeterUtils.getPropDefault("time.USER1", "")); //$NON-NLS-1$
066: aliases.put("USER2", //$NON-NLS-1$
067: JMeterUtils.getPropDefault("time.USER2", "")); //$NON-NLS-1$
068: }
069:
070: // Ensure that these are set, even if no paramters are provided
071: transient private String format = ""; //$NON-NLS-1$
072: transient private String variable = ""; //$NON-NLS-1$
073:
074: private Object readResolve() {
075: format = "";
076: variable = "";
077: return this ;
078: }
079:
080: public TimeFunction() {
081: super ();
082: }
083:
084: public Object clone() throws CloneNotSupportedException {
085: return super .clone();
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
092: */
093: public synchronized String execute(SampleResult previousResult,
094: Sampler currentSampler) throws InvalidVariableException {
095: String datetime;
096: if (format.length() == 0) {// Default to milliseconds
097: datetime = Long.toString(System.currentTimeMillis());
098: } else {
099: // Resolve any aliases
100: String fmt = (String) aliases.get(format);
101: if (fmt == null)
102: fmt = format;// Not found
103: SimpleDateFormat df = new SimpleDateFormat(fmt);// Not synchronised, so can't be shared
104: datetime = df.format(new Date());
105: }
106:
107: if (variable.length() > 0) {
108: JMeterVariables vars = getVariables();
109: vars.put(variable, datetime);
110: }
111: return datetime;
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * It appears that this is not called if no parameters are provided.
118: *
119: * @see org.apache.jmeter.functions.Function#setParameters(Collection)
120: */
121: public synchronized void setParameters(Collection parameters)
122: throws InvalidVariableException {
123:
124: checkParameterCount(parameters, 0, 2);
125:
126: Object[] values = parameters.toArray();
127: int count = values.length;
128:
129: if (count > 0) {
130: format = ((CompoundVariable) values[0]).execute();
131: }
132:
133: if (count > 1) {
134: variable = ((CompoundVariable) values[1]).execute();
135: }
136:
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see org.apache.jmeter.functions.Function#getReferenceKey()
143: */
144: public String getReferenceKey() {
145: return KEY;
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see org.apache.jmeter.functions.Function#getArgumentDesc()
152: */
153: public List getArgumentDesc() {
154: return desc;
155: }
156: }
|