KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/Property2.java,v 1.4 2004/03/30 18:07:07 sebb Exp $
2
/*
3  * Copyright 2003-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.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.util.JMeterUtils;
30
31 /**
32  * Function to get a JMeter property, or a default.
33  * Does not offer the option to store the value, as it is just as easy
34  * to refetch it.
35  * This is a specialisation of the __property() function to make it
36  * simpler to use for ThreadGroup GUI etc. The name is also shorter.
37  *
38  * Parameters:
39  * - property name
40  * - default value (optional; defaults to "1")
41  *
42  * Usage:
43  *
44  * Define the property in jmeter.properties, or on the command-line:
45  * java ... -Jpropname=value
46  *
47  * Retrieve the value in the appropriate GUI by using the string:
48  * ${__P(propname)}
49  * $(__P(propname,default)}
50  *
51  * Returns:
52  * - the property value, but if not found
53  * - the default value, but if not present
54  * - "1" (suitable for use in ThreadGroup GUI)
55  *
56  * @version $Revision: 1.4 $ Updated: $Date: 2004/03/30 18:07:07 $
57  */

58 public class Property2 extends AbstractFunction implements Serializable JavaDoc
59 {
60
61     private static final List JavaDoc desc = new LinkedList JavaDoc();
62     private static final String JavaDoc KEY = "__P";
63
64     // Number of parameters expected - used to reject invalid calls
65
private static final int MIN_PARAMETER_COUNT = 1;
66     private static final int MAX_PARAMETER_COUNT = 2;
67     static {
68         desc.add(JMeterUtils.getResString("property_name_param"));
69         desc.add(JMeterUtils.getResString("property_default_param"));
70     }
71
72     private Object JavaDoc[] values;
73
74     public Property2()
75     {
76     }
77
78     public Object JavaDoc clone()
79     {
80         return new Property2();
81     }
82
83     public synchronized String JavaDoc execute(
84         SampleResult previousResult,
85         Sampler currentSampler)
86         throws InvalidVariableException
87     {
88         String JavaDoc propertyName = ((CompoundVariable) values[0]).execute();
89         
90         String JavaDoc propertyDefault = "1"; //$NON-NLS-1$
91
if (values.length > 1){ // We have a default
92
propertyDefault= ((CompoundVariable) values[1]).execute();
93         }
94         
95         String JavaDoc propertyValue =
96             JMeterUtils.getPropDefault(propertyName, propertyDefault);
97         
98         return propertyValue;
99
100     }
101
102     public void setParameters(Collection JavaDoc parameters)
103         throws InvalidVariableException
104     {
105
106         values = parameters.toArray();
107
108         if ((values.length < MIN_PARAMETER_COUNT)
109             || (values.length > MAX_PARAMETER_COUNT))
110         {
111             throw new InvalidVariableException(
112                 "Parameter Count not between "
113                     + MIN_PARAMETER_COUNT
114                     + " & "
115                     + MAX_PARAMETER_COUNT);
116         }
117
118     }
119
120     public String JavaDoc getReferenceKey()
121     {
122         return KEY;
123     }
124
125     public List JavaDoc getArgumentDesc()
126     {
127         return desc;
128     }
129
130 }
Popular Tags