KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > plugins > SiteManagment


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.master.command.plugins;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.lang.reflect.Field JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
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 /**
45  * @author mog
46  * @author zubov
47  * @version $Id: SiteManagment.java,v 1.19.2.1 2004/06/19 23:37:26 mog Exp $
48  */

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         //.getMap().values() to get the .isDeleted files as well.
58
LinkedRemoteFile dir = conn.getCurrentDirectory();
59         if (conn.getRequest().hasArgument()) {
60             try {
61                 dir = dir.lookupFile(conn.getRequest().getArgument(), true);
62             } catch (FileNotFoundException JavaDoc e) {
63                 logger.debug("", e);
64                 return new FtpReply(200, e.getMessage());
65             }
66         }
67         ArrayList JavaDoc files = new ArrayList JavaDoc(dir.getMap().values());
68         Collections.sort(files);
69         for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
70             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
71             //if (!key.equals(file.getName()))
72
// response.addComment(
73
// "WARN: " + key + " not equals to " + file.getName());
74
//response.addComment(key);
75
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 JavaDoc 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 JavaDoc e1) {
124                 // not loaded, don't reload
125
}
126             conn.getConnectionManager().getCommandManagerFactory().reload();
127             //slaveManager.saveFilesXML();
128
} catch (IOException JavaDoc 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         //ugly hack to clear resourcebundle cache
136
//see http://developer.java.sun.com/developer/bugParade/bugs/4212439.html
137
try {
138             Field JavaDoc cacheList =
139                 ResourceBundle JavaDoc.class.getDeclaredField("cacheList");
140             cacheList.setAccessible(true);
141             ((Map JavaDoc) cacheList.get(ResourceBundle JavaDoc.class)).clear();
142             cacheList.setAccessible(false);
143         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] getFeatReplies() {
217         return null;
218     }
219     private FtpListener getFtpListener(String JavaDoc 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 JavaDoc 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 JavaDoc e) {
232             logger.error("This will not happen, I do not exist", e);
233             return null;
234         } catch (ClassNotFoundException JavaDoc e) {
235         }
236         if (ftpListener == null) {
237             try {
238                 ftpListener = (FtpListener) Class.forName(arg).newInstance();
239             } catch (InstantiationException JavaDoc 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 JavaDoc e) {
245                 logger.error("This will not happen, I do not exist", e);
246                 return null;
247             } catch (ClassNotFoundException JavaDoc 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