View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.ftp.server;
8   
9   import org.mule.util.IOUtils;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.net.URL;
14  import java.util.Map;
15  
16  import org.apache.ftpserver.FtpServer;
17  import org.apache.ftpserver.FtpServerFactory;
18  import org.apache.ftpserver.ftplet.Ftplet;
19  import org.apache.ftpserver.listener.ListenerFactory;
20  import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
21  
22  /**
23   * A wrapper for the Apache ftpServer.  This will progress into a provider of its own,
24   * but for now is necessary to avoid duplicating code in FTP tests using FTPClient.
25   */
26  public class Server
27  {
28      public static final int DEFAULT_PORT = 60196; //default for most/all tests
29  
30      private FtpServer server;
31  
32      /**
33       * Initialize the ftp server on a given port
34       * 
35       * @param port The port to start the server on. Note, you need special
36       *            permissions on *nux to open port 22, so we usually choose a very
37       *            high port number.
38       * @throws Exception
39       */
40      public Server(int port) throws Exception
41      {
42          this(port, null);
43      }
44  
45      public Server(int port, Ftplet ftplet) throws Exception
46      {
47          FtpServerFactory serverFactory = new FtpServerFactory();        
48          
49          setupListenerFactory( serverFactory, port);                
50          setupUserManagerFactory(serverFactory);
51          setupFtplet(serverFactory, ftplet);
52          
53          server = serverFactory.createServer();
54          server.start();
55      }
56  
57      private void setupListenerFactory(FtpServerFactory serverFactory, int port)
58      {
59          ListenerFactory listenerFactory = new ListenerFactory();
60          // set the port of the listener
61          listenerFactory.setPort(port);
62          listenerFactory.setIdleTimeout(60000);
63          // replace the default listener
64          serverFactory.addListener("default", listenerFactory.createListener());
65      }
66  
67      private void setupUserManagerFactory(FtpServerFactory serverFactory) throws IOException
68      {
69          PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
70          URL usersFile = IOUtils.getResourceAsUrl("users.properties", getClass());
71          if (usersFile == null)
72          {
73              throw new IOException("users.properties file not found in the classpath");
74          }
75          userManagerFactory.setFile(new File(usersFile.getFile()));
76          serverFactory.setUserManager(userManagerFactory.createUserManager());
77      }
78  
79      private void setupFtplet(FtpServerFactory serverFactory, Ftplet ftplet)
80      {
81          if (ftplet == null)
82          {
83              return;
84          }
85          
86          Map<String, Ftplet> ftplets = serverFactory.getFtplets();
87          ftplets.put("MuleFtplet", ftplet);
88          serverFactory.setFtplets(ftplets);
89      }
90  
91      /**
92       * Stop the ftp server TODO DZ: we may want to put a port check + wait time in
93       * here to make sure that the port is released before we continue. Windows tends
94       * to hold on to ports longer than it should.
95       */
96      public void stop()
97      {        
98          server.stop();
99      }
100 }