10bb42783195c864c3966bd122d2458b0d975619echrismair/*
20bb42783195c864c3966bd122d2458b0d975619echrismair * Copyright 2008 the original author or authors.
30bb42783195c864c3966bd122d2458b0d975619echrismair *
40bb42783195c864c3966bd122d2458b0d975619echrismair * Licensed under the Apache License, Version 2.0 (the "License");
50bb42783195c864c3966bd122d2458b0d975619echrismair * you may not use this file except in compliance with the License.
60bb42783195c864c3966bd122d2458b0d975619echrismair * You may obtain a copy of the License at
70bb42783195c864c3966bd122d2458b0d975619echrismair *
80bb42783195c864c3966bd122d2458b0d975619echrismair *      http://www.apache.org/licenses/LICENSE-2.0
90bb42783195c864c3966bd122d2458b0d975619echrismair *
100bb42783195c864c3966bd122d2458b0d975619echrismair * Unless required by applicable law or agreed to in writing, software
110bb42783195c864c3966bd122d2458b0d975619echrismair * distributed under the License is distributed on an "AS IS" BASIS,
120bb42783195c864c3966bd122d2458b0d975619echrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130bb42783195c864c3966bd122d2458b0d975619echrismair * See the License for the specific language governing permissions and
140bb42783195c864c3966bd122d2458b0d975619echrismair * limitations under the License.
150bb42783195c864c3966bd122d2458b0d975619echrismair */
16bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5chrismairpackage org.mockftpserver.fake;
170bb42783195c864c3966bd122d2458b0d975619echrismair
180bb42783195c864c3966bd122d2458b0d975619echrismairimport org.mockftpserver.core.command.CommandHandler;
190bb42783195c864c3966bd122d2458b0d975619echrismairimport org.mockftpserver.core.command.CommandNames;
20ff3c0fa2e5603e7b567533cc6369b7c258650299chrismairimport org.mockftpserver.core.command.ConnectCommandHandler;
210da0f84ad9831bea470896e79b5ec207c83f258echrismairimport org.mockftpserver.core.command.ReplyTextBundleUtil;
2249deb463d1cc3132e4aa60bfd4469398c57c1745chrismairimport org.mockftpserver.core.command.UnsupportedCommandHandler;
230bb42783195c864c3966bd122d2458b0d975619echrismairimport org.mockftpserver.core.server.AbstractFtpServer;
240bb42783195c864c3966bd122d2458b0d975619echrismairimport org.mockftpserver.fake.command.*;
250bb42783195c864c3966bd122d2458b0d975619echrismairimport org.mockftpserver.fake.filesystem.FileSystem;
260bb42783195c864c3966bd122d2458b0d975619echrismair
270bb42783195c864c3966bd122d2458b0d975619echrismairimport java.util.HashMap;
280bb42783195c864c3966bd122d2458b0d975619echrismairimport java.util.List;
290bb42783195c864c3966bd122d2458b0d975619echrismairimport java.util.Map;
300bb42783195c864c3966bd122d2458b0d975619echrismair
310bb42783195c864c3966bd122d2458b0d975619echrismair/**
320bb42783195c864c3966bd122d2458b0d975619echrismair * <b>FakeFtpServer</b> is the top-level class for a "fake" implementation of an FTP Server,
330bb42783195c864c3966bd122d2458b0d975619echrismair * suitable for testing FTP client code or standing in for a live FTP server.
340bb42783195c864c3966bd122d2458b0d975619echrismair * <p/>
350bb42783195c864c3966bd122d2458b0d975619echrismair * <b>FakeFtpServer</b> provides a high-level abstraction for an FTP Server and is suitable
360bb42783195c864c3966bd122d2458b0d975619echrismair * for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing
370bb42783195c864c3966bd122d2458b0d975619echrismair * an arbitrary set of files and directories. These files and directories can (optionally) have
380bb42783195c864c3966bd122d2458b0d975619echrismair * associated access permissions. You also configure a set of one or more user accounts that
390bb42783195c864c3966bd122d2458b0d975619echrismair * control which users can login to the FTP server, and their home (default) directories. The
400bb42783195c864c3966bd122d2458b0d975619echrismair * user account is also used when assigning file and directory ownership for new files.
410bb42783195c864c3966bd122d2458b0d975619echrismair * <p> <b>FakeFtpServer</b> processes FTP client requests and responds with reply codes and
420bb42783195c864c3966bd122d2458b0d975619echrismair * reply messages consistent with its configuration and the contents of its internal filesystem,
430bb42783195c864c3966bd122d2458b0d975619echrismair * including file and directory permissions, if they have been configured.
440bb42783195c864c3966bd122d2458b0d975619echrismair * <p/>
450bb42783195c864c3966bd122d2458b0d975619echrismair * <b>FakeFtpServer</b> can be fully configured programmatically or within the
46c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <a href="http://www.springframework.org/">Spring Framework</a> or other dependency-injection container.
470bb42783195c864c3966bd122d2458b0d975619echrismair * <p/>
480bb42783195c864c3966bd122d2458b0d975619echrismair * In general the steps for setting up and starting the <b>FakeFtpServer</b> are:
490bb42783195c864c3966bd122d2458b0d975619echrismair * <ol>
500bb42783195c864c3966bd122d2458b0d975619echrismair * <li>Create a new <b>FakeFtpServer</b> instance, and optionally set the server control port.</li>
510bb42783195c864c3966bd122d2458b0d975619echrismair * <li>Create and configure a <b>FileSystem</b>, and attach to the <b>FakeFtpServer</b> instance.</li>
520bb42783195c864c3966bd122d2458b0d975619echrismair * <li>Create and configure one or more <b>UserAccount</b> objects and attach to the <b>FakeFtpServer</b> instance.</li>
533736b2d78b2d799ad9318e5295e2204c21e21a44chrismair * <li>Start the <b>FakeFtpServer</b> instance.</li>
540bb42783195c864c3966bd122d2458b0d975619echrismair * </ol>
550bb42783195c864c3966bd122d2458b0d975619echrismair * <h4>Example Code</h4>
560bb42783195c864c3966bd122d2458b0d975619echrismair * <pre><code>
57c561a9b3509b5021ddb2e2f8165e903909281269chrismair * FakeFtpServer fakeFtpServer = new FakeFtpServer();
58e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
590bb42783195c864c3966bd122d2458b0d975619echrismair * FileSystem fileSystem = new WindowsFakeFileSystem();
60c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fileSystem.add(new DirectoryEntry("c:\\"));
61c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fileSystem.add(new DirectoryEntry("c:\\data"));
62c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890"));
63c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fileSystem.add(new FileEntry("c:\\data\\run.exe"));
64c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fakeFtpServer.setFileSystem(fileSystem);
65e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
66c561a9b3509b5021ddb2e2f8165e903909281269chrismair * // Create UserAccount with username, password, home-directory
67c561a9b3509b5021ddb2e2f8165e903909281269chrismair * UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\");
68c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fakeFtpServer.addUserAccounts(userAccount);
69e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
70c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fakeFtpServer.start();
71c561a9b3509b5021ddb2e2f8165e903909281269chrismair * </code></pre>
72e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
73c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <h4>Example Code with Permissions</h4>
74c561a9b3509b5021ddb2e2f8165e903909281269chrismair * You can optionally set the permissions and owner/group for each file and directory, as in the following example.
75c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <pre><code>
76c561a9b3509b5021ddb2e2f8165e903909281269chrismair * FileSystem fileSystem = new UnixFakeFileSystem();
77c561a9b3509b5021ddb2e2f8165e903909281269chrismair * DirectoryEntry directoryEntry1 = new DirectoryEntry("/");
780bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
790bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry1.setOwner("joe");
800bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry1.setGroup("dev");
81e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
82c561a9b3509b5021ddb2e2f8165e903909281269chrismair * DirectoryEntry directoryEntry2 = new DirectoryEntry("/data");
830bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry2.setPermissions(Permissions.ALL);
840bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry2.setOwner("joe");
850bb42783195c864c3966bd122d2458b0d975619echrismair * directoryEntry2.setGroup("dev");
86e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
87c561a9b3509b5021ddb2e2f8165e903909281269chrismair * FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890");
880bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry1.setPermissionsFromString("rw-rw-rw-");
890bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry1.setOwner("joe");
900bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry1.setGroup("dev");
91e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
92c561a9b3509b5021ddb2e2f8165e903909281269chrismair * FileEntry fileEntry2 = new FileEntry("/data/run.exe");
930bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry2.setPermissionsFromString("rwxrwx---");
940bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry2.setOwner("mary");
950bb42783195c864c3966bd122d2458b0d975619echrismair * fileEntry2.setGroup("dev");
96e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
970bb42783195c864c3966bd122d2458b0d975619echrismair * fileSystem.add(directoryEntry1);
980bb42783195c864c3966bd122d2458b0d975619echrismair * fileSystem.add(directoryEntry2);
990bb42783195c864c3966bd122d2458b0d975619echrismair * fileSystem.add(fileEntry1);
1000bb42783195c864c3966bd122d2458b0d975619echrismair * fileSystem.add(fileEntry2);
101e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
1020bb42783195c864c3966bd122d2458b0d975619echrismair * FakeFtpServer fakeFtpServer = new FakeFtpServer();
1030bb42783195c864c3966bd122d2458b0d975619echrismair * fakeFtpServer.setFileSystem(fileSystem);
104e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
105c561a9b3509b5021ddb2e2f8165e903909281269chrismair * // Create UserAccount with username, password, home-directory
106c561a9b3509b5021ddb2e2f8165e903909281269chrismair * UserAccount userAccount = new UserAccount("joe", "joe123", "/");
107c561a9b3509b5021ddb2e2f8165e903909281269chrismair * fakeFtpServer.addUserAccounts(userAccount);
108e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
1090bb42783195c864c3966bd122d2458b0d975619echrismair * fakeFtpServer.start();
1100bb42783195c864c3966bd122d2458b0d975619echrismair * </code></pre>
111e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
112c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <h4>FTP Server Control Port</h4>
1132cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * By default, <b>FakeFtpServer</b> binds to the server control port of 21. You can use a different server control
1142cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * port by setting the <code>serverControlPort</code> property. If you specify a value of <code>0</code>,
1152cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * then a free port number will be chosen automatically; call <code>getServerControlPort()</code> AFTER
1162cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * <code>start()</code> has been called to determine the actual port number being used. Using a non-default
1172cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * port number is usually necessary when running on Unix or some other system where that port number is
1182cfd46ef00809da42b315de79e7a457ceb40f70bchrismair * already in use or cannot be bound from a user process.
119e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
120c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <h4>Other Configuration</h4>
121c561a9b3509b5021ddb2e2f8165e903909281269chrismair * The <code>systemName</code> property specifies the value returned by the <code>SYST</code>
122c561a9b3509b5021ddb2e2f8165e903909281269chrismair * command. Note that this is typically used by an FTP client to determine how to parse
123c561a9b3509b5021ddb2e2f8165e903909281269chrismair * system-dependent reply text, such as directory listings. This value defaults to <code>"WINDOWS"</code>.
124c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <p/>
125c561a9b3509b5021ddb2e2f8165e903909281269chrismair * The <code>helpText</code> property specifies a <i>Map</i> of help text replies sent by the
126c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <code>HELP</code> command. The keys in that <i>Map</i> correspond to the command names passed as
127c561a9b3509b5021ddb2e2f8165e903909281269chrismair * parameters to the <code>HELP</code> command. An entry with the key of an empty string ("") indicates the
128c561a9b3509b5021ddb2e2f8165e903909281269chrismair * text used as the default help text when no command name parameter is specified for the <code>HELP</code> command.
129e69f7494dfd77c9a9c617afba28fd490930c1605chrismair *
130c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <h4>FTP Command Reply Text ResourceBundle</h4>
1310bb42783195c864c3966bd122d2458b0d975619echrismair * The default text asociated with each FTP command reply code is contained within the
1320bb42783195c864c3966bd122d2458b0d975619echrismair * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
1330bb42783195c864c3966bd122d2458b0d975619echrismair * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
1340bb42783195c864c3966bd122d2458b0d975619echrismair * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
1350bb42783195c864c3966bd122d2458b0d975619echrismair * completely replace the ResourceBundle file by calling the calling the
1360bb42783195c864c3966bd122d2458b0d975619echrismair * {@link #setReplyTextBaseName(String)} method.
1370bb42783195c864c3966bd122d2458b0d975619echrismair *
1380bb42783195c864c3966bd122d2458b0d975619echrismair * @author Chris Mair
1392a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$
1400bb42783195c864c3966bd122d2458b0d975619echrismair */
1410bb42783195c864c3966bd122d2458b0d975619echrismairpublic class FakeFtpServer extends AbstractFtpServer implements ServerConfiguration {
1420bb42783195c864c3966bd122d2458b0d975619echrismair
1430bb42783195c864c3966bd122d2458b0d975619echrismair    private FileSystem fileSystem;
1440bb42783195c864c3966bd122d2458b0d975619echrismair    private String systemName = "WINDOWS";
1453fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    private String systemStatus = "Connected";
1460bb42783195c864c3966bd122d2458b0d975619echrismair    private Map helpText = new HashMap();
1470bb42783195c864c3966bd122d2458b0d975619echrismair    private Map userAccounts = new HashMap();
1480bb42783195c864c3966bd122d2458b0d975619echrismair
1490bb42783195c864c3966bd122d2458b0d975619echrismair    public FileSystem getFileSystem() {
1500bb42783195c864c3966bd122d2458b0d975619echrismair        return fileSystem;
1510bb42783195c864c3966bd122d2458b0d975619echrismair    }
1520bb42783195c864c3966bd122d2458b0d975619echrismair
1530bb42783195c864c3966bd122d2458b0d975619echrismair    public void setFileSystem(FileSystem fileSystem) {
1540bb42783195c864c3966bd122d2458b0d975619echrismair        this.fileSystem = fileSystem;
1550bb42783195c864c3966bd122d2458b0d975619echrismair    }
1560bb42783195c864c3966bd122d2458b0d975619echrismair
1570bb42783195c864c3966bd122d2458b0d975619echrismair    public String getSystemName() {
1580bb42783195c864c3966bd122d2458b0d975619echrismair        return systemName;
1590bb42783195c864c3966bd122d2458b0d975619echrismair    }
1600bb42783195c864c3966bd122d2458b0d975619echrismair
1610bb42783195c864c3966bd122d2458b0d975619echrismair    public void setSystemName(String systemName) {
1620bb42783195c864c3966bd122d2458b0d975619echrismair        this.systemName = systemName;
1630bb42783195c864c3966bd122d2458b0d975619echrismair    }
1640bb42783195c864c3966bd122d2458b0d975619echrismair
1650bb42783195c864c3966bd122d2458b0d975619echrismair    public Map getHelpText() {
1660bb42783195c864c3966bd122d2458b0d975619echrismair        return helpText;
1670bb42783195c864c3966bd122d2458b0d975619echrismair    }
1680bb42783195c864c3966bd122d2458b0d975619echrismair
1690bb42783195c864c3966bd122d2458b0d975619echrismair    public void setHelpText(Map helpText) {
1700bb42783195c864c3966bd122d2458b0d975619echrismair        this.helpText = helpText;
1710bb42783195c864c3966bd122d2458b0d975619echrismair    }
1720bb42783195c864c3966bd122d2458b0d975619echrismair
1730bb42783195c864c3966bd122d2458b0d975619echrismair    public FakeFtpServer() {
1740bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.ACCT, new AcctCommandHandler());
1750bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.ABOR, new AborCommandHandler());
1760bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.ALLO, new AlloCommandHandler());
1770bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.APPE, new AppeCommandHandler());
1780bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.CWD, new CwdCommandHandler());
1790bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.CDUP, new CdupCommandHandler());
1800bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.DELE, new DeleCommandHandler());
18175520503341f870a8d2a27e0a82cca14f8aa6a4bchrismair        setCommandHandler(CommandNames.EPRT, new EprtCommandHandler());
18275520503341f870a8d2a27e0a82cca14f8aa6a4bchrismair        setCommandHandler(CommandNames.EPSV, new EpsvCommandHandler());
1830bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.HELP, new HelpCommandHandler());
1840bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.LIST, new ListCommandHandler());
1850bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.MKD, new MkdCommandHandler());
1860bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.MODE, new ModeCommandHandler());
1870bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.NLST, new NlstCommandHandler());
1880bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.NOOP, new NoopCommandHandler());
1890bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.PASS, new PassCommandHandler());
1900bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.PASV, new PasvCommandHandler());
1910bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.PWD, new PwdCommandHandler());
1920bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.PORT, new PortCommandHandler());
1930bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.QUIT, new QuitCommandHandler());
1940bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.REIN, new ReinCommandHandler());
1950bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.REST, new RestCommandHandler());
1960bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.RETR, new RetrCommandHandler());
1970bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.RMD, new RmdCommandHandler());
1980bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler());
1990bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.RNTO, new RntoCommandHandler());
2000bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.SITE, new SiteCommandHandler());
2011901d3e08564ec02e4e45345f95c1b1bdca2e3c0chrismair        setCommandHandler(CommandNames.SMNT, new SmntCommandHandler());
2023fc03edff55387bae91315653eae3ed0ca9b1f2achrismair        setCommandHandler(CommandNames.STAT, new StatCommandHandler());
2030bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.STOR, new StorCommandHandler());
2040bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.STOU, new StouCommandHandler());
2050bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.STRU, new StruCommandHandler());
2060bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.SYST, new SystCommandHandler());
2070bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.TYPE, new TypeCommandHandler());
2080bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.USER, new UserCommandHandler());
2090bb42783195c864c3966bd122d2458b0d975619echrismair        setCommandHandler(CommandNames.XPWD, new PwdCommandHandler());
21049deb463d1cc3132e4aa60bfd4469398c57c1745chrismair
21149deb463d1cc3132e4aa60bfd4469398c57c1745chrismair        // "Special" Command Handlers
21249deb463d1cc3132e4aa60bfd4469398c57c1745chrismair        setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler());
21349deb463d1cc3132e4aa60bfd4469398c57c1745chrismair        setCommandHandler(CommandNames.UNSUPPORTED, new UnsupportedCommandHandler());
2140bb42783195c864c3966bd122d2458b0d975619echrismair    }
2150bb42783195c864c3966bd122d2458b0d975619echrismair
2160bb42783195c864c3966bd122d2458b0d975619echrismair    /**
2173736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     * Initialize a CommandHandler that has been registered to this server.
2183736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     *
2193736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     * If the CommandHandler implements the <code>ServerConfigurationAware</code> interface, then set its
2203736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     * <code>ServerConfiguration</code> property to <code>this</code>.
2213736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     *
2223736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     * If the CommandHandler implements the <code>ReplyTextBundleAware</code> interface, then set its
2233736b2d78b2d799ad9318e5295e2204c21e21a44chrismair     * <code>replyTextBundle</code> property using the reply text bundle for this server.
2240bb42783195c864c3966bd122d2458b0d975619echrismair     *
2250bb42783195c864c3966bd122d2458b0d975619echrismair     * @param commandHandler - the CommandHandler to initialize
2260bb42783195c864c3966bd122d2458b0d975619echrismair     */
2270bb42783195c864c3966bd122d2458b0d975619echrismair    protected void initializeCommandHandler(CommandHandler commandHandler) {
2280bb42783195c864c3966bd122d2458b0d975619echrismair        if (commandHandler instanceof ServerConfigurationAware) {
2290bb42783195c864c3966bd122d2458b0d975619echrismair            ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler;
2300bb42783195c864c3966bd122d2458b0d975619echrismair            sca.setServerConfiguration(this);
2310bb42783195c864c3966bd122d2458b0d975619echrismair        }
2320da0f84ad9831bea470896e79b5ec207c83f258echrismair
2330da0f84ad9831bea470896e79b5ec207c83f258echrismair        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle());
2340bb42783195c864c3966bd122d2458b0d975619echrismair    }
2350bb42783195c864c3966bd122d2458b0d975619echrismair
2360bb42783195c864c3966bd122d2458b0d975619echrismair    /**
2370bb42783195c864c3966bd122d2458b0d975619echrismair     * @return the {@link UserAccount}        configured for this server for the specified user name
2380bb42783195c864c3966bd122d2458b0d975619echrismair     */
2390bb42783195c864c3966bd122d2458b0d975619echrismair    public UserAccount getUserAccount(String username) {
2400bb42783195c864c3966bd122d2458b0d975619echrismair        return (UserAccount) userAccounts.get(username);
2410bb42783195c864c3966bd122d2458b0d975619echrismair    }
2420bb42783195c864c3966bd122d2458b0d975619echrismair
2430bb42783195c864c3966bd122d2458b0d975619echrismair    /**
2440bb42783195c864c3966bd122d2458b0d975619echrismair     * Return the help text for a command or the default help text if no command name is specified
2450bb42783195c864c3966bd122d2458b0d975619echrismair     *
2460bb42783195c864c3966bd122d2458b0d975619echrismair     * @param name - the command name; may be empty or null to indicate  a request for the default help text
2470bb42783195c864c3966bd122d2458b0d975619echrismair     * @return the help text for the named command or the default help text if no name is supplied
2480bb42783195c864c3966bd122d2458b0d975619echrismair     */
2490bb42783195c864c3966bd122d2458b0d975619echrismair    public String getHelpText(String name) {
2500bb42783195c864c3966bd122d2458b0d975619echrismair        String key = name == null ? "" : name;
2510bb42783195c864c3966bd122d2458b0d975619echrismair        return (String) helpText.get(key);
2520bb42783195c864c3966bd122d2458b0d975619echrismair    }
2530bb42783195c864c3966bd122d2458b0d975619echrismair
2540bb42783195c864c3966bd122d2458b0d975619echrismair    /**
2557bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair     * Add a single UserAccount. If an account with the same <code>username</code> already exists,
2567bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair     * it will be replaced.
2577bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair     *
2587bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair     * @param userAccount - the UserAccount to add
2597bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair     */
2607bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair    public void addUserAccount(UserAccount userAccount) {
2617bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair        userAccounts.put(userAccount.getUsername(), userAccount);
2627bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair    }
2637bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair
2647bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair    /**
2650bb42783195c864c3966bd122d2458b0d975619echrismair     * Add the UserAccount objects in the <code>userAccountList</code> to the set of UserAccounts.
2660bb42783195c864c3966bd122d2458b0d975619echrismair     *
2670bb42783195c864c3966bd122d2458b0d975619echrismair     * @param userAccountList - the List of UserAccount objects to add
2680bb42783195c864c3966bd122d2458b0d975619echrismair     */
2690bb42783195c864c3966bd122d2458b0d975619echrismair    public void setUserAccounts(List userAccountList) {
2700bb42783195c864c3966bd122d2458b0d975619echrismair        for (int i = 0; i < userAccountList.size(); i++) {
2710bb42783195c864c3966bd122d2458b0d975619echrismair            UserAccount userAccount = (UserAccount) userAccountList.get(i);
2720bb42783195c864c3966bd122d2458b0d975619echrismair            userAccounts.put(userAccount.getUsername(), userAccount);
2730bb42783195c864c3966bd122d2458b0d975619echrismair        }
2740bb42783195c864c3966bd122d2458b0d975619echrismair    }
2750bb42783195c864c3966bd122d2458b0d975619echrismair
2763fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    /**
2773fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     * Return the system status description
2783fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     *
2793fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     * @return the system status
2803fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     */
2813fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    public String getSystemStatus() {
2823fc03edff55387bae91315653eae3ed0ca9b1f2achrismair        return systemStatus;
2833fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    }
2843fc03edff55387bae91315653eae3ed0ca9b1f2achrismair
2853fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    /**
2863fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     * Set the system status description text, used by the STAT command handler.
2873fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     *
2883fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     * @param systemStatus - the system status description text
2893fc03edff55387bae91315653eae3ed0ca9b1f2achrismair     */
2903fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    public void setSystemStatus(String systemStatus) {
2913fc03edff55387bae91315653eae3ed0ca9b1f2achrismair        this.systemStatus = systemStatus;
2923fc03edff55387bae91315653eae3ed0ca9b1f2achrismair    }
2933fc03edff55387bae91315653eae3ed0ca9b1f2achrismair
2940bb42783195c864c3966bd122d2458b0d975619echrismair}