1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.Collection ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import org.apache.jmeter.engine.util.CompoundVariable; 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jmeter.samplers.Sampler; 31 import org.apache.jmeter.threads.JMeterVariables; 32 import org.apache.jmeter.util.JMeterUtils; 33 34 public class MachineName extends AbstractFunction implements Serializable 35 { 36 37 private static final List desc = new LinkedList (); 38 private static final String KEY = "__machineName"; 39 40 private static final int PARAMETER_COUNT = 1; 42 static { 43 desc.add(JMeterUtils.getResString("function_name_param")); 45 } 46 47 private Object [] values; 48 49 public MachineName() 50 { 51 } 52 53 public Object clone() 54 { 55 return new MachineName(); 56 } 57 58 public synchronized String execute( 59 SampleResult previousResult, 60 Sampler currentSampler) 61 throws InvalidVariableException 62 { 63 64 JMeterVariables vars = getVariables(); 65 66 76 77 String varName = 78 ((CompoundVariable) values[values.length - 1]).execute(); 79 String machineName = ""; 80 81 try 82 { 83 84 InetAddress Address = InetAddress.getLocalHost(); 85 86 90 machineName = Address.getHostName(); 92 94 } 95 catch (UnknownHostException e) 96 { 97 } 98 99 vars.put(varName, machineName); 100 return machineName; 101 102 } 103 104 public void setParameters(Collection parameters) 105 throws InvalidVariableException 106 { 107 108 values = parameters.toArray(); 109 110 if (values.length != PARAMETER_COUNT) 111 { 112 throw new InvalidVariableException( 113 "Parameter Count != " + PARAMETER_COUNT); 114 } 115 116 } 117 118 public String getReferenceKey() 119 { 120 return KEY; 121 } 122 123 public List getArgumentDesc() 124 { 125 return desc; 126 } 127 } 128 | Popular Tags |