KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > ftp > sampler > FTPSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/ftp/org/apache/jmeter/protocol/ftp/sampler/FTPSampler.java,v 1.10 2004/02/11 23:59:31 sebb Exp $
2
/*
3  * Copyright 2001-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.protocol.ftp.sampler;
20
21 import org.apache.jmeter.config.ConfigTestElement;
22 import org.apache.jmeter.samplers.AbstractSampler;
23 import org.apache.jmeter.samplers.Entry;
24 import org.apache.jmeter.samplers.SampleResult;
25
26 /**
27  * A sampler which understands FTP file requests.
28  *
29  * @version $Revision: 1.10 $ last updated $Date: 2004/02/11 23:59:31 $
30  */

31 public class FTPSampler extends AbstractSampler
32 {
33     public final static String JavaDoc SERVER = "FTPSampler.server";
34     public final static String JavaDoc FILENAME = "FTPSampler.filename";
35
36     public FTPSampler()
37     {
38     }
39
40     public String JavaDoc getUsername()
41     {
42         return getPropertyAsString(ConfigTestElement.USERNAME);
43     }
44
45     public String JavaDoc getPassword()
46     {
47         return getPropertyAsString(ConfigTestElement.PASSWORD);
48     }
49
50     public void setServer(String JavaDoc newServer)
51     {
52         this.setProperty(SERVER, newServer);
53     }
54     public String JavaDoc getServer()
55     {
56         return getPropertyAsString(SERVER);
57     }
58     public void setFilename(String JavaDoc newFilename)
59     {
60         this.setProperty(FILENAME, newFilename);
61     }
62     public String JavaDoc getFilename()
63     {
64         return getPropertyAsString(FILENAME);
65     }
66
67     /**
68      * Returns a formatted string label describing this sampler
69      * Example output:
70      * ftp://ftp.nowhere.com/pub/README.txt
71      *
72      * @return a formatted string label describing this sampler
73      */

74     public String JavaDoc getLabel()
75     {
76         return ("ftp://" + this.getServer() + "/" + this.getFilename());
77     }
78
79     public SampleResult sample(Entry e)
80     {
81         SampleResult res = new SampleResult();
82         boolean isSuccessful = false;
83         //FtpConfig ftpConfig = (FtpConfig)e.getConfigElement(FtpConfig.class);
84
res.setSampleLabel(getLabel());
85         //LoginConfig loginConfig =
86
// (LoginConfig)e.getConfigElement(LoginConfig.class);
87
res.sampleStart();
88         try
89         {
90             FtpClient ftp = new FtpClient();
91             ftp.connect(getServer(), getUsername(), getPassword());
92             ftp.setPassive(true);
93             // this should probably come from the setup dialog
94
String JavaDoc s = ftp.get(getFilename());
95             res.setResponseData(s.getBytes());
96             // set the response code here somewhere
97
ftp.disconnect();
98             isSuccessful = true;
99         }
100         catch (java.net.ConnectException JavaDoc cex)
101         {
102             // java.net.ConnectException -- 502 error code?
103
// in the future, possibly define and place error codes into the
104
// result so we know exactly what happened.
105
res.setResponseData(cex.toString().getBytes());
106         }
107         catch (Exception JavaDoc ex)
108         {
109             // general exception
110
res.setResponseData(ex.toString().getBytes());
111         }
112
113         res.sampleEnd();
114
115         // Set if we were successful or not
116
res.setSuccessful(isSuccessful);
117
118         return res;
119     }
120 }
121
Popular Tags