01: /*
02: * This program is free software; you can redistribute it and/or
03: * modify it under the terms of the GNU General Public License
04: * as published by the Free Software Foundation; either version 2
05: * of the License, or (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package net.sf.jftp.event;
17:
18: import java.util.Hashtable;
19: import java.util.Vector;
20:
21: public class EventProcessor implements Runnable, Acceptor,
22: FtpEventConstants, EventHandler {
23: private static Hashtable table = new Hashtable();
24: private Vector buffer;
25: private boolean done = false;
26:
27: public EventProcessor(Vector b) {
28: buffer = b;
29: new Thread(this ).start();
30: addHandler(FTPShutdown, this );
31: }
32:
33: public void accept(Event e) {
34: Integer code = new Integer(e.eventCode());
35: Vector handlers = (Vector) (table.get(code));
36:
37: if (handlers != null) {
38: for (int i = 0, max = handlers.size(); i < max; i++) {
39: ((EventHandler) (handlers.elementAt(i))).handle(e);
40: }
41: }
42: }
43:
44: public static void addHandler(int eventCode, EventHandler h) {
45: Integer code = new Integer(eventCode);
46: Vector handlers = (Vector) (table.get(code));
47:
48: if (handlers == null) {
49: handlers = new Vector();
50: table.put(code, handlers);
51: }
52:
53: handlers.addElement(h);
54: }
55:
56: public boolean handle(Event e) {
57: done = true;
58:
59: return true;
60: }
61:
62: public void run() {
63: while (!done) {
64: if (buffer.size() != 0) {
65: accept((Event) buffer.firstElement());
66: buffer.removeElementAt(0);
67: }
68: }
69: }
70: }
|