KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > commands > New


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 org.drftpd.commands;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Comparator JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26 import net.sf.drftpd.master.BaseFtpConnection;
27 import net.sf.drftpd.master.FtpReply;
28 import net.sf.drftpd.master.command.plugins.Textoutput;
29 import net.sf.drftpd.master.command.CommandHandlerBundle;
30 import net.sf.drftpd.master.command.CommandManager;
31 import net.sf.drftpd.master.command.CommandManagerFactory;
32 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
33 import net.sf.drftpd.Bytes;
34
35 import org.apache.log4j.Logger;
36 import org.drftpd.sections.SectionInterface;
37 import org.tanesha.replacer.ReplacerEnvironment;
38
39 /**
40  * @version $Id: New.java,v 1.3 2004/06/01 15:40:34 mog Exp $
41  * @author zubov
42  */

43 public class New implements CommandHandlerBundle {
44     private static final Logger logger = Logger.getLogger(New.class);
45
46     private class DateComparator implements Comparator JavaDoc {
47         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
48             if (!(o1 instanceof LinkedRemoteFileInterface
49                 && o2 instanceof LinkedRemoteFileInterface)) {
50                 throw new ClassCastException JavaDoc("Not a LinkedRemoteFile");
51             }
52
53             LinkedRemoteFileInterface f1 = (LinkedRemoteFileInterface) o1;
54             LinkedRemoteFileInterface f2 = (LinkedRemoteFileInterface) o2;
55
56             if (f1.lastModified() == f2.lastModified()) {
57                 return 0;
58             }
59
60             return f1.lastModified() > f2.lastModified() ? -1 : 1;
61         }
62
63         public boolean equals(Object JavaDoc o) {
64             if (!(o instanceof DateComparator)) {
65                 return false;
66             }
67
68             return super.equals(o);
69         }
70     }
71
72     public New() {
73         super();
74     }
75
76     public FtpReply execute(BaseFtpConnection conn)
77         throws UnhandledCommandException {
78         FtpReply reply = new FtpReply(200);
79         Collection JavaDoc sections =
80             conn.getConnectionManager().getSectionManager().getSections();
81         int count = 20;
82         try {
83             count = Integer.parseInt(conn.getRequest().getArgument());
84         } catch (Exception JavaDoc e) {
85             // Ignore and output the 20 freshest...
86
}
87
88         // Collect all new files (= directories hopefully!) from all sections,
89
// and sort them.
90

91         Collection JavaDoc directories = new TreeSet JavaDoc(new DateComparator());
92         for (Iterator JavaDoc iter = sections.iterator(); iter.hasNext();) {
93             SectionInterface section = (SectionInterface) iter.next();
94             directories.addAll(section.getFile().getDirectories());
95         }
96
97         try {
98             Textoutput.addTextToResponse(reply, "new_header");
99         } catch (IOException JavaDoc ioe) {
100             logger.warn("Error reading new_header", ioe);
101         }
102
103         // Print the reply!
104
ReplacerEnvironment env = new ReplacerEnvironment();
105         int pos = 1;
106         long now = System.currentTimeMillis();
107         for (Iterator JavaDoc iter = directories.iterator();
108             iter.hasNext() && pos <= count;
109             pos++) {
110             LinkedRemoteFileInterface dir =
111                 (LinkedRemoteFileInterface) iter.next();
112             env.add("pos", "" + pos);
113             env.add("name", dir.getName());
114             env.add("diruser", dir.getUsername());
115             env.add("files", "" + dir.dirSize());
116             env.add("size", Bytes.formatBytes(dir.length()));
117             env.add("age", "" + formatAge(dir.lastModified(), now));
118             reply.addComment(conn.jprintf(New.class, "new", env));
119         }
120
121         try {
122             Textoutput.addTextToResponse(reply, "new_footer");
123         } catch (IOException JavaDoc ioe) {
124             logger.warn("Error reading new_footer", ioe);
125         }
126
127         return reply;
128     }
129
130     private static final String JavaDoc formatAge(long age, long now) {
131         if (age >= now) {
132             return "0m 0s";
133         }
134
135         // Less than an hour...
136
if (now - age < 60 * 60 * 1000) {
137             long min = (now - age) / 60000;
138             long s = ((now - age) - min * 60000) / 1000;
139             return min + "m " + (s > 9 ? "" + s : " " + s) + "s";
140         }
141
142         // Less than a day...
143
if (now - age < 24 * 60 * 60 * 1000) {
144             long h = (now - age) / (60 * 60000);
145             long min = ((now - age) - h * 60 * 60000) / 60000;
146             return h + "h " + (min > 9 ? "" + min : " " + min) + "m";
147         }
148
149         // Over a day...
150
long d = (now - age) / (24 * 60 * 60000);
151         long h = ((now - age) - d * 24 * 60 * 60000) / (60 * 60000);
152         return d + "d " + (h > 9 ? "" + h : " " + h) + "h";
153     }
154
155     public CommandHandler initialize(
156         BaseFtpConnection conn,
157         CommandManager initializer) {
158         return this;
159     }
160
161     public String JavaDoc[] getFeatReplies() {
162         return null;
163     }
164
165     public void load(CommandManagerFactory initializer) {
166     }
167
168     public void unload() {
169     }
170
171 }
172
Popular Tags