1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ftp.server;
12
13 import java.util.Properties;
14
15 import org.apache.ftpserver.ConfigurableFtpServerContext;
16 import org.apache.ftpserver.FtpServer;
17 import org.apache.ftpserver.config.PropertiesConfiguration;
18 import org.apache.ftpserver.ftplet.Configuration;
19 import org.apache.ftpserver.interfaces.FtpServerContext;
20
21
22
23
24
25 public class Server
26 {
27
28 private static final String FTP_STATE_KEY = "ftp-state-key-";
29 private FtpServer server;
30 private ServerState state;
31 private int port;
32
33 public Server(int port) throws Exception
34 {
35 this.port = port;
36 this.state = new InOutState();
37
38
39
40 System.getProperties().put(FTP_STATE_KEY + port, state);
41
42 Properties properties = new Properties();
43 properties.setProperty("config.listeners.default.port", Integer.toString(port));
44 properties.setProperty("config.file-system-manager.class", FileManager.class.getName());
45 properties.setProperty("config.file-system-manager.stateFromSystemProperties", FTP_STATE_KEY + port);
46 properties.setProperty("config.connection-manager.default-idle-time", "1");
47 properties.setProperty("config.connection-manager.max-login", "1000");
48 properties.setProperty("config.connection-manager.max-anonymous-login", "1000");
49 properties.setProperty("config.user-manager.class", InMemoryUserManager.class.getName());
50 properties.setProperty("config.ip-restrictor.class", InMemoryIpRestrictor.class.getName());
51
52 Configuration config = new PropertiesConfiguration(properties);
53 FtpServerContext context = new ConfigurableFtpServerContext(config);
54
55
56 server = new FtpServer(context);
57 server.start();
58 }
59
60 public void awaitStart(long ms) throws InterruptedException
61 {
62 state.awaitStart(ms);
63 }
64
65 public NamedPayload awaitUpload(long ms) throws InterruptedException
66 {
67 return state.awaitUpload(ms);
68 }
69
70 public void stop()
71 {
72 server.stop();
73 System.getProperties().remove(FTP_STATE_KEY + port);
74 }
75
76 }