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.net.InetAddress;
023: import java.net.UnknownHostException;
024: import java.util.Collection;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import org.apache.jmeter.engine.util.CompoundVariable;
029: import org.apache.jmeter.samplers.SampleResult;
030: import org.apache.jmeter.samplers.Sampler;
031: import org.apache.jmeter.threads.JMeterVariables;
032: import org.apache.jmeter.util.JMeterUtils;
033:
034: public class MachineName extends AbstractFunction implements
035: Serializable {
036:
037: private static final List desc = new LinkedList();
038:
039: private static final String KEY = "__machineName"; //$NON-NLS-1$
040:
041: // Number of parameters expected - used to reject invalid calls
042: private static final int PARAMETER_COUNT = 1;
043: static {
044: // desc.add("Use fully qualified host name: TRUE/FALSE (Default FALSE)");
045: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
046: }
047:
048: private Object[] values;
049:
050: public MachineName() {
051: }
052:
053: public Object clone() throws CloneNotSupportedException {
054: return super .clone();
055: }
056:
057: public synchronized String execute(SampleResult previousResult,
058: Sampler currentSampler) throws InvalidVariableException {
059:
060: JMeterVariables vars = getVariables();
061:
062: /*
063: * boolean fullHostName = false; if (((CompoundFunction) values[0])
064: * .execute() .toLowerCase() .equals("true")) { fullHostName = true; }
065: */
066:
067: String varName = ((CompoundVariable) values[values.length - 1])
068: .execute();
069: String machineName = "";
070:
071: try {
072:
073: InetAddress Address = InetAddress.getLocalHost();
074:
075: // fullHostName disabled until we move up to 1.4 as the support jre
076: // if ( fullHostName ) {
077: // machineName = Address.getCanonicalHostName();
078:
079: // } else {
080: machineName = Address.getHostName();
081: // }
082:
083: } catch (UnknownHostException e) {
084: }
085:
086: vars.put(varName, machineName);
087: return machineName;
088:
089: }
090:
091: public synchronized void setParameters(Collection parameters)
092: throws InvalidVariableException {
093:
094: values = parameters.toArray();
095:
096: if (values.length != PARAMETER_COUNT) {
097: throw new InvalidVariableException("Parameter Count != "
098: + PARAMETER_COUNT);
099: }
100:
101: }
102:
103: public String getReferenceKey() {
104: return KEY;
105: }
106:
107: public List getArgumentDesc() {
108: return desc;
109: }
110: }
|