KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.jftp.net.FtpClient;
19
20 import java.lang.reflect.*;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.Vector JavaDoc;
26
27
28 public class FtpEventHandler implements EventHandler
29 {
30     private static ArrayList JavaDoc commands = null;
31
32     static
33     {
34         commands = new ArrayList JavaDoc();
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"); // unsupported
45
commands.add("delete");
46         commands.add("dir");
47         commands.add("disconnect");
48         commands.add("form"); // unsupported
49
commands.add("get");
50         commands.add("glob"); // unsupported
51
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 JavaDoc methods = new Hashtable JavaDoc();
93
94     public FtpEventHandler()
95     {
96         this.client = new FtpClient();
97
98         Method[] m = this.getClass().getDeclaredMethods();
99         String JavaDoc 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 JavaDoc args)
113     {
114         System.out.println("***open");
115         client.login((String JavaDoc) args.elementAt(1));
116     }
117
118     public void disconnect(Vector JavaDoc args)
119     {
120         System.out.println("***disconnect");
121         client.disconnect();
122     }
123
124     public void cd(Vector JavaDoc args)
125     {
126         System.out.println("***cd");
127         client.cd((String JavaDoc) args.elementAt(1));
128     }
129
130     public void pwd(Vector JavaDoc args)
131     {
132         System.out.println("***pwd");
133
134         String JavaDoc directory = client.pwd();
135     }
136
137     public void get(Vector JavaDoc args)
138     {
139         System.out.println("***get");
140         client.get((String JavaDoc) args.elementAt(1));
141     }
142
143     public void put(Vector JavaDoc args)
144     {
145         System.out.println("***put");
146         client.put((String JavaDoc) args.elementAt(1));
147     }
148
149     public void quit(Vector JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(((FtpEvent) e).eventMsg());
160         Vector JavaDoc list = new Vector JavaDoc();
161
162         while(st.hasMoreTokens())
163         {
164             list.addElement(st.nextToken());
165         }
166
167         if(list.size() != 0)
168         {
169             String JavaDoc command = (String JavaDoc) 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 JavaDoc[] { list });
177                 }
178                 catch(Exception JavaDoc ex)
179                 {
180                 }
181             }
182         }
183
184         return true;
185     }
186 }
187
Popular Tags