Rev 379 | Rev 489 | 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 | |||
6 | import java.util.HashMap; |
||
7 | import java.net.URL; |
||
8 | import java.net.MalformedURLException; |
||
9 | import java.io.IOException; |
||
10 | |||
11 | /** |
||
12 | * @author Jorge |
||
13 | * @date 18/Jan/2009 |
||
14 | * @time 18:35:11 |
||
15 | */ |
||
16 | public class FtpServer |
||
17 | { |
||
18 | 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); |
||
19 | |||
20 | |||
21 | private static HashMap<String, FtpServer> servers = new HashMap<String, FtpServer>(); |
||
22 | |||
23 | |||
24 | 1.5.0/docs/api/java/lang/String.html">String host = null; |
||
25 | int port = 21; |
||
26 | 1.5.0/docs/api/java/lang/String.html">String username = null; |
||
27 | 1.5.0/docs/api/java/lang/String.html">String password = null; |
||
28 | |||
29 | |||
30 | public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port) |
||
31 | { |
||
32 | this.host = host; |
||
33 | if (port > 0) |
||
34 | this.port = port; |
||
35 | } |
||
36 | |||
37 | 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) |
||
38 | { |
||
39 | this.host = host; |
||
40 | this.port = port; |
||
41 | this.username = username; |
||
42 | this.password = password; |
||
43 | } |
||
44 | |||
45 | public FTPClient getClient() throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
46 | { |
||
47 | FTPClient ftp = new FTPClient(); |
||
48 | ftp.connect(host, port); |
||
49 | if (username != null) |
||
50 | ftp.user(username); |
||
51 | if (password != null) |
||
52 | ftp.pass(password); |
||
53 | return ftp; |
||
54 | } |
||
55 | |||
56 | public 1.5.0/docs/api/java/lang/String.html">String getHost() |
||
57 | { |
||
58 | return host; |
||
59 | } |
||
60 | |||
61 | public void setHost(1.5.0/docs/api/java/lang/String.html">String host) |
||
62 | { |
||
63 | this.host = host; |
||
64 | } |
||
65 | |||
66 | public int getPort() |
||
67 | { |
||
68 | return port; |
||
69 | } |
||
70 | |||
71 | public void setPort(int port) |
||
72 | { |
||
73 | this.port = port; |
||
74 | } |
||
75 | |||
76 | public 1.5.0/docs/api/java/lang/String.html">String getUsername() |
||
77 | { |
||
78 | return username; |
||
79 | } |
||
80 | |||
81 | public void setUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
82 | { |
||
83 | this.username = username; |
||
84 | } |
||
85 | |||
86 | public 1.5.0/docs/api/java/lang/String.html">String getPassword() |
||
87 | { |
||
88 | return password; |
||
89 | } |
||
90 | |||
91 | public void setPassword(1.5.0/docs/api/java/lang/String.html">String password) |
||
92 | { |
||
93 | this.password = password; |
||
94 | } |
||
95 | |||
96 | 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) |
||
97 | { |
||
98 | FtpServer ftpServer = servers.get(urlStr); |
||
99 | if (ftpServer == null) |
||
100 | ftpServer = addServer(urlStr, user, pass); |
||
101 | return ftpServer; |
||
102 | } |
||
103 | |||
406 | jmachado | 104 | 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) |
105 | { |
||
106 | |||
107 | try |
||
108 | { |
||
109 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
110 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
111 | int port = url.getPort(); |
||
112 | if (port <= 0) |
||
113 | port = 21; |
||
114 | logger.info("Connecting to " + host + "in port:" + port); |
||
115 | return new FtpServer(host, port, user, pass); |
||
116 | } |
||
117 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
118 | { |
||
119 | logger.error(e, e); |
||
120 | } |
||
121 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
122 | { |
||
123 | logger.error(e, e); |
||
124 | } |
||
125 | return null; |
||
126 | |||
127 | } |
||
128 | |||
129 | /** |
||
379 | jmachado | 130 | * Add a ftpServer to Cache |
131 | * |
||
132 | * @param urlStr to parse |
||
133 | * @param user username |
||
134 | * @param pass password |
||
135 | * @return ftpServer |
||
136 | */ |
||
137 | 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) |
||
138 | { |
||
139 | try |
||
140 | { |
||
141 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr); |
||
142 | 1.5.0/docs/api/java/lang/String.html">String host = url.getHost(); |
||
143 | int port = url.getPort(); |
||
144 | if (port <= 0) |
||
145 | port = 21; |
||
146 | logger.info("Connecting to " + host + "in port:" + port); |
||
147 | FtpServer ftpServer = new FtpServer(host, port, user, pass); |
||
148 | servers.put(urlStr, ftpServer); |
||
149 | return ftpServer; |
||
150 | } |
||
151 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
152 | { |
||
153 | logger.error(e, e); |
||
154 | } |
||
155 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
156 | { |
||
157 | logger.error(e, e); |
||
158 | } |
||
159 | return null; |
||
160 | } |
||
406 | jmachado | 161 | |
162 | |||
379 | jmachado | 163 | } |