KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import net.sf.drftpd.Bytes;
27 import net.sf.drftpd.event.DirectoryFtpEvent;
28 import net.sf.drftpd.master.BaseFtpConnection;
29 import net.sf.drftpd.master.FtpReply;
30 import net.sf.drftpd.master.FtpRequest;
31 import net.sf.drftpd.master.command.CommandManager;
32 import net.sf.drftpd.master.command.CommandManagerFactory;
33 import net.sf.drftpd.master.usermanager.NoSuchUserException;
34 import net.sf.drftpd.master.usermanager.User;
35 import net.sf.drftpd.master.usermanager.UserFileException;
36 import net.sf.drftpd.remotefile.LinkedRemoteFile;
37 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
38
39 import org.apache.log4j.Level;
40 import org.apache.log4j.Logger;
41 import org.drftpd.commands.CommandHandler;
42 import org.drftpd.commands.CommandHandlerFactory;
43 import org.drftpd.commands.UnhandledCommandException;
44 import org.drftpd.sections.SectionInterface;
45
46 /**
47  * @author mog
48  *
49  * @version $Id: Pre.java,v 1.15.2.1 2004/06/19 23:37:26 mog Exp $
50  */

51 public class Pre implements CommandHandlerFactory, CommandHandler {
52
53     private static final Logger logger = Logger.getLogger(Pre.class);
54
55     /**
56      * Syntax: SITE PRE <RELEASEDIR> [SECTION]
57      * @param request
58      * @param out
59      */

60     public FtpReply execute(BaseFtpConnection conn)
61         throws UnhandledCommandException {
62         FtpRequest request = conn.getRequest();
63         if (!"SITE PRE".equals(request.getCommand()))
64             throw UnhandledCommandException.create(Pre.class, request);
65         if (!request.hasArgument())
66             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
67
68         String JavaDoc args[] = request.getArgument().split(" ");
69         if (args.length != 2) {
70             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
71         }
72
73         SectionInterface section = conn.getConnectionManager().getSectionManager().getSection(args[1]);
74         
75         if(section.getName().equals("")) {
76             return new FtpReply(200, "Invalid section, see SITE SECTIONS for a list of available sections");
77         }
78
79         LinkedRemoteFileInterface preDir;
80         try {
81             preDir = conn.getCurrentDirectory().lookupFile(args[0]);
82         } catch (FileNotFoundException JavaDoc e) {
83             return FtpReply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
84         }
85         if (!conn
86             .getConfig()
87             .checkPathPermission("pre", conn.getUserNull(), preDir)) {
88             return FtpReply.RESPONSE_530_ACCESS_DENIED;
89         }
90
91         FtpReply response = new FtpReply(200);
92
93         //AWARD CREDITS
94
Hashtable JavaDoc awards = new Hashtable JavaDoc();
95         preAwardCredits(conn, preDir, awards);
96         for (Iterator JavaDoc iter = awards.entrySet().iterator(); iter.hasNext();) {
97             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
98             User owner = (User) entry.getKey();
99             if (conn.getConfig().getCreditCheckRatio(preDir, owner) == 0) {
100                 Long JavaDoc award = (Long JavaDoc) entry.getValue();
101                 owner.updateCredits(award.longValue());
102                 response.addComment(
103                     "Awarded "
104                         + Bytes.formatBytes(award.longValue())
105                         + " to "
106                         + owner.getUsername());
107             }
108         }
109
110         //RENAME
111
LinkedRemoteFile toDir;
112         try {
113             toDir = preDir.renameTo(section.getPath(), preDir.getName());
114         } catch (IOException JavaDoc ex) {
115             logger.warn("", ex);
116             return new FtpReply(200, ex.getMessage());
117         }
118
119         //ANNOUNCE
120
conn.getConnectionManager().dispatchFtpEvent(
121             new DirectoryFtpEvent(conn.getUserNull(), "PRE", toDir));
122
123         return FtpReply.RESPONSE_200_COMMAND_OK;
124     }
125
126     public String JavaDoc[] getFeatReplies() {
127         return null;
128     }
129
130     public CommandHandler initialize(
131         BaseFtpConnection conn,
132         CommandManager initializer) {
133         return this;
134     }
135     public void load(CommandManagerFactory initializer) {
136     }
137
138     private void preAwardCredits(
139         BaseFtpConnection conn,
140         LinkedRemoteFileInterface preDir,
141         Hashtable JavaDoc awards) {
142         for (Iterator JavaDoc iter = preDir.getFiles().iterator(); iter.hasNext();) {
143             LinkedRemoteFileInterface file =
144                 (LinkedRemoteFileInterface) iter.next();
145             User owner;
146             try {
147                 owner = conn.getConnectionManager().getUserManager().getUserByName(file.getUsername());
148             } catch (NoSuchUserException e) {
149                 logger.log(
150                     Level.INFO,
151                     "PRE: Cannot award credits to non-existing user",
152                     e);
153                 continue;
154             } catch (UserFileException e) {
155                 logger.log(Level.WARN, "", e);
156                 continue;
157             }
158             Long JavaDoc total = (Long JavaDoc) awards.get(owner);
159             if (total == null)
160                 total = new Long JavaDoc(0);
161             total =
162                 new Long JavaDoc(
163                     total.longValue()
164                         + (long) (file.length() * owner.getRatio()));
165             awards.put(owner, total);
166         }
167     }
168     public void unload() {
169     }
170
171 }
172
Popular Tags