KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/LogFunction2.java,v 1.3.2.1 2004/06/19 15:45:01 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.jorphan.logging.LoggingManager;
30 import org.apache.log.Logger;
31
32 /**
33  * Function to log a message
34  *
35  * Parameters:
36  * - string
37  * - log level (optional; defaults to INFO; or DEBUG if unrecognised)
38  * - throwable message (optional)
39  *
40  * Returns:
41  * - Empty String (so can be used where return value would be a nuisance)
42  *
43  * @version $Revision: 1.3.2.1 $ Updated: $Date: 2004/06/19 15:45:01 $
44  */

45 public class LogFunction2 extends AbstractFunction implements Serializable JavaDoc
46 {
47     private static Logger log = LoggingManager.getLoggerForClass();
48
49     private static final List JavaDoc desc = new LinkedList JavaDoc();
50     private static final String JavaDoc KEY = "__logn";
51
52     // Number of parameters expected - used to reject invalid calls
53
private static final int MIN_PARAMETER_COUNT = 1;
54     private static final int MAX_PARAMETER_COUNT = 3;
55     static {
56         desc.add("String to be logged");
57         desc.add("Log level (default INFO)");
58         desc.add("Throwable text (optional)");
59     }
60     private static final String JavaDoc DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
61

62     private Object JavaDoc[] values;
63
64     public LogFunction2()
65     {
66     }
67
68     public Object JavaDoc clone()
69     {
70         return new LogFunction2();
71     }
72
73     public synchronized String JavaDoc execute(
74         SampleResult previousResult,
75         Sampler currentSampler)
76         throws InvalidVariableException
77     {
78         String JavaDoc stringToLog = ((CompoundVariable) values[0]).execute();
79         
80         String JavaDoc priorityString;
81         if (values.length > 1){ // We have a default
82
priorityString= ((CompoundVariable) values[1]).execute();
83             if (priorityString.length()==0) priorityString= DEFAULT_PRIORITY;
84         } else {
85             priorityString = DEFAULT_PRIORITY;
86         }
87         
88         Throwable JavaDoc t=null;
89         if (values.length > 2){ // Throwable wanted
90
t = new Throwable JavaDoc(((CompoundVariable) values[2]).execute());
91         }
92
93         LogFunction.logDetails(log,stringToLog,priorityString,t);
94         
95         return "";
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