1 18 package net.sf.drftpd.mirroring; 19 20 import java.io.FileNotFoundException ; 21 import java.util.ArrayList ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 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 46 public class JobManagerCommandHandler implements CommandHandlerBundle { 47 48 public JobManagerCommandHandler() { 49 super(); 50 } 51 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 st = 67 new StringTokenizer (conn.getRequest().getArgument()); 68 LinkedRemoteFileInterface lrf; 69 try { 70 lrf = conn.getCurrentDirectory().lookupFile(st.nextToken()); 71 } catch (FileNotFoundException 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 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 e) { 88 return new FtpReply( 89 501, 90 conn.jprintf( 91 JobManagerCommandHandler.class.getName(), 92 "addjob.usage")); 93 } 94 HashSet destSlaves = new HashSet (); 95 FtpReply reply = new FtpReply(200); 96 while (st.hasMoreTokens()) { 97 String 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 jobs = 132 new ArrayList ( 133 conn.getConnectionManager().getJobManager().getAllJobs()); 134 ReplacerEnvironment env = new ReplacerEnvironment(); 135 for (Iterator iter = jobs.iterator(); iter.hasNext();) { 136 count++; 137 env.add("job", (Job) iter.next()); 138 env.add("count", new Integer (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 filename = conn.getRequest().getArgument(); 156 Job job = null; 157 List jobs = 158 new ArrayList ( 159 conn.getConnectionManager().getJobManager().getAllJobs()); 160 ReplacerEnvironment env = new ReplacerEnvironment(); 161 env.add("filename", filename); 162 for (Iterator 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 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 [] 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 |