/* * Copyright 2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mockftpserver.core.server; import org.apache.log4j.Logger; import org.mockftpserver.core.MockFtpServerException; import org.mockftpserver.core.command.Command; import org.mockftpserver.core.command.CommandHandler; import org.mockftpserver.core.session.DefaultSession; import org.mockftpserver.core.session.Session; import org.mockftpserver.core.socket.DefaultServerSocketFactory; import org.mockftpserver.core.socket.ServerSocketFactory; import org.mockftpserver.core.util.Assert; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.ResourceBundle; /** * 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 {@link org.mockftpserver.core.command.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 theserverControlPort
property. This 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.
*
* commandHandlerMapping
. All other default CommandHandler
* mappings remain unchanged.
*
* @param commandHandlerMapping - the Map of commandName->CommandHandler; these override the defaults
* @throws org.mockftpserver.core.util.AssertFailedException
* - if the commandHandlerMapping is null
*/
public void setCommandHandlers(Map commandHandlerMapping) {
Assert.notNull(commandHandlerMapping, "commandHandlers");
for (Iterator iter = commandHandlerMapping.keySet().iterator(); iter.hasNext();) {
String commandName = (String) iter.next();
setCommandHandler(commandName, (CommandHandler) commandHandlerMapping.get(commandName));
}
}
/**
* Set the CommandHandler for the specified command name. If the CommandHandler implements
* the {@link org.mockftpserver.core.command.ReplyTextBundleAware} interface and its replyTextBundle
attribute
* is null, then set its replyTextBundle
to the replyTextBundle
of
* this StubFtpServer.
*
* @param commandName - the command name to which the CommandHandler will be associated
* @param commandHandler - the CommandHandler
* @throws org.mockftpserver.core.util.AssertFailedException
* - if the commandName or commandHandler is null
*/
public void setCommandHandler(String commandName, CommandHandler commandHandler) {
Assert.notNull(commandName, "commandName");
Assert.notNull(commandHandler, "commandHandler");
commandHandlers.put(Command.normalizeName(commandName), commandHandler);
initializeCommandHandler(commandHandler);
}
/**
* Set the reply text ResourceBundle to a new ResourceBundle with the specified base name,
* accessible on the CLASSPATH. See {@link java.util.ResourceBundle#getBundle(String)}.
*
* @param baseName - the base name of the resource bundle, a fully qualified class name
*/
public void setReplyTextBaseName(String baseName) {
replyTextBundle = ResourceBundle.getBundle(baseName);
}
/**
* Return the ReplyText ResourceBundle. Set the bundle through the {@link #setReplyTextBaseName(String)} method.
*
* @return the reply text ResourceBundle
*/
public ResourceBundle getReplyTextBundle() {
return replyTextBundle;
}
/**
* Set the port number to which the server control connection socket will bind. The default value is 21.
*
* @param serverControlPort - the port number for the server control connection ServerSocket
*/
public void setServerControlPort(int serverControlPort) {
this.serverControlPort = serverControlPort;
}
//-------------------------------------------------------------------------
// Internal Helper Methods
//-------------------------------------------------------------------------
/**
* 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.
*
* @return true if this server is fully shutdown
*/
public boolean isShutdown() {
boolean shutdown = !serverThread.isAlive() && serverSocket.isClosed();
for (Iterator iter = sessions.values().iterator(); iter.hasNext();) {
SessionInfo sessionInfo = (SessionInfo) iter.next();
shutdown = shutdown && sessionInfo.socket.isClosed() && !sessionInfo.thread.isAlive();
}
return shutdown;
}
/**
* 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.
*
* @return true if this server has started
*/
public boolean isStarted() {
return serverThread != null && serverThread.isAlive() && serverSocket != null;
}
/**
* Create a new Session instance for the specified client Socket
*
* @param clientSocket - the Socket associated with the client
* @return a Session
*/
protected Session createSession(Socket clientSocket) {
return new DefaultSession(clientSocket, commandHandlers);
}
//------------------------------------------------------------------------------------
// Abstract method declarations
//------------------------------------------------------------------------------------
/**
* Initialize a CommandHandler that has been registered to this server. What "initialization"
* means is dependent on the subclass implementation.
*
* @param commandHandler - the CommandHandler to initialize
*/
protected abstract void initializeCommandHandler(CommandHandler commandHandler);
}