1 18 package net.sf.drftpd.master.command; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 27 import org.drftpd.commands.CommandHandlerFactory; 28 29 import net.sf.drftpd.FatalException; 30 import net.sf.drftpd.FileExistsException; 31 import net.sf.drftpd.ObjectNotFoundException; 32 import net.sf.drftpd.master.BaseFtpConnection; 33 import net.sf.drftpd.master.ConnectionManager; 34 35 41 public class CommandManagerFactory { 42 43 private ConnectionManager _connMgr; 44 48 private Hashtable _hnds; 49 52 private Hashtable _cmds; 53 public void reload() { 54 unload(); 55 load(); 56 } 57 private void unload() { 58 for (Iterator iter = _hnds.values().iterator(); iter.hasNext();) { 59 ((CommandHandlerFactory) iter.next()).unload(); 60 } 61 } 62 63 private void load() { 64 Hashtable cmds = new Hashtable (); 65 Hashtable hnds = new Hashtable (); 66 Properties props = new Properties (); 67 try { 68 props.load(new FileInputStream ("conf/commandhandlers.conf")); 69 } catch (IOException e) { 70 throw new FatalException("Error loading commandhandlers.conf", e); 71 } 72 for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { 83 try { 84 Map.Entry entry = (Map.Entry ) iter.next(); 85 86 Class hndclass = Class.forName((String ) entry.getValue()); 87 88 CommandHandlerFactory hndinstance = 94 (CommandHandlerFactory) hnds.get(hndclass); 95 if (hndinstance == null) { 96 hndinstance = (CommandHandlerFactory) hndclass.newInstance(); 97 hndinstance.load(this); 98 hnds.put(hndclass, hndinstance); 99 } 100 String cmd = (String ) entry.getKey(); 101 if (cmds.containsKey(cmd)) 102 throw new FileExistsException(cmd + " is already mapped"); 103 cmds.put(cmd, hndclass); 104 } catch (Exception e) { 105 throw new FatalException("", e); 106 } 107 } 108 _cmds = cmds; 109 _hnds = hnds; 110 } 111 112 public CommandManagerFactory(ConnectionManager connMgr) { 113 _connMgr = connMgr; 114 load(); 129 } 130 131 public CommandManager initialize(BaseFtpConnection conn) { 132 CommandManager mgr = new CommandManager(conn, this); 133 return mgr; 134 } 135 136 139 public Hashtable getHandlersMap() { 140 return _hnds; 141 } 142 143 146 public Hashtable getCommandsMap() { 147 return _cmds; 148 } 149 public CommandHandlerBundle getHandler(Class clazz) 150 throws ObjectNotFoundException { 151 CommandHandlerBundle ret = (CommandHandlerBundle) _hnds.get(clazz); 152 if (ret == null) 153 throw new ObjectNotFoundException(); 154 return ret; 155 } 162 163 166 public ConnectionManager getConnectionManager() { 167 return _connMgr; 168 } 169 } 170 | Popular Tags |