Source Code Cross Referenced for BeanShellSampler.java in  » Testing » jakarta-jmeter » org » apache » jmeter » protocol » java » sampler » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.protocol.java.sampler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:
019:        package org.apache.jmeter.protocol.java.sampler;
020:
021:        import org.apache.jmeter.samplers.Entry;
022:        import org.apache.jmeter.samplers.SampleResult;
023:        import org.apache.jmeter.samplers.Sampler;
024:        import org.apache.jmeter.threads.JMeterContext;
025:        import org.apache.jmeter.threads.JMeterContextService;
026:        import org.apache.jmeter.threads.JMeterVariables;
027:        import org.apache.jmeter.util.BeanShellInterpreter;
028:        import org.apache.jmeter.util.BeanShellTestElement;
029:        import org.apache.jorphan.logging.LoggingManager;
030:        import org.apache.jorphan.util.JOrphanUtils;
031:        import org.apache.log.Logger;
032:
033:        /**
034:         * A sampler which understands BeanShell
035:         * 
036:         */
037:        public class BeanShellSampler extends BeanShellTestElement implements 
038:                Sampler {
039:            private static final Logger log = LoggingManager
040:                    .getLoggerForClass();
041:
042:            private static final long serialVersionUID = 3;
043:
044:            public static final String FILENAME = "BeanShellSampler.filename"; //$NON-NLS-1$
045:
046:            public static final String SCRIPT = "BeanShellSampler.query"; //$NON-NLS-1$
047:
048:            public static final String PARAMETERS = "BeanShellSampler.parameters"; //$NON-NLS-1$
049:
050:            public static final String INIT_FILE = "beanshell.sampler.init"; //$NON-NLS-1$
051:
052:            protected String getInitFileProperty() {
053:                return INIT_FILE;
054:            }
055:
056:            /**
057:             * Returns a formatted string label describing this sampler
058:             * 
059:             * @return a formatted string label describing this sampler
060:             */
061:
062:            public String getLabel() {
063:                return getName();
064:            }
065:
066:            public String getScript() {
067:                return this .getPropertyAsString(SCRIPT);
068:            }
069:
070:            public String getFilename() {
071:                return getPropertyAsString(FILENAME);
072:            }
073:
074:            public String getParameters() {
075:                return getPropertyAsString(PARAMETERS);
076:            }
077:
078:            public SampleResult sample(Entry e)// Entry tends to be ignored ...
079:            {
080:                // log.info(getLabel()+" "+getFilename());
081:                SampleResult res = new SampleResult();
082:                boolean isSuccessful = false;
083:                res.setSampleLabel(getLabel());
084:                res.sampleStart();
085:                final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
086:                if (bshInterpreter == null) {
087:                    res.sampleEnd();
088:                    res.setResponseCode("503");//$NON-NLS-1$
089:                    res.setResponseMessage("BeanShell Interpreter not found");
090:                    res.setSuccessful(false);
091:                    return res;
092:                }
093:                try {
094:                    String request = getScript();
095:                    String fileName = getFilename();
096:                    if (fileName.length() == 0) {
097:                        res.setSamplerData(request);
098:                    } else {
099:                        res.setSamplerData(fileName);
100:                    }
101:
102:                    bshInterpreter.set("Label", getLabel()); //$NON-NLS-1$
103:                    bshInterpreter.set("FileName", getFilename()); //$NON-NLS-1$
104:                    bshInterpreter.set("SampleResult", res); //$NON-NLS-1$
105:
106:                    // Save parameters as single line and as string array
107:                    bshInterpreter.set("Parameters", getParameters());//$NON-NLS-1$
108:                    bshInterpreter.set("bsh.args", //$NON-NLS-1$
109:                            JOrphanUtils.split(getParameters(), " "));//$NON-NLS-1$
110:
111:                    // Set default values
112:                    bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$
113:                    bshInterpreter.set("ResponseMessage", "OK");//$NON-NLS-1$
114:                    bshInterpreter.set("IsSuccess", true);//$NON-NLS-1$
115:
116:                    // Add variables for access to context and variables
117:                    JMeterContext jmctx = JMeterContextService.getContext();
118:                    JMeterVariables vars = jmctx.getVariables();
119:                    bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
120:                    bshInterpreter.set("vars", vars);//$NON-NLS-1$
121:
122:                    res.setDataType(SampleResult.TEXT); // assume text output - script can override if necessary
123:
124:                    Object bshOut;
125:
126:                    if (fileName.length() == 0) {
127:                        bshOut = bshInterpreter.eval(request);
128:                    } else {
129:                        bshOut = bshInterpreter.source(fileName);
130:                    }
131:
132:                    if (bshOut != null) {// Set response data
133:                        String out = bshOut.toString();
134:                        res.setResponseData(out.getBytes());
135:                    }
136:                    // script can also use setResponseData() so long as it returns null
137:
138:                    res.setResponseCode(bshInterpreter
139:                            .get("ResponseCode").toString());//$NON-NLS-1$
140:                    res.setResponseMessage(bshInterpreter
141:                            .get("ResponseMessage").toString());//$NON-NLS-1$
142:                    isSuccessful = Boolean.valueOf(
143:                            bshInterpreter.get("IsSuccess") //$NON-NLS-1$
144:                                    .toString()).booleanValue();
145:                }
146:                /*
147:                 * To avoid class loading problems when bsh,jar is missing, we don't try
148:                 * to catch this error separately catch (bsh.EvalError ex) {
149:                 * log.debug("",ex); res.setResponseCode("500");//$NON-NLS-1$
150:                 * res.setResponseMessage(ex.toString()); }
151:                 */
152:                // but we do trap this error to make tests work better
153:                catch (NoClassDefFoundError ex) {
154:                    log.error("BeanShell Jar missing? " + ex.toString());
155:                    res.setResponseCode("501");//$NON-NLS-1$
156:                    res.setResponseMessage(ex.toString());
157:                    res.setStopThread(true); // No point continuing
158:                } catch (Exception ex) // Mainly for bsh.EvalError
159:                {
160:                    log.warn(ex.toString());
161:                    res.setResponseCode("500");//$NON-NLS-1$
162:                    res.setResponseMessage(ex.toString());
163:                }
164:
165:                res.sampleEnd();
166:
167:                // Set if we were successful or not
168:                res.setSuccessful(isSuccessful);
169:
170:                return res;
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.