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.command;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.util.Assert;
20
21import java.util.ResourceBundle;
22
23/**
24 * The abstract superclass for CommandHandler classes.
25 *
26 * @author Chris Mair
27 * @version $Revision$ - $Date$
28 */
29public abstract class AbstractCommandHandler implements CommandHandler, ReplyTextBundleAware {
30
31    protected final Logger LOG = Logger.getLogger(getClass());
32
33    private ResourceBundle replyTextBundle;
34
35    //-------------------------------------------------------------------------
36    // Support for reply text ResourceBundle
37    //-------------------------------------------------------------------------
38
39    /**
40     * Return the ResourceBundle containing the reply text messages
41     *
42     * @return the replyTextBundle
43     * @see ReplyTextBundleAware#getReplyTextBundle()
44     */
45    public ResourceBundle getReplyTextBundle() {
46        return replyTextBundle;
47    }
48
49    /**
50     * Set the ResourceBundle containing the reply text messages
51     *
52     * @param replyTextBundle - the replyTextBundle to set
53     * @see ReplyTextBundleAware#setReplyTextBundle(java.util.ResourceBundle)
54     */
55    public void setReplyTextBundle(ResourceBundle replyTextBundle) {
56        this.replyTextBundle = replyTextBundle;
57    }
58
59    // -------------------------------------------------------------------------
60    // Utility methods for subclasses
61    // -------------------------------------------------------------------------
62
63    /**
64     * Return the specified text surrounded with double quotes
65     *
66     * @param text - the text to surround with quotes
67     * @return the text with leading and trailing double quotes
68     * @throws org.mockftpserver.core.util.AssertFailedException
69     *          - if text is null
70     */
71    protected static String quotes(String text) {
72        Assert.notNull(text, "text");
73        final String QUOTES = "\"";
74        return QUOTES + text + QUOTES;
75    }
76
77    /**
78     * Assert that the specified number is a valid reply code
79     *
80     * @param replyCode - the reply code to check
81     * @throws org.mockftpserver.core.util.AssertFailedException
82     *          - if the replyCode is invalid
83     */
84    protected void assertValidReplyCode(int replyCode) {
85        Assert.isTrue(replyCode > 0, "The number [" + replyCode + "] is not a valid reply code");
86    }
87
88}