KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
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 /**
36  * @author mog
37  *
38  * Istantiates the CommandManager instances that holds per-connection CommandHandlers.
39  * @version $Id: CommandManagerFactory.java,v 1.8.2.1 2004/06/04 21:48:00 mog Exp $
40  */

41 public class CommandManagerFactory {
42
43     private ConnectionManager _connMgr;
44     //private static final Logger logger = Logger.getLogger(CommandManagerFactory.class);
45
/**
46      * Class => CommandHandlerFactory
47      */

48     private Hashtable JavaDoc _hnds;
49     /**
50      * String=> Class
51      */

52     private Hashtable JavaDoc _cmds;
53     public void reload() {
54         unload();
55         load();
56     }
57     private void unload() {
58         for (Iterator JavaDoc iter = _hnds.values().iterator(); iter.hasNext();) {
59             ((CommandHandlerFactory) iter.next()).unload();
60         }
61     }
62
63     private void load() {
64         Hashtable JavaDoc cmds = new Hashtable JavaDoc();
65         Hashtable JavaDoc hnds = new Hashtable JavaDoc();
66         Properties JavaDoc props = new Properties JavaDoc();
67         try {
68             props.load(new FileInputStream JavaDoc("conf/commandhandlers.conf"));
69         } catch (IOException JavaDoc e) {
70             throw new FatalException("Error loading commandhandlers.conf", e);
71         }
72         // URLClassLoader classLoader;
73
// try {
74
// classLoader =
75
// URLClassLoader.newInstance(
76
// new URL[] {
77
// new URL("file:classes/"),
78
// new URL("file:lib/log4j-1.2.8.jar")});
79
// } catch (MalformedURLException e1) {
80
// throw new RuntimeException(e1);
81
// }
82
for (Iterator JavaDoc iter = props.entrySet().iterator(); iter.hasNext();) {
83             try {
84                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
85
86                 Class JavaDoc hndclass = Class.forName((String JavaDoc) entry.getValue());
87
88 // Class hndclass =
89
// Class.forName(
90
// (String) entry.getValue(),
91
// false,
92
// classLoader);
93
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 JavaDoc cmd = (String JavaDoc) entry.getKey();
101                 if (cmds.containsKey(cmd))
102                     throw new FileExistsException(cmd + " is already mapped");
103                 cmds.put(cmd, hndclass);
104             } catch (Exception JavaDoc 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         // Login login = new Login();
115
// handlers = new Hashtable();
116
// handlers.put("USER", login);
117
// handlers.put("PASS", login);
118
// handlers = new ArrayList();
119
// handlers.add(new Login());
120
// handlers.add(new Dir());
121
// handlers.add(new List());
122
// handlers.add(new DataConnectionHandler());
123
// handlers.add(new Search());
124
// handlers.add(new UserManagment());
125
//_conn = conn;
126
//login.init(conn);
127
//Hashtable handlers = new Hashtable();
128
load();
129     }
130
131     public CommandManager initialize(BaseFtpConnection conn) {
132         CommandManager mgr = new CommandManager(conn, this);
133         return mgr;
134     }
135
136     /**
137      * Class => CommandHandler
138      */

139     public Hashtable JavaDoc getHandlersMap() {
140         return _hnds;
141     }
142
143     /**
144      * String=> Class
145      */

146     public Hashtable JavaDoc getCommandsMap() {
147         return _cmds;
148     }
149     public CommandHandlerBundle getHandler(Class JavaDoc clazz)
150         throws ObjectNotFoundException {
151         CommandHandlerBundle ret = (CommandHandlerBundle) _hnds.get(clazz);
152         if (ret == null)
153             throw new ObjectNotFoundException();
154         return ret;
155         // for (Iterator iter = hnds.iterator(); iter.hasNext();) {
156
// CommandHandler handler = (CommandHandler) iter.next();
157
// if (handler.getClass().equals(clazz))
158
// return handler;
159
// }
160
// throw new ObjectNotFoundException();
161
}
162
163     /**
164      *
165      */

166     public ConnectionManager getConnectionManager() {
167         return _connMgr;
168     }
169 }
170
Popular Tags