1
2 package net.sourceforge.jdbdump.connect;
3
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.net.SocketException;
10 import java.util.Properties;
11
12 import org.apache.commons.net.ftp.FTPClient;
13 import org.apache.commons.net.ftp.FTPReply;
14
15 /***
16 * This class is a subclass of abstract ConfigurationIO class.
17 * It provides support for configuration files that are accessible throught FTP connection.
18 *
19 * @author tymoteusz
20 *
21 */
22 public class FTPConfigurationIO extends ConfigurationIO {
23
24 protected String host;
25 protected String user;
26 protected String password;
27 protected String filename;
28 protected FTPClient ftp;
29
30 public FTPConfigurationIO() throws FileNotFoundException, IOException{
31 Properties props = new Properties();
32 props.load(new FileInputStream("app.properties"));
33 host = props.getProperty("FTPConfigurationIO.host");
34 user = props.getProperty("FTPConfigurationIO.user");
35 password = props.getProperty("FTPConfigurationIO.password");
36 filename = props.getProperty("FTPConfigurationIO.filename");
37 connect();
38 }
39
40 public FTPConfigurationIO(String host, String filename, String user, String password) throws IOException {
41 this.host = host;
42 this.user = user;
43 this.filename = filename;
44 this.password = password;
45 }
46
47 protected void connect() throws SocketException, IOException{
48 ftp = new FTPClient();
49 ftp.connect(host);
50 int reply = ftp.getReplyCode();
51
52 if (!FTPReply.isPositiveCompletion(reply)) {
53 ftp.disconnect();
54 throw new IOException("can't connect to server");
55 }
56 ftp.login(user, password);
57 }
58
59 /***
60 * Implements method for gaining InputStream access to data through an FTP connection.
61 * Hostname, user, and filename on remote server are determined by host, user, and filename variables set with constructor.
62 * @throws FileNotFoundException
63 * @see #FTPConfigurationIO(String)
64 * @see ConfigurationIO#save()
65 */
66 protected InputStream getInputStream() throws IOException {
67 connect();
68 return ftp.retrieveFileStream(filename);
69 }
70
71 /***
72 * Implements method for gaining OutputStream access to data through an FTP connection.
73 * Hostname, user, and filename on remote server are determined by host, user, and filename variables set with constructor.
74 * @see #FTPConfigurationIO(String)
75 * @see ConfigurationIO#save()
76 */
77 protected OutputStream getOutputStream() throws IOException {
78 connect();
79 ftp.enterLocalPassiveMode();
80 return ftp.storeFileStream(filename);
81 }
82
83 public boolean getDefaultAutosave(){
84 return false;
85 }
86
87 public boolean getDefaultAutoload(){
88 return false;
89 }
90
91 }