KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > mirroring > JobManagerCommandHandler


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.mirroring;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import org.drftpd.commands.CommandHandler;
28 import org.drftpd.commands.UnhandledCommandException;
29 import org.tanesha.replacer.ReplacerEnvironment;
30
31 import net.sf.drftpd.ObjectNotFoundException;
32 import net.sf.drftpd.master.BaseFtpConnection;
33 import net.sf.drftpd.master.FtpReply;
34 import net.sf.drftpd.master.RemoteSlave;
35 import net.sf.drftpd.master.command.CommandHandlerBundle;
36 import net.sf.drftpd.master.command.CommandManager;
37 import net.sf.drftpd.master.command.CommandManagerFactory;
38 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
39
40 /**
41  * CommandHandler plugin for viewing and manipulating the JobManager queue.
42  *
43  * @author mog
44  * @version $Id: JobManagerCommandHandler.java,v 1.16 2004/06/01 15:40:32 mog Exp $
45  */

46 public class JobManagerCommandHandler implements CommandHandlerBundle {
47
48     public JobManagerCommandHandler() {
49         super();
50     }
51     /**
52      * USAGE: <file> <priority> [destslave ...]
53      * @param conn
54      * @return
55      */

56     private FtpReply doADDJOB(BaseFtpConnection conn) {
57         if (!conn.getUserNull().isAdmin())
58             return FtpReply.RESPONSE_530_ACCESS_DENIED;
59         if (!conn.getRequest().hasArgument()) {
60             return new FtpReply(
61                 501,
62                 conn.jprintf(
63                     JobManagerCommandHandler.class.getName(),
64                     "addjob.usage"));
65         }
66         StringTokenizer JavaDoc st =
67             new StringTokenizer JavaDoc(conn.getRequest().getArgument());
68         LinkedRemoteFileInterface lrf;
69         try {
70             lrf = conn.getCurrentDirectory().lookupFile(st.nextToken());
71         } catch (FileNotFoundException JavaDoc e) {
72             return new FtpReply(500, "File does not exist");
73         }
74         int priority;
75         try {
76             priority = Integer.parseInt(st.nextToken());
77         } catch (NumberFormatException JavaDoc e) {
78             return new FtpReply(
79                     501,
80                     conn.jprintf(
81                         JobManagerCommandHandler.class.getName(),
82                         "addjob.usage"));
83         }
84         int timesToMirror;
85         try {
86             timesToMirror = Integer.parseInt(st.nextToken());
87         } catch (NumberFormatException JavaDoc e) {
88             return new FtpReply(
89                     501,
90                     conn.jprintf(
91                         JobManagerCommandHandler.class.getName(),
92                         "addjob.usage"));
93         }
94         HashSet JavaDoc destSlaves = new HashSet JavaDoc();
95         FtpReply reply = new FtpReply(200);
96         while (st.hasMoreTokens()) {
97             String JavaDoc slaveName = st.nextToken();
98             RemoteSlave rslave;
99             try {
100                 rslave = conn.getSlaveManager().getSlave(slaveName);
101             } catch (ObjectNotFoundException e1) {
102                 reply.addComment(
103                     slaveName
104                         + "was not found, cannot add to destination slave list");
105                 continue;
106             }
107             destSlaves.add(rslave);
108         }
109         if (destSlaves.size() == 0 ) {
110             return new FtpReply(
111                 501,
112                 conn.jprintf(JobManagerCommandHandler.class, "addjob.usage"));
113         }
114         Job job = new Job(lrf, destSlaves, this, conn.getUserNull(), priority, timesToMirror);
115         conn.getConnectionManager().getJobManager().addJob(job);
116         ReplacerEnvironment env = new ReplacerEnvironment();
117         env.add("job", job);
118         reply.addComment(
119             conn.jprintf(
120                 JobManagerCommandHandler.class,
121                 "addjob.success",
122                 env));
123         return reply;
124     }
125
126     private FtpReply doLISTJOBS(BaseFtpConnection conn) {
127         if (!conn.getUserNull().isAdmin())
128             return FtpReply.RESPONSE_530_ACCESS_DENIED;
129         FtpReply reply = new FtpReply(200);
130         int count = 0;
131         List JavaDoc jobs =
132             new ArrayList JavaDoc(
133                 conn.getConnectionManager().getJobManager().getAllJobs());
134         ReplacerEnvironment env = new ReplacerEnvironment();
135         for (Iterator JavaDoc iter = jobs.iterator(); iter.hasNext();) {
136             count++;
137             env.add("job", (Job) iter.next());
138             env.add("count", new Integer JavaDoc(count));
139             reply.addComment(
140                 conn.jprintf(JobManagerCommandHandler.class, "listjob", env));
141         }
142         return reply;
143     }
144
145     private FtpReply doREMOVEJOB(BaseFtpConnection conn) {
146         if (!conn.getUserNull().isAdmin())
147             return FtpReply.RESPONSE_530_ACCESS_DENIED;
148         if (!conn.getRequest().hasArgument()) {
149             return new FtpReply(
150                 501,
151                 conn.jprintf(
152                     JobManagerCommandHandler.class.getName(),
153                     "removejob.usage"));
154         }
155         String JavaDoc filename = conn.getRequest().getArgument();
156         Job job = null;
157         List JavaDoc jobs =
158             new ArrayList JavaDoc(
159                 conn.getConnectionManager().getJobManager().getAllJobs());
160         ReplacerEnvironment env = new ReplacerEnvironment();
161         env.add("filename", filename);
162         for (Iterator JavaDoc iter = jobs.iterator(); iter.hasNext();) {
163             job = (Job) iter.next();
164             if (job.getFile().getName().equals(filename)) {
165                 env.add("job", job);
166                 conn.getConnectionManager().getJobManager().stopJob(job);
167                 return new FtpReply(
168                     200,
169                     conn.jprintf(
170                         JobManagerCommandHandler.class.getName(),
171                         "removejob.success",
172                         env));
173             }
174         }
175         return new FtpReply(
176             200,
177             conn.jprintf(
178                 JobManagerCommandHandler.class.getName(),
179                 "removejob.fail",
180                 env));
181     }
182
183     private FtpReply doSTARTJOBS(BaseFtpConnection conn) {
184         if (!conn.getUserNull().isAdmin())
185             return FtpReply.RESPONSE_530_ACCESS_DENIED;
186         conn.getConnectionManager().getJobManager().startJobs();
187         return new FtpReply(200, "JobTransfers will now start");
188     }
189
190     private FtpReply doSTOPJOBS(BaseFtpConnection conn) {
191         if (!conn.getUserNull().isAdmin())
192             return FtpReply.RESPONSE_530_ACCESS_DENIED;
193         conn.getConnectionManager().getJobManager().stopJobs();
194         return new FtpReply(
195             200,
196             "All JobTransfers will stop after their current transfer");
197     }
198
199     public FtpReply execute(BaseFtpConnection conn)
200         throws UnhandledCommandException {
201         String JavaDoc cmd = conn.getRequest().getCommand();
202         if ("SITE LISTJOBS".equals(cmd)) {
203             return doLISTJOBS(conn);
204         }
205         if ("SITE REMOVEJOB".equals(cmd)) {
206             return doREMOVEJOB(conn);
207         }
208         if ("SITE ADDJOB".equals(cmd)) {
209             return doADDJOB(conn);
210         }
211         if ("SITE STOPJOBS".equals(cmd)) {
212             return doSTOPJOBS(conn);
213         }
214         if ("SITE STARTJOBS".equals(cmd)) {
215             return doSTARTJOBS(conn);
216         }
217
218         throw UnhandledCommandException.create(
219             JobManagerCommandHandler.class,
220             conn.getRequest());
221
222     }
223
224     public String JavaDoc[] getFeatReplies() {
225         return null;
226     }
227
228     public CommandHandler initialize(
229         BaseFtpConnection conn,
230         CommandManager initializer) {
231         return this;
232     }
233
234     public void load(CommandManagerFactory initializer) {
235     }
236
237     public void unload() {
238     }
239
240 }
241
Popular Tags