Rev 1008 | Rev 1012 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
379 | jmachado | 1 | package pt.estgp.estgweb.web; |
2 | |||
3 | import org.apache.commons.net.ftp.FTPClient; |
||
4 | import org.apache.log4j.Logger; |
||
5 | |||
1005 | jmachado | 6 | import java.io.IOException; |
1008 | jmachado | 7 | import java.io.InputStream; |
1005 | jmachado | 8 | import java.net.MalformedURLException; |
9 | import java.net.URL; |
||
379 | jmachado | 10 | import java.util.HashMap; |
11 | |||
12 | /** |
||
13 | * @author Jorge |
||
14 | * @date 18/Jan/2009 |
||
15 | * @time 18:35:11 |
||
16 | */ |
||
17 | public class FtpServer |
||
18 | { |
||
19 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(FtpServer.class); |
||
20 | |||
21 | |||
22 | private static HashMap<String, FtpServer> servers = new HashMap<String, FtpServer>(); |
||
23 | |||
24 | |||
25 | 1.5.0/docs/api/java/lang/String.html">String host = null; |
||
26 | int port = 21; |
||
27 | 1.5.0/docs/api/java/lang/String.html">String username = null; |
||
28 | 1.5.0/docs/api/java/lang/String.html">String password = null; |
||
29 | |||
30 | |||
31 | public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port) |
||
32 | { |
||
33 | this.host = host; |
||
34 | if (port > 0) |
||
35 | this.port = port; |
||
36 | } |
||
37 | |||
38 | public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port, 1.5.0/docs/api/java/lang/String.html">String username, 1.5.0/docs/api/java/lang/String.html">String password) |
||
39 | { |
||
40 | this.host = host; |
||
41 | this.port = port; |
||
42 | this.username = username; |
||
43 | this.password = password; |
||
44 | } |
||
45 | |||
1005 | jmachado | 46 | private static final int FTP_TIMEOUT_SECONDS = pt.estgp.estgweb.Globals.FTP_TIMEOUT_SECONDS; |
379 | jmachado | 47 | public FTPClient getClient() throws 1.5.0/docs/api/java/io/IOException.html">IOException |
48 | { |
||
1008 | jmachado | 49 | final FTPClient ftp = new MyFTPClient(); |
1005 | jmachado | 50 | logger.info("CONNECTING... waiting " + FTP_TIMEOUT_SECONDS + " seconds"); |
51 | 1.5.0/docs/api/java/lang/Thread.html">Thread t = new 1.5.0/docs/api/java/lang/Thread.html">Thread(new 1.5.0/docs/api/java/lang/Runnable.html">Runnable() { |
||
52 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
53 | public void run() { |
||
54 | try { |
||
55 | ftp.connect(host, port); |
||
56 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
57 | logger.error(e,e); |
||
58 | } |
||
59 | } |
||
60 | }); |
||
61 | t.start(); |
||
62 | try { |
||
63 | for(int i=0;i<FTP_TIMEOUT_SECONDS;i++) |
||
64 | { |
||
65 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000); |
||
66 | if(!t.isAlive()) |
||
67 | { |
||
68 | if(ftp.isConnected()) |
||
69 | break; |
||
70 | } |
||
71 | } |
||
72 | if(!ftp.isConnected()) |
||
73 | { |
||
74 | logger.info("Not Connected will interrupt"); |
||
75 | t.interrupt(); |
||
76 | return null; |
||
77 | } |
||
78 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
79 | logger.error(e,e); |
||
80 | } |
||
915 | jmachado | 81 | |
379 | jmachado | 82 | if (username != null) |
83 | ftp.user(username); |
||
84 | if (password != null) |
||
85 | ftp.pass(password); |
||
86 | return ftp; |
||
87 | } |
||
88 | |||
89 | public 1.5.0/docs/api/java/lang/String.html">String getHost() |
||
90 | { |
||
91 | return host; |
||
92 | } |
||
93 | |||
94 | public void setHost(1.5.0/docs/api/java/lang/String.html">String host) |
||
95 | { |
||
96 | this.host = host; |
||
97 | } |
||
98 | |||
99 | public int getPort() |
||
100 | { |
||
101 | return port; |
||
102 | } |
||
103 | |||
104 | public void setPort(int port) |
||
105 | { |
||
106 | this.port = port; |
||
107 | } |
||
108 | |||
109 | public 1.5.0/docs/api/java/lang/String.html">String getUsername() |
||
110 | { |
||
111 | return username; |
||
112 | } |
||
113 | |||
114 | public void setUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
115 | { |
||
116 | this.username = username; |
||
117 | } |
||
118 | |||
119 | public 1.5.0/docs/api/java/lang/String.html">String getPassword() |
||
120 | { |
||
121 | return password; |
||
122 | } |
||
123 | |||
124 | public void setPassword(1.5.0/docs/api/java/lang/String.html">String password) |
||
125 | { |
||
126 | this.password = password; |
||
127 | } |
||
128 | |||
129 | public static FtpServer getServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass) |
||
130 | { |
||
131 | FtpServer ftpServer = servers.get(urlStr); |
||
132 | if (ftpServer == null) |
||
133 | ftpServer = addServer(urlStr, user, pass); |
||
134 | return ftpServer; |
||
135 | } |
||
136 | |||
406 | jmachado | 137 | public static FtpServer getNewServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass) |
138 | { |
||
139 | |||
140 | try |
||
141 | { |
||
142 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
143 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
144 | int port = url.getPort(); |
||
145 | if (port <= 0) |
||
146 | port = 21; |
||
147 | logger.info("Connecting to " + host + "in port:" + port); |
||
148 | return new FtpServer(host, port, user, pass); |
||
149 | } |
||
150 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
151 | { |
||
152 | logger.error(e, e); |
||
153 | } |
||
154 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
155 | { |
||
156 | logger.error(e, e); |
||
157 | } |
||
158 | return null; |
||
159 | |||
160 | } |
||
161 | |||
162 | /** |
||
379 | jmachado | 163 | * Add a ftpServer to Cache |
164 | * |
||
165 | * @param urlStr to parse |
||
166 | * @param user username |
||
167 | * @param pass password |
||
168 | * @return ftpServer |
||
169 | */ |
||
170 | private static synchronized FtpServer addServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass) |
||
171 | { |
||
172 | try |
||
173 | { |
||
174 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
175 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
176 | int port = url.getPort(); |
||
177 | if (port <= 0) |
||
178 | port = 21; |
||
489 | jmachado | 179 | logger.info("Connecting to " + host + " in port:" + port); |
379 | jmachado | 180 | FtpServer ftpServer = new FtpServer(host, port, user, pass); |
181 | servers.put(urlStr, ftpServer); |
||
182 | return ftpServer; |
||
183 | } |
||
184 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
185 | { |
||
186 | logger.error(e, e); |
||
187 | } |
||
188 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
189 | { |
||
190 | logger.error(e, e); |
||
191 | } |
||
192 | return null; |
||
193 | } |
||
406 | jmachado | 194 | |
1008 | jmachado | 195 | public static final int FTP_DATA_STORE_TIMEOUT_SECONDS = pt.estgp.estgweb.Globals.FTP_DATA_STORE_TIMEOUT_SECONDS; |
196 | public class MyFTPClient extends FTPClient implements 1.5.0/docs/api/java/lang/Runnable.html">Runnable |
||
197 | { |
||
198 | 1.5.0/docs/api/java/lang/String.html">String remote = ""; |
||
199 | 5+0%2Fdocs%2Fapi+InputStream">InputStream local = null; |
||
200 | boolean storeResult = false; |
||
406 | jmachado | 201 | |
1008 | jmachado | 202 | public void run() { |
203 | try { |
||
204 | storeResult = super.storeFile(remote, local); |
||
205 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
206 | logger.error(e,e); |
||
207 | } |
||
208 | } |
||
209 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
210 | public boolean storeFile(1.5.0/docs/api/java/lang/String.html">String remote, 5+0%2Fdocs%2Fapi+InputStream">InputStream local) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
211 | { |
||
1011 | jmachado | 212 | logger.info("FTP store Timeout protection of " + FTP_DATA_STORE_TIMEOUT_SECONDS); |
1008 | jmachado | 213 | this.remote = remote; |
214 | this.local = local; |
||
215 | 1.5.0/docs/api/java/lang/Thread.html">Thread t = new 1.5.0/docs/api/java/lang/Thread.html">Thread(this); |
||
216 | t.start(); |
||
217 | try { |
||
218 | for(int i=0;i<FTP_DATA_STORE_TIMEOUT_SECONDS;i++) |
||
219 | { |
||
220 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000); |
||
221 | if(!t.isAlive()) |
||
222 | { |
||
1011 | jmachado | 223 | break; |
1008 | jmachado | 224 | } |
225 | } |
||
1011 | jmachado | 226 | if(t.isAlive()) |
1008 | jmachado | 227 | { |
1011 | jmachado | 228 | logger.info("Timeout expired will interrupt"); |
1008 | jmachado | 229 | t.interrupt(); |
230 | return false; |
||
231 | } |
||
232 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
233 | logger.error(e,e); |
||
234 | } |
||
235 | return storeResult; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | |||
240 | |||
379 | jmachado | 241 | } |