1dfb59d50631968ab1a13002ea5421ece93169851chrismair/*
2dfb59d50631968ab1a13002ea5421ece93169851chrismair * Copyright 2008 the original author or authors.
3dfb59d50631968ab1a13002ea5421ece93169851chrismair *
4dfb59d50631968ab1a13002ea5421ece93169851chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5dfb59d50631968ab1a13002ea5421ece93169851chrismair * you may not use this file except in compliance with the License.
6dfb59d50631968ab1a13002ea5421ece93169851chrismair * You may obtain a copy of the License at
7dfb59d50631968ab1a13002ea5421ece93169851chrismair *
8dfb59d50631968ab1a13002ea5421ece93169851chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9dfb59d50631968ab1a13002ea5421ece93169851chrismair *
10dfb59d50631968ab1a13002ea5421ece93169851chrismair * Unless required by applicable law or agreed to in writing, software
11dfb59d50631968ab1a13002ea5421ece93169851chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12dfb59d50631968ab1a13002ea5421ece93169851chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dfb59d50631968ab1a13002ea5421ece93169851chrismair * See the License for the specific language governing permissions and
14dfb59d50631968ab1a13002ea5421ece93169851chrismair * limitations under the License.
15dfb59d50631968ab1a13002ea5421ece93169851chrismair */
16dfb59d50631968ab1a13002ea5421ece93169851chrismairpackage org.mockftpserver.fake;
17dfb59d50631968ab1a13002ea5421ece93169851chrismair
18dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.command.CommandHandler;
19dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.command.CommandNames;
20dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.command.ConnectCommandHandler;
21dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.command.ReplyTextBundleUtil;
22dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.command.UnsupportedCommandHandler;
23dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.server.AbstractFtpServer;
24dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.fake.command.*;
25dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.fake.filesystem.FileSystem;
26dfb59d50631968ab1a13002ea5421ece93169851chrismair
27dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.util.HashMap;
28dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.util.List;
29dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.util.Map;
30dfb59d50631968ab1a13002ea5421ece93169851chrismair
31dfb59d50631968ab1a13002ea5421ece93169851chrismair/**
32dfb59d50631968ab1a13002ea5421ece93169851chrismair * <b>FakeFtpServer</b> is the top-level class for a "fake" implementation of an FTP Server,
33dfb59d50631968ab1a13002ea5421ece93169851chrismair * suitable for testing FTP client code or standing in for a live FTP server.
34dfb59d50631968ab1a13002ea5421ece93169851chrismair * <p/>
35dfb59d50631968ab1a13002ea5421ece93169851chrismair * <b>FakeFtpServer</b> provides a high-level abstraction for an FTP Server and is suitable
36dfb59d50631968ab1a13002ea5421ece93169851chrismair * for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing
37dfb59d50631968ab1a13002ea5421ece93169851chrismair * an arbitrary set of files and directories. These files and directories can (optionally) have
38dfb59d50631968ab1a13002ea5421ece93169851chrismair * associated access permissions. You also configure a set of one or more user accounts that
39dfb59d50631968ab1a13002ea5421ece93169851chrismair * control which users can login to the FTP server, and their home (default) directories. The
40dfb59d50631968ab1a13002ea5421ece93169851chrismair * user account is also used when assigning file and directory ownership for new files.
41dfb59d50631968ab1a13002ea5421ece93169851chrismair * <p> <b>FakeFtpServer</b> processes FTP client requests and responds with reply codes and
42dfb59d50631968ab1a13002ea5421ece93169851chrismair * reply messages consistent with its configuration and the contents of its internal filesystem,
43dfb59d50631968ab1a13002ea5421ece93169851chrismair * including file and directory permissions, if they have been configured.
44dfb59d50631968ab1a13002ea5421ece93169851chrismair * <p/>
45dfb59d50631968ab1a13002ea5421ece93169851chrismair * <b>FakeFtpServer</b> can be fully configured programmatically or within the
46dfb59d50631968ab1a13002ea5421ece93169851chrismair * <a href="http://www.springframework.org/">Spring Framework</a> or other dependency-injection container.
47dfb59d50631968ab1a13002ea5421ece93169851chrismair * <p/>
48dfb59d50631968ab1a13002ea5421ece93169851chrismair * In general the steps for setting up and starting the <b>FakeFtpServer</b> are:
49dfb59d50631968ab1a13002ea5421ece93169851chrismair * <ol>
50dfb59d50631968ab1a13002ea5421ece93169851chrismair * <li>Create a new <b>FakeFtpServer</b> instance, and optionally set the server control port.</li>
51dfb59d50631968ab1a13002ea5421ece93169851chrismair * <li>Create and configure a <b>FileSystem</b>, and attach to the <b>FakeFtpServer</b> instance.</li>
52dfb59d50631968ab1a13002ea5421ece93169851chrismair * <li>Create and configure one or more <b>UserAccount</b> objects and attach to the <b>FakeFtpServer</b> instance.</li>
53dfb59d50631968ab1a13002ea5421ece93169851chrismair * <li>Start the <b>FakeFtpServer</b> instance.</li>
54dfb59d50631968ab1a13002ea5421ece93169851chrismair * </ol>
55dfb59d50631968ab1a13002ea5421ece93169851chrismair * <h4>Example Code</h4>
56dfb59d50631968ab1a13002ea5421ece93169851chrismair * <pre><code>
57dfb59d50631968ab1a13002ea5421ece93169851chrismair * FakeFtpServer fakeFtpServer = new FakeFtpServer();
58dfb59d50631968ab1a13002ea5421ece93169851chrismair *
59dfb59d50631968ab1a13002ea5421ece93169851chrismair * FileSystem fileSystem = new WindowsFakeFileSystem();
60dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(new DirectoryEntry("c:\\"));
61dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(new DirectoryEntry("c:\\data"));
62dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890"));
63dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(new FileEntry("c:\\data\\run.exe"));
64dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.setFileSystem(fileSystem);
65dfb59d50631968ab1a13002ea5421ece93169851chrismair *
66dfb59d50631968ab1a13002ea5421ece93169851chrismair * // Create UserAccount with username, password, home-directory
67dfb59d50631968ab1a13002ea5421ece93169851chrismair * UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\");
68dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.addUserAccounts(userAccount);
69dfb59d50631968ab1a13002ea5421ece93169851chrismair *
70dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.start();
71dfb59d50631968ab1a13002ea5421ece93169851chrismair * </code></pre>
72dfb59d50631968ab1a13002ea5421ece93169851chrismair *
73dfb59d50631968ab1a13002ea5421ece93169851chrismair * <h4>Example Code with Permissions</h4>
74dfb59d50631968ab1a13002ea5421ece93169851chrismair * You can optionally set the permissions and owner/group for each file and directory, as in the following example.
75dfb59d50631968ab1a13002ea5421ece93169851chrismair * <pre><code>
76dfb59d50631968ab1a13002ea5421ece93169851chrismair * FileSystem fileSystem = new UnixFakeFileSystem();
77dfb59d50631968ab1a13002ea5421ece93169851chrismair * DirectoryEntry directoryEntry1 = new DirectoryEntry("/");
78dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
79dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry1.setOwner("joe");
80dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry1.setGroup("dev");
81dfb59d50631968ab1a13002ea5421ece93169851chrismair *
82dfb59d50631968ab1a13002ea5421ece93169851chrismair * DirectoryEntry directoryEntry2 = new DirectoryEntry("/data");
83dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry2.setPermissions(Permissions.ALL);
84dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry2.setOwner("joe");
85dfb59d50631968ab1a13002ea5421ece93169851chrismair * directoryEntry2.setGroup("dev");
86dfb59d50631968ab1a13002ea5421ece93169851chrismair *
87dfb59d50631968ab1a13002ea5421ece93169851chrismair * FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890");
88dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry1.setPermissionsFromString("rw-rw-rw-");
89dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry1.setOwner("joe");
90dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry1.setGroup("dev");
91dfb59d50631968ab1a13002ea5421ece93169851chrismair *
92dfb59d50631968ab1a13002ea5421ece93169851chrismair * FileEntry fileEntry2 = new FileEntry("/data/run.exe");
93dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry2.setPermissionsFromString("rwxrwx---");
94dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry2.setOwner("mary");
95dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileEntry2.setGroup("dev");
96dfb59d50631968ab1a13002ea5421ece93169851chrismair *
97dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(directoryEntry1);
98dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(directoryEntry2);
99dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(fileEntry1);
100dfb59d50631968ab1a13002ea5421ece93169851chrismair * fileSystem.add(fileEntry2);
101dfb59d50631968ab1a13002ea5421ece93169851chrismair *
102dfb59d50631968ab1a13002ea5421ece93169851chrismair * FakeFtpServer fakeFtpServer = new FakeFtpServer();
103dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.setFileSystem(fileSystem);
104dfb59d50631968ab1a13002ea5421ece93169851chrismair *
105dfb59d50631968ab1a13002ea5421ece93169851chrismair * // Create UserAccount with username, password, home-directory
106dfb59d50631968ab1a13002ea5421ece93169851chrismair * UserAccount userAccount = new UserAccount("joe", "joe123", "/");
107dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.addUserAccounts(userAccount);
108dfb59d50631968ab1a13002ea5421ece93169851chrismair *
109dfb59d50631968ab1a13002ea5421ece93169851chrismair * fakeFtpServer.start();
110dfb59d50631968ab1a13002ea5421ece93169851chrismair * </code></pre>
111dfb59d50631968ab1a13002ea5421ece93169851chrismair *
112dfb59d50631968ab1a13002ea5421ece93169851chrismair * <h4>FTP Server Control Port</h4>
113dfb59d50631968ab1a13002ea5421ece93169851chrismair * By default, <b>MockFtpServer</b> binds to the server control port of 21. You can use a different server
114dfb59d50631968ab1a13002ea5421ece93169851chrismair * control port by setting the the <code>serverControlPort</code> property. This is usually necessary
115dfb59d50631968ab1a13002ea5421ece93169851chrismair * when running on Unix or some other system where that port number is already in use or cannot be bound
116dfb59d50631968ab1a13002ea5421ece93169851chrismair * from a user process.
117dfb59d50631968ab1a13002ea5421ece93169851chrismair *
118dfb59d50631968ab1a13002ea5421ece93169851chrismair * <h4>Other Configuration</h4>
119dfb59d50631968ab1a13002ea5421ece93169851chrismair * The <code>systemName</code> property specifies the value returned by the <code>SYST</code>
120dfb59d50631968ab1a13002ea5421ece93169851chrismair * command. Note that this is typically used by an FTP client to determine how to parse
121dfb59d50631968ab1a13002ea5421ece93169851chrismair * system-dependent reply text, such as directory listings. This value defaults to <code>"WINDOWS"</code>.
122dfb59d50631968ab1a13002ea5421ece93169851chrismair * <p/>
123dfb59d50631968ab1a13002ea5421ece93169851chrismair * The <code>helpText</code> property specifies a <i>Map</i> of help text replies sent by the
124dfb59d50631968ab1a13002ea5421ece93169851chrismair * <code>HELP</code> command. The keys in that <i>Map</i> correspond to the command names passed as
125dfb59d50631968ab1a13002ea5421ece93169851chrismair * parameters to the <code>HELP</code> command. An entry with the key of an empty string ("") indicates the
126dfb59d50631968ab1a13002ea5421ece93169851chrismair * text used as the default help text when no command name parameter is specified for the <code>HELP</code> command.
127dfb59d50631968ab1a13002ea5421ece93169851chrismair *
128dfb59d50631968ab1a13002ea5421ece93169851chrismair * <h4>FTP Command Reply Text ResourceBundle</h4>
129dfb59d50631968ab1a13002ea5421ece93169851chrismair * The default text asociated with each FTP command reply code is contained within the
130dfb59d50631968ab1a13002ea5421ece93169851chrismair * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
131dfb59d50631968ab1a13002ea5421ece93169851chrismair * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
132dfb59d50631968ab1a13002ea5421ece93169851chrismair * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
133dfb59d50631968ab1a13002ea5421ece93169851chrismair * completely replace the ResourceBundle file by calling the calling the
134dfb59d50631968ab1a13002ea5421ece93169851chrismair * {@link #setReplyTextBaseName(String)} method.
135dfb59d50631968ab1a13002ea5421ece93169851chrismair *
136dfb59d50631968ab1a13002ea5421ece93169851chrismair * @author Chris Mair
137dfb59d50631968ab1a13002ea5421ece93169851chrismair * @version $Revision$ - $Date$
138dfb59d50631968ab1a13002ea5421ece93169851chrismair */
139dfb59d50631968ab1a13002ea5421ece93169851chrismairpublic class FakeFtpServer extends AbstractFtpServer implements ServerConfiguration {
140dfb59d50631968ab1a13002ea5421ece93169851chrismair
141dfb59d50631968ab1a13002ea5421ece93169851chrismair    private FileSystem fileSystem;
142dfb59d50631968ab1a13002ea5421ece93169851chrismair    private String systemName = "WINDOWS";
143dfb59d50631968ab1a13002ea5421ece93169851chrismair    private String systemStatus = "Connected";
144dfb59d50631968ab1a13002ea5421ece93169851chrismair    private Map helpText = new HashMap();
145dfb59d50631968ab1a13002ea5421ece93169851chrismair    private Map userAccounts = new HashMap();
146dfb59d50631968ab1a13002ea5421ece93169851chrismair
147dfb59d50631968ab1a13002ea5421ece93169851chrismair    public FileSystem getFileSystem() {
148dfb59d50631968ab1a13002ea5421ece93169851chrismair        return fileSystem;
149dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
150dfb59d50631968ab1a13002ea5421ece93169851chrismair
151dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void setFileSystem(FileSystem fileSystem) {
152dfb59d50631968ab1a13002ea5421ece93169851chrismair        this.fileSystem = fileSystem;
153dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
154dfb59d50631968ab1a13002ea5421ece93169851chrismair
155dfb59d50631968ab1a13002ea5421ece93169851chrismair    public String getSystemName() {
156dfb59d50631968ab1a13002ea5421ece93169851chrismair        return systemName;
157dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
158dfb59d50631968ab1a13002ea5421ece93169851chrismair
159dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void setSystemName(String systemName) {
160dfb59d50631968ab1a13002ea5421ece93169851chrismair        this.systemName = systemName;
161dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
162dfb59d50631968ab1a13002ea5421ece93169851chrismair
163dfb59d50631968ab1a13002ea5421ece93169851chrismair    public Map getHelpText() {
164dfb59d50631968ab1a13002ea5421ece93169851chrismair        return helpText;
165dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
166dfb59d50631968ab1a13002ea5421ece93169851chrismair
167dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void setHelpText(Map helpText) {
168dfb59d50631968ab1a13002ea5421ece93169851chrismair        this.helpText = helpText;
169dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
170dfb59d50631968ab1a13002ea5421ece93169851chrismair
171dfb59d50631968ab1a13002ea5421ece93169851chrismair    public FakeFtpServer() {
172dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.ACCT, new AcctCommandHandler());
173dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.ABOR, new AborCommandHandler());
174dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.ALLO, new AlloCommandHandler());
175dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.APPE, new AppeCommandHandler());
176dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.CWD, new CwdCommandHandler());
177dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.CDUP, new CdupCommandHandler());
178dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.DELE, new DeleCommandHandler());
179dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.EPRT, new EprtCommandHandler());
180dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.EPSV, new EpsvCommandHandler());
181dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.HELP, new HelpCommandHandler());
182dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.LIST, new ListCommandHandler());
183dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.MKD, new MkdCommandHandler());
184dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.MODE, new ModeCommandHandler());
185dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.NLST, new NlstCommandHandler());
186dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.NOOP, new NoopCommandHandler());
187dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.PASS, new PassCommandHandler());
188dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.PASV, new PasvCommandHandler());
189dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.PWD, new PwdCommandHandler());
190dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.PORT, new PortCommandHandler());
191dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.QUIT, new QuitCommandHandler());
192dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.REIN, new ReinCommandHandler());
193dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.REST, new RestCommandHandler());
194dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.RETR, new RetrCommandHandler());
195dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.RMD, new RmdCommandHandler());
196dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler());
197dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.RNTO, new RntoCommandHandler());
198dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.SITE, new SiteCommandHandler());
199dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.SMNT, new SmntCommandHandler());
200dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.STAT, new StatCommandHandler());
201dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.STOR, new StorCommandHandler());
202dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.STOU, new StouCommandHandler());
203dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.STRU, new StruCommandHandler());
204dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.SYST, new SystCommandHandler());
205dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.TYPE, new TypeCommandHandler());
206dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.USER, new UserCommandHandler());
207dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.XPWD, new PwdCommandHandler());
208dfb59d50631968ab1a13002ea5421ece93169851chrismair
209dfb59d50631968ab1a13002ea5421ece93169851chrismair        // "Special" Command Handlers
210dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler());
211dfb59d50631968ab1a13002ea5421ece93169851chrismair        setCommandHandler(CommandNames.UNSUPPORTED, new UnsupportedCommandHandler());
212dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
213dfb59d50631968ab1a13002ea5421ece93169851chrismair
214dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
215dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Initialize a CommandHandler that has been registered to this server.
216dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
217dfb59d50631968ab1a13002ea5421ece93169851chrismair     * If the CommandHandler implements the <code>ServerConfigurationAware</code> interface, then set its
218dfb59d50631968ab1a13002ea5421ece93169851chrismair     * <code>ServerConfiguration</code> property to <code>this</code>.
219dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
220dfb59d50631968ab1a13002ea5421ece93169851chrismair     * If the CommandHandler implements the <code>ReplyTextBundleAware</code> interface, then set its
221dfb59d50631968ab1a13002ea5421ece93169851chrismair     * <code>replyTextBundle</code> property using the reply text bundle for this server.
222dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
223dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param commandHandler - the CommandHandler to initialize
224dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
225dfb59d50631968ab1a13002ea5421ece93169851chrismair    protected void initializeCommandHandler(CommandHandler commandHandler) {
226dfb59d50631968ab1a13002ea5421ece93169851chrismair        if (commandHandler instanceof ServerConfigurationAware) {
227dfb59d50631968ab1a13002ea5421ece93169851chrismair            ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler;
228dfb59d50631968ab1a13002ea5421ece93169851chrismair            sca.setServerConfiguration(this);
229dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
230dfb59d50631968ab1a13002ea5421ece93169851chrismair
231dfb59d50631968ab1a13002ea5421ece93169851chrismair        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle());
232dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
233dfb59d50631968ab1a13002ea5421ece93169851chrismair
234dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
235dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the {@link UserAccount}        configured for this server for the specified user name
236dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
237dfb59d50631968ab1a13002ea5421ece93169851chrismair    public UserAccount getUserAccount(String username) {
238dfb59d50631968ab1a13002ea5421ece93169851chrismair        return (UserAccount) userAccounts.get(username);
239dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
240dfb59d50631968ab1a13002ea5421ece93169851chrismair
241dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
242dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Return the help text for a command or the default help text if no command name is specified
243dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
244dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param name - the command name; may be empty or null to indicate  a request for the default help text
245dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the help text for the named command or the default help text if no name is supplied
246dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
247dfb59d50631968ab1a13002ea5421ece93169851chrismair    public String getHelpText(String name) {
248dfb59d50631968ab1a13002ea5421ece93169851chrismair        String key = name == null ? "" : name;
249dfb59d50631968ab1a13002ea5421ece93169851chrismair        return (String) helpText.get(key);
250dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
251dfb59d50631968ab1a13002ea5421ece93169851chrismair
252dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
253dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Add a single UserAccount. If an account with the same <code>username</code> already exists,
254dfb59d50631968ab1a13002ea5421ece93169851chrismair     * it will be replaced.
255dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
256dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param userAccount - the UserAccount to add
257dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
258dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void addUserAccount(UserAccount userAccount) {
259dfb59d50631968ab1a13002ea5421ece93169851chrismair        userAccounts.put(userAccount.getUsername(), userAccount);
260dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
261dfb59d50631968ab1a13002ea5421ece93169851chrismair
262dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
263dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Add the UserAccount objects in the <code>userAccountList</code> to the set of UserAccounts.
264dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
265dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param userAccountList - the List of UserAccount objects to add
266dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
267dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void setUserAccounts(List userAccountList) {
268dfb59d50631968ab1a13002ea5421ece93169851chrismair        for (int i = 0; i < userAccountList.size(); i++) {
269dfb59d50631968ab1a13002ea5421ece93169851chrismair            UserAccount userAccount = (UserAccount) userAccountList.get(i);
270dfb59d50631968ab1a13002ea5421ece93169851chrismair            userAccounts.put(userAccount.getUsername(), userAccount);
271dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
272dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
273dfb59d50631968ab1a13002ea5421ece93169851chrismair
274dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
275dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Return the system status description
276dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
277dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the system status
278dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
279dfb59d50631968ab1a13002ea5421ece93169851chrismair    public String getSystemStatus() {
280dfb59d50631968ab1a13002ea5421ece93169851chrismair        return systemStatus;
281dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
282dfb59d50631968ab1a13002ea5421ece93169851chrismair
283dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
284dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Set the system status description text, used by the STAT command handler.
285dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
286dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param systemStatus - the system status description text
287dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
288dfb59d50631968ab1a13002ea5421ece93169851chrismair    public void setSystemStatus(String systemStatus) {
289dfb59d50631968ab1a13002ea5421ece93169851chrismair        this.systemStatus = systemStatus;
290dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
291dfb59d50631968ab1a13002ea5421ece93169851chrismair
292dfb59d50631968ab1a13002ea5421ece93169851chrismair}