KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/SplitFunction.java,v 1.1.2.1 2004/09/22 22:07:17 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.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.threads.JMeterVariables;
30 import org.apache.jmeter.util.JMeterUtils;
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33
34 /**
35  * Function to log a message
36  *
37  * Parameters:
38  * - string
39  * - log level (optional; defaults to INFO; or DEBUG if unrecognised)
40  * - throwable message (optional)
41  *
42  * Returns:
43  * - the input string
44  *
45  * @version $Revision: 1.1.2.1 $ Updated: $Date: 2004/09/22 22:07:17 $
46  */

47 public class SplitFunction extends AbstractFunction implements Serializable JavaDoc
48 {
49     private static Logger log = LoggingManager.getLoggerForClass();
50
51     private static final List JavaDoc desc = new LinkedList JavaDoc();
52     private static final String JavaDoc KEY = "__split";
53
54     // Number of parameters expected - used to reject invalid calls
55
private static final int MIN_PARAMETER_COUNT = 2;
56     private static final int MAX_PARAMETER_COUNT = 3;
57     static {
58         desc.add("String to split");
59         desc.add("Variable name");
60         desc.add("Split character (omit for ',')");
61     }
62
63     private Object JavaDoc[] values;
64
65     public SplitFunction()
66     {
67     }
68
69     public Object JavaDoc clone()
70     {
71         return new SplitFunction();
72     }
73
74     public synchronized String JavaDoc execute(
75         SampleResult previousResult,
76         Sampler currentSampler)
77         throws InvalidVariableException
78     {
79         JMeterVariables vars = getVariables();
80         
81         String JavaDoc stringToSplit = ((CompoundVariable) values[0]).execute();
82         String JavaDoc varNamePrefix = ((CompoundVariable) values[1]).execute();
83         String JavaDoc splitString = ",";
84         
85         if (values.length > 2){ // Split string provided
86
splitString = ((CompoundVariable) values[2]).execute();
87         }
88         String JavaDoc parts[] = JMeterUtils.split(stringToSplit,splitString,"?");
89         
90         vars.put(varNamePrefix, stringToSplit);
91         vars.put(varNamePrefix+"_n", ""+parts.length);
92         for (int i = 1; i <= parts.length ;i++){
93             vars.put(varNamePrefix+"_"+i,parts[i-1]);
94         }
95         return stringToSplit;
96
97     }
98     
99     public void setParameters(Collection JavaDoc parameters)
100         throws InvalidVariableException
101     {
102
103         values = parameters.toArray();
104
105         if ((values.length < MIN_PARAMETER_COUNT)
106             || (values.length > MAX_PARAMETER_COUNT))
107         {
108             throw new InvalidVariableException(
109                 "Parameter Count not between "
110                     + MIN_PARAMETER_COUNT
111                     + " & "
112                     + MAX_PARAMETER_COUNT);
113         }
114
115     }
116
117     public String JavaDoc getReferenceKey()
118     {
119         return KEY;
120     }
121
122     public List JavaDoc getArgumentDesc()
123     {
124         return desc;
125     }
126
127 }
Popular Tags