1 18 package net.sf.drftpd.master.command; 19 20 import java.util.ArrayList ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 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 36 public class CommandManager { 37 39 private CommandManagerFactory _factory; 40 41 45 private Map commands = new Hashtable (); 46 47 51 private Hashtable hnds = new Hashtable (); 52 53 public CommandManager( 54 BaseFtpConnection conn, 55 CommandManagerFactory initializer) { 56 _factory = initializer; 57 for (Iterator iter = _factory.getHandlersMap().entrySet().iterator(); 58 iter.hasNext(); 59 ) { 60 Map.Entry entry = (Map.Entry ) iter.next(); 61 hnds.put( 62 entry.getKey(), 63 ((CommandHandlerFactory) entry.getValue()).initialize(conn, this)); 64 } 65 for (Iterator iter = _factory.getCommandsMap().entrySet().iterator(); 66 iter.hasNext(); 67 ) { 68 Map.Entry entry = (Map.Entry ) iter.next(); 69 commands.put( 70 (String ) entry.getKey(), 71 (CommandHandler) hnds.get((Class ) entry.getValue())); 72 } 73 } 74 75 public FtpReply execute(BaseFtpConnection conn) 76 throws UnhandledCommandException { 77 String 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 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 getHandledCommands(Class class1) { 95 ArrayList list = new ArrayList (); 96 for (Iterator iter = commands.entrySet().iterator(); iter.hasNext();) { 97 Map.Entry element = (Map.Entry ) iter.next(); 98 if (element.getValue().getClass().equals(class1)) { 99 list.add((String ) element.getKey()); 100 } 101 } 102 return list; 103 } 104 105 108 public Map getCommandHandlersMap() { 109 return hnds; 110 } 111 } 112 | Popular Tags |