KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/Property.java,v 1.6 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, and optionally store it
33  *
34  * Parameters:
35  * - property name
36  * - variable name (optional)
37  * - default value (optional)
38  *
39  *
40  * Returns:
41  * - the property value, but if not found
42  * - the default value, but if not define
43  * - the property name itself
44  *
45  * @version $Revision: 1.6 $ Updated: $Date: 2004/03/30 18:07:07 $
46  */

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