KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > engine > util > CompoundVariable


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/engine/util/CompoundVariable.java,v 1.23 2004/02/13 02:21:38 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.engine.util;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.jmeter.functions.Function;
29 import org.apache.jmeter.functions.InvalidVariableException;
30 import org.apache.jmeter.samplers.SampleResult;
31 import org.apache.jmeter.samplers.Sampler;
32 import org.apache.jmeter.threads.JMeterContext;
33 import org.apache.jmeter.threads.JMeterContextService;
34 import org.apache.jmeter.util.JMeterUtils;
35 import org.apache.jorphan.logging.LoggingManager;
36 import org.apache.jorphan.reflect.ClassFinder;
37 import org.apache.log.Logger;
38
39
40 /**
41  * CompoundFunction.
42  *
43  * @author mstover
44  * @version $Id: CompoundVariable.java,v 1.23 2004/02/13 02:21:38 sebb Exp $
45  */

46 public class CompoundVariable implements Function
47 {
48     transient private static Logger log =
49         LoggingManager.getLoggerForClass();
50         
51     private String JavaDoc rawParameters;
52     
53     static FunctionParser functionParser = new FunctionParser();
54
55     static Map JavaDoc functions = new HashMap JavaDoc();
56     private boolean hasFunction, isDynamic;
57
58     private String JavaDoc permanentResults = "";
59     
60     LinkedList JavaDoc compiledComponents = new LinkedList JavaDoc();
61
62     static {
63         try
64         {
65             List JavaDoc classes =
66                 ClassFinder.findClassesThatExtend(
67                     JMeterUtils.getSearchPaths(),
68                     new Class JavaDoc[] { Function.class },
69                     true);
70             Iterator JavaDoc iter = classes.iterator();
71             while (iter.hasNext())
72             {
73                 Function tempFunc =
74                     (Function) Class
75                         .forName((String JavaDoc) iter.next())
76                         .newInstance();
77                 functions.put(tempFunc.getReferenceKey(), tempFunc.getClass());
78             }
79         }
80         catch (Exception JavaDoc err)
81         {
82             log.error("", err);
83         }
84     }
85
86
87     public CompoundVariable()
88     {
89         super();
90         isDynamic = true;
91         hasFunction = false;
92     }
93     
94     public CompoundVariable(String JavaDoc parameters)
95     {
96         this();
97         try
98         {
99             setParameters(parameters);
100         }
101         catch (InvalidVariableException e)
102         {
103         }
104     }
105
106     public String JavaDoc execute()
107     {
108         if (isDynamic)
109         {
110             JMeterContext context = JMeterContextService.getContext();
111             SampleResult previousResult = context.getPreviousResult();
112             Sampler currentSampler = context.getCurrentSampler();
113             return execute(previousResult, currentSampler);
114         }
115         else
116         {
117             return permanentResults;
118         }
119     }
120     
121     /**
122      * Allows the retrieval of the original String prior to it being compiled.
123      * @return String
124      */

125     public String JavaDoc getRawParameters()
126     {
127         return rawParameters;
128     }
129  
130     /* (non-Javadoc)
131      * @see Function#execute(SampleResult, Sampler)
132      */

133     public String JavaDoc execute(SampleResult previousResult, Sampler currentSampler)
134     {
135         if (compiledComponents == null || compiledComponents.size() == 0)
136         {
137             return "";
138         }
139         boolean testDynamic = false;
140         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
141         Iterator JavaDoc iter = compiledComponents.iterator();
142         while (iter.hasNext())
143         {
144             Object JavaDoc item = iter.next();
145             if (item instanceof Function)
146             {
147                 testDynamic = true;
148                 try
149                 {
150                     results.append(
151                         ((Function) item).execute(
152                             previousResult,
153                             currentSampler));
154                 }
155                 catch (InvalidVariableException e)
156                 {
157                 }
158             }
159             else if (item instanceof SimpleVariable)
160             {
161                 testDynamic = true;
162                 results.append(((SimpleVariable) item).toString());
163             }
164             else
165             {
166                 results.append(item);
167             }
168         }
169         if(!testDynamic)
170         {
171             isDynamic = false;
172             permanentResults = results.toString();
173         }
174         return results.toString();
175     }
176
177     public CompoundVariable getFunction()
178     {
179         CompoundVariable func = new CompoundVariable();
180         func.compiledComponents = (LinkedList JavaDoc) compiledComponents.clone();
181         func.rawParameters = rawParameters;
182         return func;
183     }
184
185     public List JavaDoc getArgumentDesc()
186     {
187         return new LinkedList JavaDoc();
188     }
189
190     public void clear()
191     {
192         hasFunction = false;
193         compiledComponents.clear();
194     }
195
196     public void setParameters(String JavaDoc parameters)
197         throws InvalidVariableException
198     {
199         this.rawParameters = parameters;
200         if (parameters == null || parameters.length() == 0)
201         {
202             return;
203         }
204
205         compiledComponents = functionParser.compileString(parameters);
206         if (compiledComponents.size() > 1
207             || !(compiledComponents.get(0) instanceof String JavaDoc))
208         {
209             hasFunction = true;
210         }
211     }
212
213     /* (non-Javadoc)
214      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
215      */

216     public void setParameters(Collection JavaDoc parameters)
217         throws InvalidVariableException
218     {
219     }
220     
221     static Object JavaDoc getNamedFunction(String JavaDoc functionName)
222         throws InvalidVariableException
223     {
224         if(functions.containsKey(functionName))
225         {
226             try
227             {
228                 return (Function) ((Class JavaDoc) functions.get(functionName))
229                     .newInstance();
230             }
231             catch (Exception JavaDoc e)
232             {
233                 log.error("", e);
234                  throw new InvalidVariableException();
235             }
236         }
237         else
238         {
239             return new SimpleVariable(functionName);
240         }
241     }
242
243     public boolean hasFunction()
244     {
245         return hasFunction;
246     }
247
248     /**
249      * @see Function#getReferenceKey()
250      */

251     public String JavaDoc getReferenceKey()
252     {
253         return "";
254     }
255     /*
256      * NOT USED
257      *
258     private JMeterVariables getVariables()
259     {
260         return JMeterContextService.getContext().getVariables();
261     }
262     */

263 }
264
Popular Tags