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.*; 30import java.util.HashMap; 31import java.util.Iterator; 32import java.util.Map; 33import java.util.ResourceBundle; 34 35/** 36 * This is the abstract superclass for "mock" implementations of an FTP Server, 37 * suitable for testing FTP client code or standing in for a live FTP server. It supports 38 * the main FTP commands by defining handlers for each of the corresponding low-level FTP 39 * server commands (e.g. RETR, DELE, LIST). These handlers implement the {@link org.mockftpserver.core.command.CommandHandler} 40 * interface. 41 * <p/> 42 * By default, mock FTP Servers bind to the server control port of 21. You can use a different server control 43 * port by setting the <code>serverControlPort</code> property. If you specify a value of <code>0</code>, 44 * then a free port number will be chosen automatically; call <code>getServerControlPort()</code> AFTER 45 * <code>start()</code> has been called to determine the actual port number being used. Using a non-default 46 * port number is usually necessary when running on Unix or some other system where that port number is 47 * already in use or cannot be bound from a user process. 48 * <p/> 49 * <h4>Command Handlers</h4> 50 * You can set the existing {@link CommandHandler} defined for an FTP server command 51 * by calling the {@link #setCommandHandler(String, CommandHandler)} method, passing 52 * in the FTP server command name and {@link CommandHandler} instance. 53 * You can also replace multiple command handlers at once by using the {@link #setCommandHandlers(Map)} 54 * method. That is especially useful when configuring the server through the <b>Spring Framework</b>. 55 * <p/> 56 * You can retrieve the existing {@link CommandHandler} defined for an FTP server command by 57 * calling the {@link #getCommandHandler(String)} method, passing in the FTP server command name. 58 * <p/> 59 * <h4>FTP Command Reply Text ResourceBundle</h4> 60 * The default text asociated with each FTP command reply code is contained within the 61 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a 62 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of 63 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can 64 * completely replace the ResourceBundle file by calling the calling the 65 * {@link #setReplyTextBaseName(String)} method. 66 * 67 * @author Chris Mair 68 * @version $Revision$ - $Date$ 69 * @see org.mockftpserver.fake.FakeFtpServer 70 * @see org.mockftpserver.stub.StubFtpServer 71 */ 72public abstract class AbstractFtpServer implements Runnable { 73 74 /** 75 * Default basename for reply text ResourceBundle 76 */ 77 public static final String REPLY_TEXT_BASENAME = "ReplyText"; 78 private static final int DEFAULT_SERVER_CONTROL_PORT = 21; 79 80 protected Logger LOG = Logger.getLogger(getClass()); 81 82 // Simple value object that holds the socket and thread for a single session 83 private static class SessionInfo { 84 Socket socket; 85 Thread thread; 86 } 87 88 protected ServerSocketFactory serverSocketFactory = new DefaultServerSocketFactory(); 89 private ServerSocket serverSocket = null; 90 private ResourceBundle replyTextBundle; 91 private volatile boolean terminate = false; 92 private Map commandHandlers; 93 private Thread serverThread; 94 private int serverControlPort = DEFAULT_SERVER_CONTROL_PORT; 95 private final Object startLock = new Object(); 96 97 // Map of Session -> SessionInfo 98 private Map sessions = new HashMap(); 99 100 /** 101 * Create a new instance. Initialize the default command handlers and 102 * reply text ResourceBundle. 103 */ 104 public AbstractFtpServer() { 105 replyTextBundle = ResourceBundle.getBundle(REPLY_TEXT_BASENAME); 106 commandHandlers = new HashMap(); 107 } 108 109 /** 110 * Start a new Thread for this server instance 111 */ 112 public void start() { 113 serverThread = new Thread(this); 114 115 synchronized (startLock) { 116 try { 117 // Start here in case server thread runs faster than main thread. 118 // See https://sourceforge.net/tracker/?func=detail&atid=1006533&aid=1925590&group_id=208647 119 serverThread.start(); 120 121 // Wait until the server thread is initialized 122 startLock.wait(); 123 } 124 catch (InterruptedException e) { 125 e.printStackTrace(); 126 throw new MockFtpServerException(e); 127 } 128 } 129 } 130 131 /** 132 * The logic for the server thread 133 * 134 * @see Runnable#run() 135 */ 136 public void run() { 137 try { 138 LOG.info("Starting the server on port " + serverControlPort); 139 serverSocket = serverSocketFactory.createServerSocket(serverControlPort); 140 if (serverControlPort == 0) { 141 this.serverControlPort = serverSocket.getLocalPort(); 142 LOG.info("Actual server port is " + this.serverControlPort); 143 } 144 145 // Notify to allow the start() method to finish and return 146 synchronized (startLock) { 147 startLock.notify(); 148 } 149 150 while (!terminate) { 151 try { 152 Socket clientSocket = serverSocket.accept(); 153 LOG.info("Connection accepted from host " + clientSocket.getInetAddress()); 154 155 Session session = createSession(clientSocket); 156 Thread sessionThread = new Thread(session); 157 sessionThread.start(); 158 159 SessionInfo sessionInfo = new SessionInfo(); 160 sessionInfo.socket = clientSocket; 161 sessionInfo.thread = sessionThread; 162 sessions.put(session, sessionInfo); 163 } 164 catch (SocketException e) { 165 LOG.trace("Socket exception: " + e.toString()); 166 } 167 } 168 } 169 catch (IOException e) { 170 LOG.error("Error", e); 171 } 172 finally { 173 174 LOG.debug("Cleaning up server..."); 175 176 // Ensure that the start() method is not still blocked 177 synchronized (startLock) { 178 startLock.notifyAll(); 179 } 180 181 try { 182 if (serverSocket != null) { 183 serverSocket.close(); 184 } 185 closeSessions(); 186 } 187 catch (IOException e) { 188 LOG.error("Error cleaning up server", e); 189 } 190 catch (InterruptedException e) { 191 LOG.error("Error cleaning up server", e); 192 } 193 LOG.info("Server stopped."); 194 terminate = false; 195 } 196 } 197 198 /** 199 * Stop this server instance and wait for it to terminate. 200 */ 201 public void stop() { 202 203 LOG.trace("Stopping the server..."); 204 terminate = true; 205 206 if (serverSocket != null) { 207 try { 208 serverSocket.close(); 209 } catch (IOException e) { 210 throw new MockFtpServerException(e); 211 } 212 } 213 214 try { 215 if (serverThread != null) { 216 serverThread.join(); 217 } 218 } 219 catch (InterruptedException e) { 220 e.printStackTrace(); 221 throw new MockFtpServerException(e); 222 } 223 } 224 225 /** 226 * Return the CommandHandler defined for the specified command name 227 * 228 * @param name - the command name 229 * @return the CommandHandler defined for name 230 */ 231 public CommandHandler getCommandHandler(String name) { 232 return (CommandHandler) commandHandlers.get(Command.normalizeName(name)); 233 } 234 235 /** 236 * Override the default CommandHandlers with those in the specified Map of 237 * commandName>>CommandHandler. This will only override the default CommandHandlers 238 * for the keys in <code>commandHandlerMapping</code>. All other default CommandHandler 239 * mappings remain unchanged. 240 * 241 * @param commandHandlerMapping - the Map of commandName->CommandHandler; these override the defaults 242 * @throws org.mockftpserver.core.util.AssertFailedException 243 * - if the commandHandlerMapping is null 244 */ 245 public void setCommandHandlers(Map commandHandlerMapping) { 246 Assert.notNull(commandHandlerMapping, "commandHandlers"); 247 for (Iterator iter = commandHandlerMapping.keySet().iterator(); iter.hasNext();) { 248 String commandName = (String) iter.next(); 249 setCommandHandler(commandName, (CommandHandler) commandHandlerMapping.get(commandName)); 250 } 251 } 252 253 /** 254 * Set the CommandHandler for the specified command name. If the CommandHandler implements 255 * the {@link org.mockftpserver.core.command.ReplyTextBundleAware} interface and its <code>replyTextBundle</code> attribute 256 * is null, then set its <code>replyTextBundle</code> to the <code>replyTextBundle</code> of 257 * this StubFtpServer. 258 * 259 * @param commandName - the command name to which the CommandHandler will be associated 260 * @param commandHandler - the CommandHandler 261 * @throws org.mockftpserver.core.util.AssertFailedException 262 * - if the commandName or commandHandler is null 263 */ 264 public void setCommandHandler(String commandName, CommandHandler commandHandler) { 265 Assert.notNull(commandName, "commandName"); 266 Assert.notNull(commandHandler, "commandHandler"); 267 commandHandlers.put(Command.normalizeName(commandName), commandHandler); 268 initializeCommandHandler(commandHandler); 269 } 270 271 /** 272 * Set the reply text ResourceBundle to a new ResourceBundle with the specified base name, 273 * accessible on the CLASSPATH. See {@link java.util.ResourceBundle#getBundle(String)}. 274 * 275 * @param baseName - the base name of the resource bundle, a fully qualified class name 276 */ 277 public void setReplyTextBaseName(String baseName) { 278 replyTextBundle = ResourceBundle.getBundle(baseName); 279 } 280 281 /** 282 * Return the ReplyText ResourceBundle. Set the bundle through the {@link #setReplyTextBaseName(String)} method. 283 * 284 * @return the reply text ResourceBundle 285 */ 286 public ResourceBundle getReplyTextBundle() { 287 return replyTextBundle; 288 } 289 290 /** 291 * Set the port number to which the server control connection socket will bind. The default value is 21. 292 * 293 * @param serverControlPort - the port number for the server control connection ServerSocket 294 */ 295 public void setServerControlPort(int serverControlPort) { 296 this.serverControlPort = serverControlPort; 297 } 298 299 /** 300 * Return the port number to which the server control connection socket will bind. The default value is 21. 301 * 302 * @return the port number for the server control connection ServerSocket 303 */ 304 public int getServerControlPort() { 305 return serverControlPort; 306 } 307 308 /** 309 * Return true if this server is fully shutdown -- i.e., there is no active (alive) threads and 310 * all sockets are closed. This method is intended for testing only. 311 * 312 * @return true if this server is fully shutdown 313 */ 314 public boolean isShutdown() { 315 boolean shutdown = !serverThread.isAlive() && serverSocket.isClosed(); 316 317 for (Iterator iter = sessions.values().iterator(); iter.hasNext();) { 318 SessionInfo sessionInfo = (SessionInfo) iter.next(); 319 shutdown = shutdown && sessionInfo.socket.isClosed() && !sessionInfo.thread.isAlive(); 320 } 321 return shutdown; 322 } 323 324 /** 325 * Return true if this server has started -- i.e., there is an active (alive) server threads 326 * and non-null server socket. This method is intended for testing only. 327 * 328 * @return true if this server has started 329 */ 330 public boolean isStarted() { 331 return serverThread != null && serverThread.isAlive() && serverSocket != null; 332 } 333 334 //------------------------------------------------------------------------- 335 // Internal Helper Methods 336 //------------------------------------------------------------------------- 337 338 /** 339 * Create a new Session instance for the specified client Socket 340 * 341 * @param clientSocket - the Socket associated with the client 342 * @return a Session 343 */ 344 protected Session createSession(Socket clientSocket) { 345 return new DefaultSession(clientSocket, commandHandlers); 346 } 347 348 private void closeSessions() throws InterruptedException, IOException { 349 for (Iterator iter = sessions.entrySet().iterator(); iter.hasNext();) { 350 Map.Entry entry = (Map.Entry) iter.next(); 351 Session session = (Session) entry.getKey(); 352 SessionInfo sessionInfo = (SessionInfo) entry.getValue(); 353 session.close(); 354 sessionInfo.thread.join(500L); 355 Socket sessionSocket = sessionInfo.socket; 356 if (sessionSocket != null) { 357 sessionSocket.close(); 358 } 359 } 360 } 361 362 //------------------------------------------------------------------------------------ 363 // Abstract method declarations 364 //------------------------------------------------------------------------------------ 365 366 /** 367 * Initialize a CommandHandler that has been registered to this server. What "initialization" 368 * means is dependent on the subclass implementation. 369 * 370 * @param commandHandler - the CommandHandler to initialize 371 */ 372 protected abstract void initializeCommandHandler(CommandHandler commandHandler); 373 374}