AbstractFtpServer.java revision abc66ab652b34d39ea5a00a75b1d7c7cc157a84f
1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.core.server;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.MockFtpServerException;
20import org.mockftpserver.core.command.Command;
21import org.mockftpserver.core.command.CommandHandler;
22import org.mockftpserver.core.session.DefaultSession;
23import org.mockftpserver.core.session.Session;
24import org.mockftpserver.core.socket.DefaultServerSocketFactory;
25import org.mockftpserver.core.socket.ServerSocketFactory;
26import org.mockftpserver.core.util.Assert;
27
28import java.io.IOException;
29import java.net.ServerSocket;
30import java.net.Socket;
31import java.net.SocketTimeoutException;
32import java.util.HashMap;
33import java.util.Iterator;
34import java.util.Map;
35import java.util.ResourceBundle;
36
37/**
38 * <b>StubFtpServer</b> is the top-level class for a "stub" implementation of an FTP Server,
39 * suitable for testing FTP client code or standing in for a live FTP server. It supports
40 * the main FTP commands by defining handlers for each of the corresponding low-level FTP
41 * server commands (e.g. RETR, DELE, LIST). These handlers implement the {@link org.mockftpserver.core.command.CommandHandler}
42 * interface.
43 * <p/>
44 * <b>StubFtpServer</b> works out of the box with default command handlers that return
45 * success reply codes and empty data (for retrieved files, directory listings, etc.).
46 * The command handler for any command can be easily configured to return custom data
47 * or reply codes. Or it can be replaced with a custom {@link org.mockftpserver.core.command.CommandHandler}
48 * implementation. This allows simulation of a complete range of both success and
49 * failure scenarios. The command handlers can also be interrogated to verify command
50 * invocation data such as command parameters and timestamps.
51 * <p/>
52 * <b>StubFtpServer</b> can be fully configured programmatically or within a Spring Framework
53 * ({@link http://www.springframework.org/}) or similar container.
54 * <p/>
55 * <h4>Starting the StubFtpServer</h4>
56 * Here is how to start the <b>StubFtpServer</b> with the default configuration.
57 * <pre><code>
58 * StubFtpServer stubFtpServer = new StubFtpServer();
59 * stubFtpServer.start();
60 * </code></pre>
61 * <p/>
62 * <h4>Retrieving Command Handlers</h4>
63 * You can retrieve the existing {@link org.mockftpserver.core.command.CommandHandler} defined for an FTP server command
64 * by calling the {@link #getCommandHandler(String)} method, passing in the FTP server
65 * command name. For example:
66 * <pre><code>
67 * PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD");
68 * </code></pre>
69 * <p/>
70 * <h4>Replacing Command Handlers</h4>
71 * You can replace the existing {@link org.mockftpserver.core.command.CommandHandler} defined for an FTP server command
72 * by calling the {@link #setCommandHandler(String, org.mockftpserver.core.command.CommandHandler)} method, passing
73 * in the FTP server command name and {@link org.mockftpserver.core.command.CommandHandler} instance. For example:
74 * <pre><code>
75 * PwdCommandHandler pwdCommandHandler = new PwdCommandHandler();
76 * pwdCommandHandler.setDirectory("some/dir");
77 * stubFtpServer.setCommandHandler("PWD", pwdCommandHandler);
78 * </code></pre>
79 * You can also replace multiple command handlers at once by using the {@link #setCommandHandlers(java.util.Map)}
80 * method. That is especially useful when configuring the server through the <b>Spring Framework</b>.
81 * <h4>FTP Command Reply Text ResourceBundle</h4>
82 * <p/>
83 * The default text asociated with each FTP command reply code is contained within the
84 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
85 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
86 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
87 * completely replace the ResourceBundle file by calling the calling the
88 * {@link #setReplyTextBaseName(String)} method.
89 *
90 * @author Chris Mair
91 * @version $Revision$ - $Date$
92 */
93public abstract class AbstractFtpServer implements Runnable {
94
95    /**
96     * Default basename for reply text ResourceBundle
97     */
98    public static final String REPLY_TEXT_BASENAME = "ReplyText";
99    private static final int DEFAULT_SERVER_CONTROL_PORT = 21;
100
101    protected Logger LOG = Logger.getLogger(getClass());
102
103    // Simple value object that holds the socket and thread for a single session
104    private static class SessionInfo {
105        private Socket socket;
106        private Thread thread;
107    }
108
109    private ServerSocketFactory serverSocketFactory = new DefaultServerSocketFactory();
110    private ServerSocket serverSocket = null;
111    ResourceBundle replyTextBundle;             // non-private for testing only
112    private volatile boolean terminate = false;
113    private Map commandHandlers;
114    private Thread serverThread;
115    private int serverControlPort = DEFAULT_SERVER_CONTROL_PORT;
116    private final Object startLock = new Object();
117
118    // Map of Session -> SessionInfo
119    private Map sessions = new HashMap();
120
121    /**
122     * Create a new instance. Initialize the default command handlers and
123     * reply text ResourceBundle.
124     */
125    public AbstractFtpServer() {
126//        replyTextBundle = ResourceBundle.getBundle(REPLY_TEXT_BASENAME);
127
128        commandHandlers = new HashMap();
129
130//        PwdCommandHandler pwdCommandHandler = new ConnectCommandHandler();
131//
132//        // Initialize the default CommandHandler mappings
133//        setCommandHandler(CommandNames.ABOR, new AborCommandHandler());
134//        setCommandHandler(CommandNames.ACCT, new AcctCommandHandler());
135//        setCommandHandler(CommandNames.ALLO, new AlloCommandHandler());
136//        setCommandHandler(CommandNames.APPE, new AppeCommandHandler());
137//        setCommandHandler(CommandNames.PWD, pwdCommandHandler);            // same as XPWD
138//        setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler());
139//        setCommandHandler(CommandNames.CWD, new CwdCommandHandler());
140//        setCommandHandler(CommandNames.CDUP, new CdupCommandHandler());
141//        setCommandHandler(CommandNames.DELE, new DeleCommandHandler());
142//        setCommandHandler(CommandNames.HELP, new HelpCommandHandler());
143//        setCommandHandler(CommandNames.LIST, new ListCommandHandler());
144//        setCommandHandler(CommandNames.MKD, new MkdCommandHandler());
145//        setCommandHandler(CommandNames.MODE, new ModeCommandHandler());
146//        setCommandHandler(CommandNames.NOOP, new NoopCommandHandler());
147//        setCommandHandler(CommandNames.NLST, new NlstCommandHandler());
148//        setCommandHandler(CommandNames.PASS, new PassCommandHandler());
149//        setCommandHandler(CommandNames.PASV, new PasvCommandHandler());
150//        setCommandHandler(CommandNames.PORT, new PortCommandHandler());
151//        setCommandHandler(CommandNames.RETR, new RetrCommandHandler());
152//        setCommandHandler(CommandNames.QUIT, new QuitCommandHandler());
153//        setCommandHandler(CommandNames.REIN, new ReinCommandHandler());
154//        setCommandHandler(CommandNames.REST, new RestCommandHandler());
155//        setCommandHandler(CommandNames.RMD, new RmdCommandHandler());
156//        setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler());
157//        setCommandHandler(CommandNames.RNTO, new RntoCommandHandler());
158//        setCommandHandler(CommandNames.SITE, new SiteCommandHandler());
159//        setCommandHandler(CommandNames.SMNT, new SmntCommandHandler());
160//        setCommandHandler(CommandNames.STAT, new StatCommandHandler());
161//        setCommandHandler(CommandNames.STOR, new StorCommandHandler());
162//        setCommandHandler(CommandNames.STOU, new StouCommandHandler());
163//        setCommandHandler(CommandNames.STRU, new StruCommandHandler());
164//        setCommandHandler(CommandNames.SYST, new SystCommandHandler());
165//        setCommandHandler(CommandNames.TYPE, new TypeCommandHandler());
166//        setCommandHandler(CommandNames.USER, new UserCommandHandler());
167//        setCommandHandler(CommandNames.XPWD, pwdCommandHandler);           // same as PWD
168    }
169
170    /**
171     * Start a new Thread for this server instance
172     */
173    public void start() {
174        serverThread = new Thread(this);
175        serverThread.start();
176
177        // Wait until the thread is initialized
178        synchronized (startLock) {
179            try {
180                startLock.wait();
181            }
182            catch (InterruptedException e) {
183                e.printStackTrace();
184                throw new MockFtpServerException(e);
185            }
186        }
187    }
188
189    /**
190     * The logic for the server thread
191     *
192     * @see Runnable#run()
193     */
194    public void run() {
195        try {
196            LOG.info("Starting the server on port " + serverControlPort);
197            serverSocket = serverSocketFactory.createServerSocket(serverControlPort);
198
199            // Notify to allow the start() method to finish and return
200            synchronized (startLock) {
201                startLock.notify();
202            }
203
204            serverSocket.setSoTimeout(500);
205            while (!terminate) {
206                try {
207                    Socket clientSocket = serverSocket.accept();
208                    LOG.info("Connection accepted from host " + clientSocket.getInetAddress());
209
210                    DefaultSession session = new DefaultSession(clientSocket, commandHandlers);
211                    Thread sessionThread = new Thread(session);
212                    sessionThread.start();
213
214                    SessionInfo sessionInfo = new SessionInfo();
215                    sessionInfo.socket = clientSocket;
216                    sessionInfo.thread = sessionThread;
217                    sessions.put(session, sessionInfo);
218                }
219                catch (SocketTimeoutException socketTimeoutException) {
220                    LOG.trace("Socket accept() timeout");
221                }
222            }
223        }
224        catch (IOException e) {
225            LOG.error("Error", e);
226        }
227        finally {
228
229            LOG.debug("Cleaning up server...");
230
231            try {
232                serverSocket.close();
233
234                for (Iterator iter = sessions.keySet().iterator(); iter.hasNext();) {
235                    Session session = (Session) iter.next();
236                    SessionInfo sessionInfo = (SessionInfo) sessions.get(session);
237                    session.close();
238                    sessionInfo.thread.join(500L);
239                    Socket sessionSocket = sessionInfo.socket;
240                    if (sessionSocket != null) {
241                        sessionSocket.close();
242                    }
243                }
244            }
245            catch (IOException e) {
246                e.printStackTrace();
247                throw new MockFtpServerException(e);
248            }
249            catch (InterruptedException e) {
250                e.printStackTrace();
251                throw new MockFtpServerException(e);
252            }
253            LOG.info("Server stopped.");
254        }
255    }
256
257    /**
258     * Stop this server instance and wait for it to terminate.
259     */
260    public void stop() {
261
262        LOG.trace("Stopping the server...");
263        terminate = true;
264
265        try {
266            serverThread.join();
267        }
268        catch (InterruptedException e) {
269            e.printStackTrace();
270            throw new MockFtpServerException(e);
271        }
272    }
273
274    /**
275     * Return the CommandHandler defined for the specified command name
276     *
277     * @param name - the command name
278     * @return the CommandHandler defined for name
279     */
280    public CommandHandler getCommandHandler(String name) {
281        return (CommandHandler) commandHandlers.get(Command.normalizeName(name));
282    }
283
284    /**
285     * Override the default CommandHandlers with those in the specified Map of
286     * commandName>>CommandHandler. This will only override the default CommandHandlers
287     * for the keys in <code>commandHandlerMapping</code>. All other default CommandHandler
288     * mappings remain unchanged.
289     *
290     * @param commandHandlerMapping - the Map of commandName->CommandHandler; these override the defaults
291     * @throws org.mockftpserver.core.util.AssertFailedException
292     *          - if the commandHandlerMapping is null
293     */
294    public void setCommandHandlers(Map commandHandlerMapping) {
295        Assert.notNull(commandHandlerMapping, "commandHandlers");
296        for (Iterator iter = commandHandlerMapping.keySet().iterator(); iter.hasNext();) {
297            String commandName = (String) iter.next();
298            setCommandHandler(commandName, (CommandHandler) commandHandlerMapping.get(commandName));
299        }
300    }
301
302    /**
303     * Set the CommandHandler for the specified command name. If the CommandHandler implements
304     * the {@link org.mockftpserver.core.command.ReplyTextBundleAware} interface and its <code>replyTextBundle</code> attribute
305     * is null, then set its <code>replyTextBundle</code> to the <code>replyTextBundle</code> of
306     * this StubFtpServer.
307     *
308     * @param commandName    - the command name to which the CommandHandler will be associated
309     * @param commandHandler - the CommandHandler
310     * @throws org.mockftpserver.core.util.AssertFailedException
311     *          - if the commandName or commandHandler is null
312     */
313    public void setCommandHandler(String commandName, CommandHandler commandHandler) {
314        Assert.notNull(commandName, "commandName");
315        Assert.notNull(commandHandler, "commandHandler");
316        commandHandlers.put(Command.normalizeName(commandName), commandHandler);
317        initializeCommandHandler(commandHandler);
318    }
319
320    /**
321     * Set the reply text ResourceBundle to a new ResourceBundle with the specified base name,
322     * accessible on the CLASSPATH. See {@link java.util.ResourceBundle#getBundle(String)}.
323     *
324     * @param baseName - the base name of the resource bundle, a fully qualified class name
325     */
326    public void setReplyTextBaseName(String baseName) {
327        replyTextBundle = ResourceBundle.getBundle(baseName);
328    }
329
330    /**
331     * Set the port number to which the server control connection socket will bind. The default value is 21.
332     *
333     * @param serverControlPort - the port number for the server control connection ServerSocket
334     */
335    public void setServerControlPort(int serverControlPort) {
336        this.serverControlPort = serverControlPort;
337    }
338
339    //-------------------------------------------------------------------------
340    // Internal Helper Methods
341    //-------------------------------------------------------------------------
342
343    /**
344     * Return true if this server is fully shutdown -- i.e., there is no active (alive) threads and
345     * all sockets are closed. This method is intended for testing only.
346     *
347     * @return true if this server is fully shutdown
348     */
349    public boolean isShutdown() {
350        boolean shutdown = !serverThread.isAlive() && serverSocket.isClosed();
351
352        for (Iterator iter = sessions.keySet().iterator(); iter.hasNext();) {
353            SessionInfo sessionInfo = (SessionInfo) iter.next();
354            shutdown = shutdown && sessionInfo.socket.isClosed() && !sessionInfo.thread.isAlive();
355        }
356        return shutdown;
357    }
358
359    /**
360     * Return true if this server has started -- i.e., there is an active (alive) server threads
361     * and non-null server socket. This method is intended for testing only.
362     *
363     * @return true if this server has started
364     */
365    public boolean isStarted() {
366        return serverThread != null && serverThread.isAlive() && serverSocket != null;
367    }
368
369    //------------------------------------------------------------------------------------
370    // Abstract method declarations
371    //------------------------------------------------------------------------------------
372
373    /**
374     * Initialize a CommandHandler that has been registered to this server. What "initialization"
375     * means is dependent on the subclass implementation.
376     *
377     * @param commandHandler - the CommandHandler to initialize
378     */
379    protected abstract void initializeCommandHandler(CommandHandler commandHandler);
380
381}