KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
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 /**
40  * @author mog
41  *
42  * @version $Id: SlaveManagment.java,v 1.11.2.1 2004/06/19 23:37:26 mog Exp $
43  */

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     /** Lists all slaves used by the master
82      * USAGE: SITE SLAVES
83      *
84      */

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 JavaDoc slaves = conn.getSlaveManager().getSlaves();
96         FtpReply response =
97             new FtpReply(200, "OK, " + slaves.size() + " slaves listed.");
98
99         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
176      * @see net.sf.drftpd.master.command.CommandHandler#initialize(net.sf.drftpd.master.BaseFtpConnection)
177      */

178     public CommandHandler initialize(
179         BaseFtpConnection conn,
180         CommandManager initializer) {
181         return this;
182     }
183
184     public String JavaDoc[] getFeatReplies() {
185         return null;
186     }
187 }
188
Popular Tags