KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > MachineName


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/MachineName.java,v 1.10 2004/02/10 00:35:12 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
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 JavaDoc
35 {
36
37     private static final List JavaDoc desc = new LinkedList JavaDoc();
38     private static final String JavaDoc KEY = "__machineName";
39
40     // Number of parameters expected - used to reject invalid calls
41
private static final int PARAMETER_COUNT = 1;
42     static {
43         //desc.add("Use fully qualified host name: TRUE/FALSE (Default FALSE)");
44
desc.add(JMeterUtils.getResString("function_name_param"));
45     }
46
47     private Object JavaDoc[] values;
48
49     public MachineName()
50     {
51     }
52
53     public Object JavaDoc clone()
54     {
55         return new MachineName();
56     }
57
58     public synchronized String JavaDoc execute(
59         SampleResult previousResult,
60         Sampler currentSampler)
61         throws InvalidVariableException
62     {
63
64         JMeterVariables vars = getVariables();
65
66         /*
67         boolean fullHostName = false;
68         if (((CompoundFunction) values[0])
69             .execute()
70             .toLowerCase()
71             .equals("true"))
72         {
73             fullHostName = true;
74         }
75         */

76         
77         String JavaDoc varName =
78             ((CompoundVariable) values[values.length - 1]).execute();
79         String JavaDoc machineName = "";
80
81         try
82         {
83
84             InetAddress JavaDoc Address = InetAddress.getLocalHost();
85
86             //fullHostName disabled until we move up to 1.4 as the support jre
87
// if ( fullHostName ) {
88
// machineName = Address.getCanonicalHostName();
89

90             // } else {
91
machineName = Address.getHostName();
92             // }
93

94         }
95         catch (UnknownHostException JavaDoc e)
96         {
97         }
98
99         vars.put(varName, machineName);
100         return machineName;
101
102     }
103
104     public void setParameters(Collection JavaDoc 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 JavaDoc getReferenceKey()
119     {
120         return KEY;
121     }
122
123     public List JavaDoc getArgumentDesc()
124     {
125         return desc;
126     }
127 }
128
Popular Tags