1 // re/mind - Remote Reminder Software
2 // Copyright (C) 2002 Moritz Petersen
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // $Id: FTPStorage.java,v 1.2 2002/09/29 19:33:23 powerpete Exp $
19 package de.jface.remind.io;
20
21 import de.jface.remind.Config;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.OutputStream;
29
30 import java.util.Collection;
31
32 import org.apache.commons.io.Util;
33 import org.apache.commons.net.ftp.FTPClient;
34 import org.apache.commons.net.ftp.FTPFile;
35 import org.apache.commons.net.ftp.FTPReply;
36 import org.apache.log4j.Logger;
37
38 /***
39 * Class <code>FTPStorage</code>
40 * of project RemotePostIt.
41 *
42 * @author Moritz Petersen
43 * @version $Revision: 1.2 $
44 */
45 public class FTPStorage extends Storage
46 {
47 private final Logger log4j = Logger.getLogger(getClass());
48
49 /***
50 * This method is only for testing.
51 */
52 Collection readNotes(String filename) throws StorageException
53 {
54 try
55 {
56 ByteArrayOutputStream out = new ByteArrayOutputStream();
57 FTPClient ftpClient = open();
58 log4j.debug("Reading file " + filename);
59 ftpClient.retrieveFile(filename, out);
60 log4j.debug("success.");
61 ByteArrayInputStream in =
62 new ByteArrayInputStream(out.toByteArray());
63 Collection notes = Serializer.getInstance().deserialize(in);
64
65 close(ftpClient);
66
67 return notes;
68 }
69 catch (IOException e)
70 {
71 log4j.fatal("An IO error occured.", e);
72 throw new StorageException("An IO error occured");
73 }
74 catch (SerializerException e)
75 {
76 log4j.fatal("Serialization failed.", e);
77 throw new StorageException("Serialization failed.");
78 }
79 }
80
81 public Collection readNotes()
82 throws StorageException
83 {
84 return readNotes(Config.FTP_DATA_FILE_NAME);
85 }
86
87 public void writeNotes(Collection notes)
88 throws StorageException
89 {
90 writeNotes(notes, Config.FTP_DATA_FILE_NAME);
91 }
92
93 void writeNotes(Collection notes, String filename)
94 throws StorageException
95 {
96 try
97 {
98 FTPClient ftpClient = open();
99
100 log4j.assertLog(ftpClient.storeFile(filename,
101 Serializer.getInstance()
102 .serialize(notes)),
103 "File store failed.");
104 close(ftpClient);
105 }
106 catch (IOException e)
107 {
108 log4j.fatal("An IO error occured.", e);
109 throw new StorageException("An IO error occured");
110 }
111 catch (SerializerException e)
112 {
113 log4j.fatal("Deserialization failed.", e);
114 throw new StorageException("Serialization failed.");
115 }
116 }
117
118 /***
119 * Opens the connection to the FTP server.
120 */
121 FTPClient open() throws StorageException
122 {
123 UserPreferences preferences = UserPreferences.getInstance();
124 String user = preferences.getUser();
125 String host = preferences.getHost();
126 String password = preferences.getPassword();
127
128 FTPClient ftpClient = new FTPClient();
129
130 try
131 {
132 log4j.debug("Connecting to " + host);
133 ftpClient.connect(host);
134
135 if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
136 {
137 ftpClient.disconnect();
138 throw new StorageException("Access to FTP server failed.");
139 }
140
141 ftpClient.login(user, password);
142 log4j.debug("success.");
143 }
144 catch (Exception e)
145 {
146 // try to disconnect, if still connected.
147 if (ftpClient.isConnected())
148 {
149 try
150 {
151 ftpClient.disconnect();
152 }
153 catch (IOException ioe)
154 {
155 // do nothing.
156 }
157 }
158
159 throw new StorageException(e.getMessage(), e);
160 }
161
162 return ftpClient;
163 }
164
165 /***
166 * Closes the connection to the FTP server.
167 */
168 void close(FTPClient ftpClient)
169 throws StorageException
170 {
171 try
172 {
173 log4j.debug("Disconnecting");
174 ftpClient.logout();
175 ftpClient.disconnect();
176 log4j.debug("success.");
177 }
178 catch (IOException e)
179 {
180 // try to disconnect, if still connected.
181 if (ftpClient.isConnected())
182 {
183 try
184 {
185 ftpClient.disconnect();
186 }
187 catch (IOException ioe)
188 {
189 // do nothing
190 }
191 }
192
193 throw new StorageException(e.getMessage(), e);
194 }
195 }
196 }
This page was automatically generated by Maven