001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.master.command;
019:
020: import net.sf.drftpd.FatalException;
021: import net.sf.drftpd.FileExistsException;
022: import net.sf.drftpd.ObjectNotFoundException;
023: import net.sf.drftpd.master.BaseFtpConnection;
024:
025: import org.drftpd.commands.CommandHandler;
026: import org.drftpd.commands.CommandHandlerFactory;
027: import org.drftpd.master.ConnectionManager;
028:
029: import java.io.FileInputStream;
030: import java.io.IOException;
031:
032: import java.util.Hashtable;
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.Properties;
036:
037: /**
038: * @author mog
039: *
040: * Istantiates the CommandManager instances that holds per-connection CommandHandlers.
041: * @version $Id: CommandManagerFactory.java 1562 2007-01-05 12:37:07Z zubov $
042: */
043: public class CommandManagerFactory {
044: private ConnectionManager _connMgr;
045:
046: /**
047: * Class => CommandHandlerFactory
048: */
049: private Hashtable _hnds;
050:
051: /**
052: * String=> Class
053: */
054: private Hashtable _cmds;
055:
056: public CommandManagerFactory(ConnectionManager connMgr) {
057: _connMgr = connMgr;
058:
059: // Login login = new Login();
060: // handlers = new Hashtable();
061: // handlers.put("USER", login);
062: // handlers.put("PASS", login);
063: // handlers = new ArrayList();
064: // handlers.add(new Login());
065: // handlers.add(new Dir());
066: // handlers.add(new List());
067: // handlers.add(new DataConnectionHandler());
068: // handlers.add(new Search());
069: // handlers.add(new UserManagement());
070: //_conn = conn;
071: //login.init(conn);
072: //Hashtable handlers = new Hashtable();
073: load();
074: }
075:
076: public void reload() {
077: unload();
078: load();
079: }
080:
081: private void unload() {
082: for (Iterator iter = _hnds.values().iterator(); iter.hasNext();) {
083: Object o = iter.next();
084:
085: try {
086: CommandHandlerFactory c = ((CommandHandlerFactory) o);
087: c.unload();
088: } catch (ClassCastException e) {
089: throw e;
090: }
091: }
092: }
093:
094: private void load() {
095: Hashtable cmds = new Hashtable();
096: Hashtable hnds = new Hashtable();
097: Properties props = new Properties();
098: FileInputStream stream = null;
099: try {
100: stream = new FileInputStream("conf/commandhandlers.conf");
101: props.load(stream);
102:
103: // URLClassLoader classLoader;
104: // try {
105: // classLoader =
106: // URLClassLoader.newInstance(
107: // new URL[] {
108: // new URL("file:classes/"),
109: // new URL("file:lib/log4j-1.2.8.jar")});
110: // } catch (MalformedURLException e1) {
111: // throw new RuntimeException(e1);
112: // }
113: for (Iterator iter = props.entrySet().iterator(); iter
114: .hasNext();) {
115: try {
116: Map.Entry entry = (Map.Entry) iter.next();
117:
118: Class hndclass = Class.forName((String) entry
119: .getValue());
120:
121: // Class hndclass =
122: // Class.forName(
123: // (String) entry.getValue(),
124: // false,
125: // classLoader);
126: CommandHandlerFactory hndinstance = (CommandHandlerFactory) hnds
127: .get(hndclass);
128:
129: if (hndinstance == null) {
130: hndinstance = (CommandHandlerFactory) hndclass
131: .newInstance();
132: hndinstance.load(this );
133: hnds.put(hndclass, hndinstance);
134: }
135:
136: String cmd = (String) entry.getKey();
137:
138: if (cmds.containsKey(cmd)) {
139: throw new FileExistsException(cmd
140: + " is already mapped");
141: }
142:
143: cmds.put(cmd, hndclass);
144: } catch (Exception e) {
145: throw new FatalException("", e);
146: }
147: }
148:
149: _cmds = cmds;
150: _hnds = hnds;
151: } catch (IOException e) {
152: throw new FatalException(
153: "Error loading commandhandlers.conf", e);
154: } finally {
155: if (stream != null) {
156: try {
157: stream.close();
158: } catch (IOException e) {
159: }
160: }
161: }
162: }
163:
164: public CommandManager initialize(BaseFtpConnection conn) {
165: CommandManager mgr = new CommandManager(conn, this );
166:
167: return mgr;
168: }
169:
170: /**
171: * Class => CommandHandler
172: */
173: public Hashtable getHandlersMap() {
174: return _hnds;
175: }
176:
177: /**
178: * String=> Class
179: */
180: public Hashtable getCommandsMap() {
181: return _cmds;
182: }
183:
184: public CommandHandler getHandler(Class clazz)
185: throws ObjectNotFoundException {
186: CommandHandler ret = (CommandHandler) _hnds.get(clazz);
187:
188: if (ret == null) {
189: throw new ObjectNotFoundException();
190: }
191:
192: return ret;
193:
194: // for (Iterator iter = hnds.iterator(); iter.hasNext();) {
195: // CommandHandler handler = (CommandHandler) iter.next();
196: // if (handler.getClass().equals(clazz))
197: // return handler;
198: // }
199: // throw new ObjectNotFoundException();
200: }
201:
202: /**
203: *
204: */
205: public ConnectionManager getConnectionManager() {
206: return _connMgr;
207: }
208: }
|