1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.lang.reflect.Field ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.ResourceBundle ; 28 29 import net.sf.drftpd.event.FtpListener; 30 import net.sf.drftpd.event.UserEvent; 31 import net.sf.drftpd.master.BaseFtpConnection; 32 import net.sf.drftpd.master.FtpReply; 33 import net.sf.drftpd.master.command.CommandManager; 34 import net.sf.drftpd.master.command.CommandManagerFactory; 35 import net.sf.drftpd.remotefile.LinkedRemoteFile; 36 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 37 38 import org.apache.log4j.Level; 39 import org.apache.log4j.Logger; 40 import org.drftpd.commands.CommandHandler; 41 import org.drftpd.commands.CommandHandlerFactory; 42 import org.drftpd.commands.UnhandledCommandException; 43 44 49 public class SiteManagment implements CommandHandlerFactory, CommandHandler { 50 51 private static final Logger logger = Logger.getLogger(SiteManagment.class); 52 53 private FtpReply doSITE_LIST(BaseFtpConnection conn) { 54 if (!conn.getUserNull().isAdmin()) 55 return FtpReply.RESPONSE_530_ACCESS_DENIED; 56 FtpReply response = (FtpReply) FtpReply.RESPONSE_200_COMMAND_OK.clone(); 57 LinkedRemoteFile dir = conn.getCurrentDirectory(); 59 if (conn.getRequest().hasArgument()) { 60 try { 61 dir = dir.lookupFile(conn.getRequest().getArgument(), true); 62 } catch (FileNotFoundException e) { 63 logger.debug("", e); 64 return new FtpReply(200, e.getMessage()); 65 } 66 } 67 ArrayList files = new ArrayList (dir.getMap().values()); 68 Collections.sort(files); 69 for (Iterator iter = files.iterator(); iter.hasNext();) { 70 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 71 response.addComment(file.toString()); 76 } 77 return response; 78 } 79 80 private FtpReply doSITE_LOADPLUGIN(BaseFtpConnection conn) { 81 if (!conn.getUserNull().isAdmin()) { 82 return FtpReply.RESPONSE_530_ACCESS_DENIED; 83 } 84 if (!conn.getRequest().hasArgument()) { 85 return new FtpReply(500, "Usage: site load className"); 86 } 87 FtpListener ftpListener = 88 getFtpListener(conn.getRequest().getArgument()); 89 if (ftpListener == null) 90 return new FtpReply( 91 500, 92 "Was not able to find the class you are trying to load"); 93 conn.getConnectionManager().addFtpListener(ftpListener); 94 return new FtpReply(200, "Successfully loaded your plugin"); 95 } 96 97 private FtpReply doSITE_PLUGINS(BaseFtpConnection conn) { 98 if (!conn.getUserNull().isAdmin()) { 99 return FtpReply.RESPONSE_530_ACCESS_DENIED; 100 } 101 FtpReply ftpReply = new FtpReply(200, "Command ok"); 102 ftpReply.addComment("Plugins loaded:"); 103 for (Iterator iter = 104 conn.getConnectionManager().getFtpListeners().iterator(); 105 iter.hasNext(); 106 ) { 107 ftpReply.addComment(iter.next().getClass().getName()); 108 } 109 return ftpReply; 110 } 111 112 private FtpReply doSITE_RELOAD(BaseFtpConnection conn) { 113 if (!conn.getUserNull().isAdmin()) { 114 return FtpReply.RESPONSE_530_ACCESS_DENIED; 115 } 116 117 try { 118 conn.getConnectionManager().reload(); 119 conn.getConnectionManager().getConfig().reloadConfig(); 120 conn.getSlaveManager().reload(); 121 try { 122 conn.getConnectionManager().getJobManager().reload(); 123 } catch (IllegalStateException e1) { 124 } 126 conn.getConnectionManager().getCommandManagerFactory().reload(); 127 } catch (IOException e) { 129 logger.log(Level.FATAL, "Error reloading config", e); 130 return new FtpReply(200, e.getMessage()); 131 } 132 conn.getConnectionManager().dispatchFtpEvent( 133 new UserEvent(conn.getUserNull(), "RELOAD")); 134 135 try { 138 Field cacheList = 139 ResourceBundle .class.getDeclaredField("cacheList"); 140 cacheList.setAccessible(true); 141 ((Map ) cacheList.get(ResourceBundle .class)).clear(); 142 cacheList.setAccessible(false); 143 } catch (Exception e) { 144 logger.error("", e); 145 } 146 return FtpReply.RESPONSE_200_COMMAND_OK; 147 } 148 149 private FtpReply doSITE_SHUTDOWN(BaseFtpConnection conn) { 150 if (!conn.getUserNull().isAdmin()) { 151 return FtpReply.RESPONSE_530_ACCESS_DENIED; 152 } 153 String message; 154 if (!conn.getRequest().hasArgument()) { 155 message = 156 "Service shutdown issued by " 157 + conn.getUserNull().getUsername(); 158 } else { 159 message = conn.getRequest().getArgument(); 160 } 161 conn.getConnectionManager().shutdown(message); 162 return FtpReply.RESPONSE_200_COMMAND_OK; 163 } 164 private FtpReply doSITE_UNLOADPLUGIN(BaseFtpConnection conn) { 165 if (!conn.getUserNull().isAdmin()) { 166 return FtpReply.RESPONSE_530_ACCESS_DENIED; 167 } 168 if (!conn.getRequest().hasArgument()) { 169 return new FtpReply(500, "Usage: site unload className"); 170 } 171 for (Iterator iter = 172 conn.getConnectionManager().getFtpListeners().iterator(); 173 iter.hasNext(); 174 ) { 175 FtpListener ftpListener = (FtpListener) iter.next(); 176 if (ftpListener 177 .getClass() 178 .getName() 179 .equals( 180 "net.sf.drftpd.event.listeners." 181 + conn.getRequest().getArgument()) 182 || ftpListener.getClass().getName().equals( 183 conn.getRequest().getArgument())) { 184 try { 185 ftpListener.unload(); 186 } catch (RuntimeException e) { 187 return new FtpReply(200, "Exception unloading plugin, plugin removed"); 188 } 189 iter.remove(); 190 return new FtpReply(200, "Successfully unloaded your plugin"); 191 } 192 } 193 return new FtpReply(500, "Could not find your plugin on the site"); 194 } 195 196 public FtpReply execute(BaseFtpConnection conn) 197 throws UnhandledCommandException { 198 String cmd = conn.getRequest().getCommand(); 199 if ("SITE RELOAD".equals(cmd)) 200 return doSITE_RELOAD(conn); 201 if ("SITE SHUTDOWN".equals(cmd)) 202 return doSITE_SHUTDOWN(conn); 203 if ("SITE LIST".equals(cmd)) 204 return doSITE_LIST(conn); 205 if ("SITE LOADPLUGIN".equals(cmd)) 206 return doSITE_LOADPLUGIN(conn); 207 if ("SITE UNLOADPLUGIN".equals(cmd)) 208 return doSITE_UNLOADPLUGIN(conn); 209 if ("SITE PLUGINS".equals(cmd)) 210 return doSITE_PLUGINS(conn); 211 throw UnhandledCommandException.create( 212 SiteManagment.class, 213 conn.getRequest()); 214 } 215 216 public String [] getFeatReplies() { 217 return null; 218 } 219 private FtpListener getFtpListener(String arg) { 220 FtpListener ftpListener = null; 221 try { 222 ftpListener = 223 (FtpListener) Class 224 .forName("net.sf.drftpd.event.listeners." + arg) 225 .newInstance(); 226 } catch (InstantiationException e) { 227 logger.error( 228 "Was not able to create an instance of the class, did not load", 229 e); 230 return null; 231 } catch (IllegalAccessException e) { 232 logger.error("This will not happen, I do not exist", e); 233 return null; 234 } catch (ClassNotFoundException e) { 235 } 236 if (ftpListener == null) { 237 try { 238 ftpListener = (FtpListener) Class.forName(arg).newInstance(); 239 } catch (InstantiationException e) { 240 logger.error( 241 "Was not able to create an instance of the class, did not load", 242 e); 243 return null; 244 } catch (IllegalAccessException e) { 245 logger.error("This will not happen, I do not exist", e); 246 return null; 247 } catch (ClassNotFoundException e) { 248 return null; 249 } 250 } 251 return ftpListener; 252 } 253 public CommandHandler initialize( 254 BaseFtpConnection conn, 255 CommandManager initializer) { 256 return this; 257 } 258 public void load(CommandManagerFactory initializer) { 259 } 260 public void unload() { 261 } 262 263 } 264 | Popular Tags |