001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.event;
017:
018: import net.sf.jftp.net.FtpClient;
019:
020: import java.lang.reflect.*;
021:
022: import java.util.ArrayList;
023: import java.util.Hashtable;
024: import java.util.StringTokenizer;
025: import java.util.Vector;
026:
027: public class FtpEventHandler implements EventHandler {
028: private static ArrayList commands = null;
029:
030: static {
031: commands = new ArrayList();
032: commands.add("account");
033: commands.add("append");
034: commands.add("ascii");
035: commands.add("bell");
036: commands.add("bye");
037: commands.add("cd");
038: commands.add("cdup");
039: commands.add("chmod");
040: commands.add("close");
041: commands.add("cr"); // unsupported
042: commands.add("delete");
043: commands.add("dir");
044: commands.add("disconnect");
045: commands.add("form"); // unsupported
046: commands.add("get");
047: commands.add("glob"); // unsupported
048: commands.add("hash");
049: commands.add("idle");
050: commands.add("image");
051: commands.add("lcd");
052: commands.add("mdelete");
053: commands.add("mdir");
054: commands.add("mget");
055: commands.add("mkdir");
056: commands.add("mls");
057: commands.add("mode");
058: commands.add("modtime");
059: commands.add("mput");
060: commands.add("newer");
061: commands.add("nlist");
062: commands.add("open");
063: commands.add("passive");
064: commands.add("put");
065: commands.add("pwd");
066: commands.add("quote");
067: commands.add("recv");
068: commands.add("reget");
069: commands.add("rstatus");
070: commands.add("rhelp");
071: commands.add("rename");
072: commands.add("reset");
073: commands.add("restart");
074: commands.add("rmdir");
075: commands.add("runique");
076: commands.add("send");
077: commands.add("sendport");
078: commands.add("site");
079: commands.add("size");
080: commands.add("status");
081: commands.add("struct");
082: commands.add("system");
083: commands.add("sunique");
084: commands.add("type");
085: commands.add("user");
086: }
087:
088: private FtpClient client;
089: private Hashtable methods = new Hashtable();
090:
091: public FtpEventHandler() {
092: this .client = new FtpClient();
093:
094: Method[] m = this .getClass().getDeclaredMethods();
095: String methodName = null;
096:
097: for (int i = 0; i < m.length; i++) {
098: methodName = m[i].getName();
099:
100: if (commands.contains(methodName)) {
101: methods.put(methodName, m[i]);
102: }
103: }
104: }
105:
106: public void open(Vector args) {
107: System.out.println("***open");
108: client.login((String) args.elementAt(1));
109: }
110:
111: public void disconnect(Vector args) {
112: System.out.println("***disconnect");
113: client.disconnect();
114: }
115:
116: public void cd(Vector args) {
117: System.out.println("***cd");
118: client.cd((String) args.elementAt(1));
119: }
120:
121: public void pwd(Vector args) {
122: System.out.println("***pwd");
123:
124: String directory = client.pwd();
125: }
126:
127: public void get(Vector args) {
128: System.out.println("***get");
129: client.get((String) args.elementAt(1));
130: }
131:
132: public void put(Vector args) {
133: System.out.println("***put");
134: client.put((String) args.elementAt(1));
135: }
136:
137: public void quit(Vector args) {
138: disconnect(args);
139: }
140:
141: public boolean handle(Event e) {
142: System.out.println(e.eventCode());
143: System.out.println(((FtpEvent) e).eventMsg());
144:
145: StringTokenizer st = new StringTokenizer(((FtpEvent) e)
146: .eventMsg());
147: Vector list = new Vector();
148:
149: while (st.hasMoreTokens()) {
150: list.addElement(st.nextToken());
151: }
152:
153: if (list.size() != 0) {
154: String command = (String) list.elementAt(0);
155: Method o = (Method) methods.get(command.toLowerCase());
156:
157: if (o != null) {
158: try {
159: o.invoke(this , new Object[] { list });
160: } catch (Exception ex) {
161: }
162: }
163: }
164:
165: return true;
166: }
167: }
|