KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > event > EventProcessor


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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 JavaDoc;
19 import java.util.Vector JavaDoc;
20
21
22 public class EventProcessor implements Runnable JavaDoc, Acceptor, FtpEventConstants,
23                                        EventHandler
24 {
25     private static Hashtable JavaDoc table = new Hashtable JavaDoc();
26     private Vector JavaDoc buffer;
27     private boolean done = false;
28
29     public EventProcessor(Vector JavaDoc b)
30     {
31         buffer = b;
32         new Thread JavaDoc(this).start();
33         addHandler(FTPShutdown, this);
34     }
35
36     public void accept(Event e)
37     {
38         Integer JavaDoc code = new Integer JavaDoc(e.eventCode());
39         Vector JavaDoc handlers = (Vector JavaDoc) (table.get(code));
40
41         if(handlers != null)
42         {
43             for(int i = 0, max = handlers.size(); i < max; i++)
44             {
45                 ((EventHandler) (handlers.elementAt(i))).handle(e);
46             }
47         }
48     }
49
50     public static void addHandler(int eventCode, EventHandler h)
51     {
52         Integer JavaDoc code = new Integer JavaDoc(eventCode);
53         Vector JavaDoc handlers = (Vector JavaDoc) (table.get(code));
54
55         if(handlers == null)
56         {
57             handlers = new Vector JavaDoc();
58             table.put(code, handlers);
59         }
60
61         handlers.addElement(h);
62     }
63
64     public boolean handle(Event e)
65     {
66         done = true;
67
68         return true;
69     }
70
71     public void run()
72     {
73         while(!done)
74         {
75             if(buffer.size() != 0)
76             {
77                 accept((Event) buffer.firstElement());
78                 buffer.removeElementAt(0);
79             }
80         }
81     }
82 }
83
Popular Tags