1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.IOException ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import net.sf.drftpd.ObjectNotFoundException; 25 import net.sf.drftpd.SlaveUnavailableException; 26 import net.sf.drftpd.master.BaseFtpConnection; 27 import net.sf.drftpd.master.FtpReply; 28 import net.sf.drftpd.master.RemoteSlave; 29 import net.sf.drftpd.master.command.CommandManager; 30 import net.sf.drftpd.master.command.CommandManagerFactory; 31 import net.sf.drftpd.slave.SlaveStatus; 32 33 import org.drftpd.commands.CommandHandler; 34 import org.drftpd.commands.CommandHandlerFactory; 35 import org.drftpd.commands.UnhandledCommandException; 36 import org.drftpd.plugins.SiteBot; 37 import org.tanesha.replacer.ReplacerEnvironment; 38 39 44 public class SlaveManagment implements CommandHandlerFactory, CommandHandler { 45 public void unload() { 46 } 47 public void load(CommandManagerFactory initializer) { 48 } 49 50 private FtpReply doSITE_CHECKSLAVES(BaseFtpConnection conn) { 51 return new FtpReply( 52 200, 53 "Ok, " 54 + conn.getSlaveManager().verifySlaves() 55 + " stale slaves removed"); 56 } 57 58 private FtpReply doSITE_KICKSLAVE(BaseFtpConnection conn) { 59 if (!conn.getUserNull().isAdmin()) { 60 return FtpReply.RESPONSE_530_ACCESS_DENIED; 61 } 62 if (!conn.getRequest().hasArgument()) { 63 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 64 } 65 RemoteSlave rslave; 66 try { 67 rslave = 68 conn.getConnectionManager().getSlaveManager().getSlave( 69 conn.getRequest().getArgument()); 70 } catch (ObjectNotFoundException e) { 71 return new FtpReply(200, "No such slave"); 72 } 73 if (!rslave.isAvailable()) { 74 return new FtpReply(200, "Slave is already offline"); 75 } 76 rslave.setOffline( 77 "Slave kicked by " + conn.getUserNull().getUsername()); 78 return FtpReply.RESPONSE_200_COMMAND_OK; 79 } 80 81 85 private FtpReply doSITE_SLAVES(BaseFtpConnection conn) { 86 boolean showRMI = 87 conn.getRequest().hasArgument() 88 && conn.getRequest().getArgument().indexOf("rmi") != -1; 89 if (showRMI && !conn.getUserNull().isAdmin()) { 90 return FtpReply.RESPONSE_530_ACCESS_DENIED; 91 } 92 boolean showPlain = 93 !conn.getRequest().hasArgument() 94 || conn.getRequest().getArgument().indexOf("plain") != -1; 95 Collection slaves = conn.getSlaveManager().getSlaves(); 96 FtpReply response = 97 new FtpReply(200, "OK, " + slaves.size() + " slaves listed."); 98 99 for (Iterator iter = conn.getSlaveManager().getSlaves().iterator(); 100 iter.hasNext(); 101 ) { 102 RemoteSlave rslave = (RemoteSlave) iter.next(); 103 if (showRMI) { 104 response.addComment(rslave.toString()); 105 } 106 if (showPlain) { 107 ReplacerEnvironment env = new ReplacerEnvironment(); 108 env.add("slave", rslave.getName()); 109 try { 110 SlaveStatus status = rslave.getStatus(); 111 SiteBot.fillEnvSlaveStatus( 112 env, 113 status, 114 conn.getSlaveManager()); 115 response.addComment( 116 conn.jprintf(SlaveManagment.class, "slaves", env)); 117 } catch (SlaveUnavailableException e) { 118 response.addComment( 119 conn.jprintf( 120 SlaveManagment.class, 121 "slaves.offline", 122 env)); 123 } 124 } 125 } 126 return response; 127 } 128 129 private FtpReply doSITE_REMERGE(BaseFtpConnection conn) { 130 if (!conn.getUserNull().isAdmin()) { 131 return FtpReply.RESPONSE_530_ACCESS_DENIED; 132 } 133 if (!conn.getRequest().hasArgument()) { 134 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 135 } 136 RemoteSlave rslave; 137 try { 138 rslave = 139 conn.getConnectionManager().getSlaveManager().getSlave( 140 conn.getRequest().getArgument()); 141 } catch (ObjectNotFoundException e) { 142 return new FtpReply(200, "No such slave"); 143 } 144 if (!rslave.isAvailable()) { 145 return new FtpReply(200, "Slave is offline"); 146 } 147 try { 148 conn.getConnectionManager().getSlaveManager().remerge(rslave); 149 } catch (IOException e) { 150 rslave.setOffline("IOException during remerge()"); 151 return new FtpReply(200, "IOException during remerge()"); 152 } catch (SlaveUnavailableException e) { 153 rslave.setOffline("Slave Unavailable during remerge()"); 154 return new FtpReply(200, "Slave Unavailable during remerge()"); 155 } 156 return FtpReply.RESPONSE_200_COMMAND_OK; 157 158 } 159 160 public FtpReply execute(BaseFtpConnection conn) 161 throws UnhandledCommandException { 162 String cmd = conn.getRequest().getCommand(); 163 if ("SITE CHECKSLAVES".equals(cmd)) 164 return doSITE_CHECKSLAVES(conn); 165 if ("SITE KICKSLAVE".equals(cmd)) 166 return doSITE_KICKSLAVE(conn); 167 if ("SITE SLAVES".equals(cmd)) 168 return doSITE_SLAVES(conn); 169 if ("SITE REMERGE".equals(cmd)) 170 return doSITE_REMERGE(conn); 171 throw UnhandledCommandException.create( 172 SlaveManagment.class, 173 conn.getRequest()); 174 } 175 178 public CommandHandler initialize( 179 BaseFtpConnection conn, 180 CommandManager initializer) { 181 return this; 182 } 183 184 public String [] getFeatReplies() { 185 return null; 186 } 187 } 188 | Popular Tags |