org.mockftpserver.core.server
Class AbstractFtpServer

java.lang.Object
  extended by org.mockftpserver.core.server.AbstractFtpServer
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
FakeFtpServer, StubFtpServer

public abstract class AbstractFtpServer
extends Object
implements Runnable

This is the abstract superclass for "mock" implementations of an FTP Server, suitable for testing FTP client code or standing in for a live FTP server. It supports the main FTP commands by defining handlers for each of the corresponding low-level FTP server commands (e.g. RETR, DELE, LIST). These handlers implement the CommandHandler interface.

By default, mock FTP Servers bind to the server control port of 21. You can use a different server control port by setting the serverControlPort property. If you specify a value of 0, then a free port number will be chosen automatically; call getServerControlPort() AFTER start() has been called to determine the actual port number being used. Using a non-default port number is usually necessary when running on Unix or some other system where that port number is already in use or cannot be bound from a user process.

Command Handlers

You can set the existing CommandHandler defined for an FTP server command by calling the setCommandHandler(String, CommandHandler) method, passing in the FTP server command name and CommandHandler instance. You can also replace multiple command handlers at once by using the setCommandHandlers(Map) method. That is especially useful when configuring the server through the Spring Framework.

You can retrieve the existing CommandHandler defined for an FTP server command by calling the getCommandHandler(String) method, passing in the FTP server command name.

FTP Command Reply Text ResourceBundle

The default text asociated with each FTP command reply code is contained within the "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can completely replace the ResourceBundle file by calling the calling the setReplyTextBaseName(String) method.

Version:
$Revision: 264 $ - $Date: 2012-07-17 21:19:23 -0400 (Tue, 17 Jul 2012) $
Author:
Chris Mair
See Also:
FakeFtpServer, StubFtpServer

Field Summary
protected  org.slf4j.Logger LOG
           
static String REPLY_TEXT_BASENAME
          Default basename for reply text ResourceBundle
protected  ServerSocketFactory serverSocketFactory
           
 
Constructor Summary
AbstractFtpServer()
          Create a new instance.
 
Method Summary
protected  Session createSession(Socket clientSocket)
          Create a new Session instance for the specified client Socket
 CommandHandler getCommandHandler(String name)
          Return the CommandHandler defined for the specified command name
 ResourceBundle getReplyTextBundle()
          Return the ReplyText ResourceBundle.
 int getServerControlPort()
          Return the port number to which the server control connection socket will bind.
protected abstract  void initializeCommandHandler(CommandHandler commandHandler)
          Initialize a CommandHandler that has been registered to this server.
 boolean isShutdown()
          Return true if this server is fully shutdown -- i.e., there is no active (alive) threads and all sockets are closed.
 boolean isStarted()
          Return true if this server has started -- i.e., there is an active (alive) server threads and non-null server socket.
 void run()
          The logic for the server thread
 void setCommandHandler(String commandName, CommandHandler commandHandler)
          Set the CommandHandler for the specified command name.
 void setCommandHandlers(Map commandHandlerMapping)
          Override the default CommandHandlers with those in the specified Map of commandName>>CommandHandler.
 void setReplyTextBaseName(String baseName)
          Set the reply text ResourceBundle to a new ResourceBundle with the specified base name, accessible on the CLASSPATH.
 void setServerControlPort(int serverControlPort)
          Set the port number to which the server control connection socket will bind.
 void start()
          Start a new Thread for this server instance
 void stop()
          Stop this server instance and wait for it to terminate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REPLY_TEXT_BASENAME

public static final String REPLY_TEXT_BASENAME
Default basename for reply text ResourceBundle

See Also:
Constant Field Values

LOG

protected org.slf4j.Logger LOG

serverSocketFactory

protected ServerSocketFactory serverSocketFactory
Constructor Detail

AbstractFtpServer

public AbstractFtpServer()
Create a new instance. Initialize the default command handlers and reply text ResourceBundle.

Method Detail

start

public void start()
Start a new Thread for this server instance


run

public void run()
The logic for the server thread

Specified by:
run in interface Runnable
See Also:
Runnable.run()

stop

public void stop()
Stop this server instance and wait for it to terminate.


getCommandHandler

public CommandHandler getCommandHandler(String name)
Return the CommandHandler defined for the specified command name

Parameters:
name - - the command name
Returns:
the CommandHandler defined for name

setCommandHandlers

public void setCommandHandlers(Map commandHandlerMapping)
Override the default CommandHandlers with those in the specified Map of commandName>>CommandHandler. This will only override the default CommandHandlers for the keys in commandHandlerMapping. All other default CommandHandler mappings remain unchanged.

Parameters:
commandHandlerMapping - - the Map of commandName->CommandHandler; these override the defaults
Throws:
AssertFailedException - - if the commandHandlerMapping is null

setCommandHandler

public void setCommandHandler(String commandName,
                              CommandHandler commandHandler)
Set the CommandHandler for the specified command name. If the CommandHandler implements the ReplyTextBundleAware interface and its replyTextBundle attribute is null, then set its replyTextBundle to the replyTextBundle of this StubFtpServer.

Parameters:
commandName - - the command name to which the CommandHandler will be associated
commandHandler - - the CommandHandler
Throws:
AssertFailedException - - if the commandName or commandHandler is null

setReplyTextBaseName

public void setReplyTextBaseName(String baseName)
Set the reply text ResourceBundle to a new ResourceBundle with the specified base name, accessible on the CLASSPATH. See ResourceBundle.getBundle(String).

Parameters:
baseName - - the base name of the resource bundle, a fully qualified class name

getReplyTextBundle

public ResourceBundle getReplyTextBundle()
Return the ReplyText ResourceBundle. Set the bundle through the setReplyTextBaseName(String) method.

Returns:
the reply text ResourceBundle

setServerControlPort

public void setServerControlPort(int serverControlPort)
Set the port number to which the server control connection socket will bind. The default value is 21.

Parameters:
serverControlPort - - the port number for the server control connection ServerSocket

getServerControlPort

public int getServerControlPort()
Return the port number to which the server control connection socket will bind. The default value is 21.

Returns:
the port number for the server control connection ServerSocket

isShutdown

public boolean isShutdown()
Return true if this server is fully shutdown -- i.e., there is no active (alive) threads and all sockets are closed. This method is intended for testing only.

Returns:
true if this server is fully shutdown

isStarted

public boolean isStarted()
Return true if this server has started -- i.e., there is an active (alive) server threads and non-null server socket. This method is intended for testing only.

Returns:
true if this server has started

createSession

protected Session createSession(Socket clientSocket)
Create a new Session instance for the specified client Socket

Parameters:
clientSocket - - the Socket associated with the client
Returns:
a Session

initializeCommandHandler

protected abstract void initializeCommandHandler(CommandHandler commandHandler)
Initialize a CommandHandler that has been registered to this server. What "initialization" means is dependent on the subclass implementation.

Parameters:
commandHandler - - the CommandHandler to initialize


Copyright © 2014. All rights reserved.