1 16 package net.sf.jftp.event; 17 18 import net.sf.jftp.net.FtpClient; 19 20 import java.lang.reflect.*; 21 22 import java.util.ArrayList ; 23 import java.util.Hashtable ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 27 28 public class FtpEventHandler implements EventHandler 29 { 30 private static ArrayList commands = null; 31 32 static 33 { 34 commands = new ArrayList (); 35 commands.add("account"); 36 commands.add("append"); 37 commands.add("ascii"); 38 commands.add("bell"); 39 commands.add("bye"); 40 commands.add("cd"); 41 commands.add("cdup"); 42 commands.add("chmod"); 43 commands.add("close"); 44 commands.add("cr"); commands.add("delete"); 46 commands.add("dir"); 47 commands.add("disconnect"); 48 commands.add("form"); commands.add("get"); 50 commands.add("glob"); commands.add("hash"); 52 commands.add("idle"); 53 commands.add("image"); 54 commands.add("lcd"); 55 commands.add("mdelete"); 56 commands.add("mdir"); 57 commands.add("mget"); 58 commands.add("mkdir"); 59 commands.add("mls"); 60 commands.add("mode"); 61 commands.add("modtime"); 62 commands.add("mput"); 63 commands.add("newer"); 64 commands.add("nlist"); 65 commands.add("open"); 66 commands.add("passive"); 67 commands.add("put"); 68 commands.add("pwd"); 69 commands.add("quote"); 70 commands.add("recv"); 71 commands.add("reget"); 72 commands.add("rstatus"); 73 commands.add("rhelp"); 74 commands.add("rename"); 75 commands.add("reset"); 76 commands.add("restart"); 77 commands.add("rmdir"); 78 commands.add("runique"); 79 commands.add("send"); 80 commands.add("sendport"); 81 commands.add("site"); 82 commands.add("size"); 83 commands.add("status"); 84 commands.add("struct"); 85 commands.add("system"); 86 commands.add("sunique"); 87 commands.add("type"); 88 commands.add("user"); 89 } 90 91 private FtpClient client; 92 private Hashtable methods = new Hashtable (); 93 94 public FtpEventHandler() 95 { 96 this.client = new FtpClient(); 97 98 Method[] m = this.getClass().getDeclaredMethods(); 99 String methodName = null; 100 101 for(int i = 0; i < m.length; i++) 102 { 103 methodName = m[i].getName(); 104 105 if(commands.contains(methodName)) 106 { 107 methods.put(methodName, m[i]); 108 } 109 } 110 } 111 112 public void open(Vector args) 113 { 114 System.out.println("***open"); 115 client.login((String ) args.elementAt(1)); 116 } 117 118 public void disconnect(Vector args) 119 { 120 System.out.println("***disconnect"); 121 client.disconnect(); 122 } 123 124 public void cd(Vector args) 125 { 126 System.out.println("***cd"); 127 client.cd((String ) args.elementAt(1)); 128 } 129 130 public void pwd(Vector args) 131 { 132 System.out.println("***pwd"); 133 134 String directory = client.pwd(); 135 } 136 137 public void get(Vector args) 138 { 139 System.out.println("***get"); 140 client.get((String ) args.elementAt(1)); 141 } 142 143 public void put(Vector args) 144 { 145 System.out.println("***put"); 146 client.put((String ) args.elementAt(1)); 147 } 148 149 public void quit(Vector args) 150 { 151 disconnect(args); 152 } 153 154 public boolean handle(Event e) 155 { 156 System.out.println(e.eventCode()); 157 System.out.println(((FtpEvent) e).eventMsg()); 158 159 StringTokenizer st = new StringTokenizer (((FtpEvent) e).eventMsg()); 160 Vector list = new Vector (); 161 162 while(st.hasMoreTokens()) 163 { 164 list.addElement(st.nextToken()); 165 } 166 167 if(list.size() != 0) 168 { 169 String command = (String ) list.elementAt(0); 170 Method o = (Method) methods.get(command.toLowerCase()); 171 172 if(o != null) 173 { 174 try 175 { 176 o.invoke(this, new Object [] { list }); 177 } 178 catch(Exception ex) 179 { 180 } 181 } 182 } 183 184 return true; 185 } 186 } 187 | Popular Tags |