KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > CommandManager


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.command;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.drftpd.commands.*;
27
28 import net.sf.drftpd.ObjectNotFoundException;
29 import net.sf.drftpd.master.BaseFtpConnection;
30 import net.sf.drftpd.master.FtpReply;
31
32 /**
33  * @author mog
34  * @version $Id: CommandManager.java,v 1.8 2004/06/01 15:40:29 mog Exp $
35  */

36 public class CommandManager {
37     //TODO reload me
38

39     private CommandManagerFactory _factory;
40     
41     /**
42      * String => CommandHandler
43      * Mapping commands to commandhandlers.
44      */

45     private Map JavaDoc commands = new Hashtable JavaDoc();
46     
47     /**
48      * Class => CommandHandler
49      * Kept so that CommandHandlers can look up each other.
50      */

51     private Hashtable JavaDoc hnds = new Hashtable JavaDoc();
52
53     public CommandManager(
54         BaseFtpConnection conn,
55         CommandManagerFactory initializer) {
56         _factory = initializer;
57         for (Iterator JavaDoc iter = _factory.getHandlersMap().entrySet().iterator();
58             iter.hasNext();
59             ) {
60             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
61             hnds.put(
62                 entry.getKey(),
63                 ((CommandHandlerFactory) entry.getValue()).initialize(conn, this));
64         }
65         for (Iterator JavaDoc iter = _factory.getCommandsMap().entrySet().iterator();
66             iter.hasNext();
67             ) {
68             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
69             commands.put(
70                 (String JavaDoc) entry.getKey(),
71                 (CommandHandler) hnds.get((Class JavaDoc) entry.getValue()));
72         }
73     }
74
75     public FtpReply execute(BaseFtpConnection conn)
76         throws UnhandledCommandException {
77         String JavaDoc command = conn.getRequest().getCommand();
78         CommandHandler handler = (CommandHandler) commands.get(command);
79         if (handler == null) {
80             throw new UnhandledCommandException(
81                 "No command handler for " + command);
82         }
83         return handler.execute(conn);
84     }
85
86     public CommandHandler getCommandHandler(Class JavaDoc clazz)
87         throws ObjectNotFoundException {
88         CommandHandler ret = (CommandHandler) hnds.get(clazz);
89         if (ret == null)
90             throw new ObjectNotFoundException();
91         return ret;
92     }
93
94     public List JavaDoc getHandledCommands(Class JavaDoc class1) {
95         ArrayList JavaDoc list = new ArrayList JavaDoc();
96         for (Iterator JavaDoc iter = commands.entrySet().iterator(); iter.hasNext();) {
97             Map.Entry JavaDoc element = (Map.Entry JavaDoc) iter.next();
98             if (element.getValue().getClass().equals(class1)) {
99                 list.add((String JavaDoc) element.getKey());
100             }
101         }
102         return list;
103     }
104
105     /**
106      * Class => CommandHandler
107      */

108     public Map JavaDoc getCommandHandlersMap() {
109         return hnds;
110     }
111 }
112
Popular Tags