View Javadoc

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