001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.commands;
019:
020: import net.sf.drftpd.master.BaseFtpConnection;
021: import net.sf.drftpd.master.command.CommandManager;
022: import net.sf.drftpd.master.command.CommandManagerFactory;
023: import net.sf.drftpd.master.command.plugins.Textoutput;
024:
025: import org.apache.log4j.Logger;
026:
027: import org.drftpd.Bytes;
028: import org.drftpd.Time;
029: import org.drftpd.remotefile.LinkedRemoteFileInterface;
030: import org.drftpd.sections.SectionInterface;
031:
032: import org.tanesha.replacer.ReplacerEnvironment;
033:
034: import java.io.IOException;
035:
036: import java.util.Collection;
037: import java.util.Comparator;
038: import java.util.Iterator;
039: import java.util.TreeSet;
040:
041: /**
042: * @version $Id: New.java 974 2005-02-07 03:29:33Z teflon $
043: * @author zubov
044: */
045: public class New implements CommandHandler, CommandHandlerFactory {
046: private static final Logger logger = Logger.getLogger(New.class);
047:
048: public New() {
049: super ();
050: }
051:
052: public Reply execute(BaseFtpConnection conn)
053: throws UnhandledCommandException {
054: Reply reply = new Reply(200);
055: Collection sections = conn.getGlobalContext()
056: .getConnectionManager().getGlobalContext()
057: .getSectionManager().getSections();
058: int count = 20;
059:
060: try {
061: count = Integer.parseInt(conn.getRequest().getArgument());
062: } catch (Exception e) {
063: // Ignore and output the 20 freshest...
064: }
065:
066: // Collect all new files (= directories hopefully!) from all sections,
067: // and sort them.
068: Collection<LinkedRemoteFileInterface> directories = new TreeSet<LinkedRemoteFileInterface>(
069: new DateComparator());
070:
071: for (Iterator iter = sections.iterator(); iter.hasNext();) {
072: SectionInterface section = (SectionInterface) iter.next();
073: directories.addAll(section.getFile().getDirectories());
074: }
075:
076: try {
077: Textoutput.addTextToResponse(reply, "new_header");
078: } catch (IOException ioe) {
079: logger.warn("Error reading new_header", ioe);
080: }
081:
082: // Print the reply!
083: ReplacerEnvironment env = new ReplacerEnvironment();
084: int pos = 1;
085:
086: for (Iterator iter = directories.iterator(); iter.hasNext()
087: && (pos <= count); pos++) {
088: LinkedRemoteFileInterface dir = (LinkedRemoteFileInterface) iter
089: .next();
090: env.add("pos", "" + pos);
091: env.add("name", dir.getName());
092: env.add("diruser", dir.getUsername());
093: env.add("files", "" + dir.dirSize());
094: env.add("size", Bytes.formatBytes(dir.length()));
095: env.add("age", ""
096: + Time.formatTime(System.currentTimeMillis()
097: - dir.lastModified()));
098: reply.addComment(conn.jprintf(New.class, "new", env));
099: }
100:
101: try {
102: Textoutput.addTextToResponse(reply, "new_footer");
103: } catch (IOException ioe) {
104: logger.warn("Error reading new_footer", ioe);
105: }
106:
107: return reply;
108: }
109:
110: public CommandHandler initialize(BaseFtpConnection conn,
111: CommandManager initializer) {
112: return this ;
113: }
114:
115: public String[] getFeatReplies() {
116: return null;
117: }
118:
119: public void load(CommandManagerFactory initializer) {
120: }
121:
122: public void unload() {
123: }
124:
125: private class DateComparator implements Comparator {
126: public int compare(Object o1, Object o2) {
127: if (!(o1 instanceof LinkedRemoteFileInterface && o2 instanceof LinkedRemoteFileInterface)) {
128: throw new ClassCastException("Not a LinkedRemoteFile");
129: }
130:
131: LinkedRemoteFileInterface f1 = (LinkedRemoteFileInterface) o1;
132: LinkedRemoteFileInterface f2 = (LinkedRemoteFileInterface) o2;
133:
134: if (f1.lastModified() == f2.lastModified()) {
135: return 0;
136: }
137:
138: return (f1.lastModified() > f2.lastModified()) ? (-1) : 1;
139: }
140:
141: public boolean equals(Object o) {
142: if (!(o instanceof DateComparator)) {
143: return false;
144: }
145:
146: return super.equals(o);
147: }
148: }
149: }
|