1   /*
2    * $Id: Server.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.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   * An initial wrapper for the Apache ftpServer.  This will progress into a provider of its own,
23   * but for now is necessary to avoid duplicating code in FTP tests using FTPClient.
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          // this must be set BEFORE the configuration is created
39          // it is accessed BEFORE server startup
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  }