1 16 package net.sf.jftp.event; 17 18 import java.util.Hashtable ; 19 import java.util.Vector ; 20 21 22 public class EventProcessor implements Runnable , Acceptor, FtpEventConstants, 23 EventHandler 24 { 25 private static Hashtable table = new Hashtable (); 26 private Vector buffer; 27 private boolean done = false; 28 29 public EventProcessor(Vector b) 30 { 31 buffer = b; 32 new Thread (this).start(); 33 addHandler(FTPShutdown, this); 34 } 35 36 public void accept(Event e) 37 { 38 Integer code = new Integer (e.eventCode()); 39 Vector handlers = (Vector ) (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 code = new Integer (eventCode); 53 Vector handlers = (Vector ) (table.get(code)); 54 55 if(handlers == null) 56 { 57 handlers = new Vector (); 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 |