/*
* 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.command;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.mockftpserver.core.session.Session;
import org.mockftpserver.core.util.Assert;
import org.mockftpserver.core.util.AssertFailedException;
/**
* The abstract superclass for CommandHandler classes. This class manages the List of
* InvocationRecord objects corresponding to each invocation of the command handler,
* and provides helper methods for subclasses.
*
* @version $Revision$ - $Date$
*
* @author Chris Mair
*/
public abstract class AbstractCommandHandler implements CommandHandler, ReplyTextBundleAware, InvocationHistory {
private static final Logger LOG = Logger.getLogger(AbstractCommandHandler.class);
private ResourceBundle replyTextBundle;
private List invocations = new ArrayList();
// -------------------------------------------------------------------------
// Template Method
// -------------------------------------------------------------------------
/**
* Handle the specified command for the session. This method is declared to throw Exception,
* allowing CommandHandler implementations to avoid unnecessary exception-handling. All checked
* exceptions are expected to be wrapped and handled by the caller.
*
* @param command - the Command to be handled
* @param session - the session on which the Command was submitted
*
* @throws Exception
* @throws AssertFailedException - if the command or session is null
*
* @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command,
* org.mockftpserver.core.session.Session)
*/
public final void handleCommand(Command command, Session session) throws Exception {
Assert.notNull(command, "command");
Assert.notNull(session, "session");
InvocationRecord invocationRecord = new InvocationRecord(command, session.getClientHost());
invocations.add(invocationRecord);
handleCommand(command, session, invocationRecord);
invocationRecord.lock();
}
/**
* Handle the specified command for the session. This method is declared to throw Exception,
* allowing CommandHandler implementations to avoid unnecessary exception-handling. All checked
* exceptions are expected to be wrapped and handled by the caller.
*
* @param command - the Command to be handled
* @param session - the session on which the Command was submitted
* @param invocationRecord - the InvocationRecord; CommandHandlers are expected to add
* handler-specific data to the InvocationRecord, as appropriate
* @throws Exception
*/
protected abstract void handleCommand(Command command, Session session, InvocationRecord invocationRecord)
throws Exception;
//-------------------------------------------------------------------------
// Support for reply text ResourceBundle
//-------------------------------------------------------------------------
/**
* Return the ResourceBundle containing the reply text messages
* @return the replyTextBundle
*
* @see org.mockftpserver.core.command.ReplyTextBundleAware#getReplyTextBundle()
*/
public ResourceBundle getReplyTextBundle() {
return replyTextBundle;
}
/**
* Set the ResourceBundle containing the reply text messages
* @param replyTextBundle - the replyTextBundle to set
*
* @see org.mockftpserver.core.command.ReplyTextBundleAware#setReplyTextBundle(java.util.ResourceBundle)
*/
public void setReplyTextBundle(ResourceBundle replyTextBundle) {
this.replyTextBundle = replyTextBundle;
}
// -------------------------------------------------------------------------
// Utility methods for subclasses
// -------------------------------------------------------------------------
/**
* Send a reply for this command on the control connection.
*
* The reply code is designated by the replyCode
property, and the reply text
* is determined by the following rules:
*
replyText
property is non-null, then use that.replyMessageKey
is non-null, the use that to retrieve a
* localized message from the replyText
ResourceBundle.replyText
ResourceBundle,
* using the reply code as the key.numberOfInvocations()
method will return zero.
*
* @see org.mockftpserver.core.command.InvocationHistory#clearInvocations()
*/
public void clearInvocations() {
invocations.clear();
}
// -------------------------------------------------------------------------
// Internal Helper Methods
// -------------------------------------------------------------------------
/**
* Return the text for the specified reply code, formatted using the message arguments, if
* supplied. If overrideText is not null, then return that. Otherwise, return the text mapped to
* the code from the replyText ResourceBundle. If the ResourceBundle contains no mapping, then
* return null.
* * If arguments is not null, then the returned reply text if formatted using the * {@link MessageFormat} class. * * @param code - the reply code * @param messageKey - the key used to retrieve the reply text from the replyTextBundle * @param overrideText - if not null, this is used instead of the text from the replyTextBundle. * @param arguments - the array of arguments to be formatted and substituted within the reply * text; may be null * @return the text for the reply code; may be null */ private String getTextForReplyCode(int code, String messageKey, String overrideText, Object[] arguments) { try { String t = (overrideText == null) ? replyTextBundle.getString(messageKey) : overrideText; String formattedMessage = MessageFormat.format(t, arguments); return (formattedMessage == null) ? null : formattedMessage.trim(); } catch (MissingResourceException e) { // No reply text is mapped for the specified key LOG.warn("No reply text defined for reply code [" + code + "]"); return null; } } }