1 18 package org.drftpd.commands; 19 20 import java.io.FileNotFoundException ; 21 import java.lang.reflect.Constructor ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.StringTokenizer ; 25 26 import net.sf.drftpd.ObjectNotFoundException; 27 import net.sf.drftpd.master.BaseFtpConnection; 28 import net.sf.drftpd.master.FtpReply; 29 import net.sf.drftpd.master.RemoteSlave; 30 import net.sf.drftpd.master.command.CommandHandlerBundle; 31 import net.sf.drftpd.master.command.CommandManager; 32 import net.sf.drftpd.master.command.CommandManagerFactory; 33 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 34 35 import org.apache.log4j.Logger; 36 import org.drftpd.mirroring.ArchiveHandler; 37 import org.drftpd.mirroring.ArchiveType; 38 import org.drftpd.sections.SectionInterface; 39 import org.tanesha.replacer.ReplacerEnvironment; 40 41 45 public class ArchiveCommandHandler implements CommandHandlerBundle { 46 47 public ArchiveCommandHandler() { 48 super(); 49 } 50 private static final Logger logger = Logger.getLogger(ArchiveCommandHandler.class); 51 52 public FtpReply execute(BaseFtpConnection conn) 53 throws UnhandledCommandException { 54 String cmd = conn.getRequest().getCommand(); 55 if ("SITE LISTARCHIVETYPES".equals(cmd)) { 56 return doLISTARCHIVETYPES(conn); 57 } 58 if ("SITE ARCHIVE".equals(cmd)) { 59 return doARCHIVE(conn); 60 } 61 throw UnhandledCommandException.create( 62 ArchiveCommandHandler.class, 63 conn.getRequest()); 64 } 65 66 private FtpReply doARCHIVE(BaseFtpConnection conn) { 67 FtpReply reply = new FtpReply(200); 68 ReplacerEnvironment env = new ReplacerEnvironment(); 69 if (!conn.getRequest().hasArgument()) { 70 reply.addComment(conn.jprintf(ArchiveCommandHandler.class, "archive.usage", env)); 71 return reply; 72 } 73 StringTokenizer st = 74 new StringTokenizer (conn.getRequest().getArgument()); 75 String dirname = st.nextToken(); 76 LinkedRemoteFileInterface lrf; 77 try { 78 lrf = conn.getCurrentDirectory().getFile(dirname); 79 } catch (FileNotFoundException e1) { 80 try { 81 lrf = conn.getConnectionManager().getRoot().lookupFile(dirname); 82 } catch (FileNotFoundException e2) { 83 reply.addComment( 84 conn.jprintf(ArchiveCommandHandler.class, "archive.usage", env)); 85 env.add("dirname", dirname); 86 reply.addComment( 87 conn.jprintf(ArchiveCommandHandler.class, "archive.baddir", env)); 88 return reply; 89 } 90 } 91 net.sf.drftpd.event.listeners.Archive archive; 92 try { 93 archive = 94 (net.sf.drftpd.event.listeners.Archive) conn 95 .getConnectionManager() 96 .getFtpListener(net.sf.drftpd.event.listeners.Archive.class); 97 } catch (ObjectNotFoundException e3) { 98 reply.addComment( 99 conn.jprintf(ArchiveCommandHandler.class, "archive.loadarchive", env)); 100 return reply; 101 } 102 String archiveTypeName = null; 103 ArchiveType archiveType = null; 104 SectionInterface section = 105 conn.getConnectionManager().getSectionManager().lookup( 106 lrf.getPath()); 107 if (st.hasMoreTokens()) { 108 archiveTypeName = st.nextToken(); 109 Class [] classParams = {net.sf.drftpd.event.listeners.Archive.class, SectionInterface.class}; 110 Constructor constructor = null; 111 try { 112 constructor = Class.forName( 113 "org.drftpd.mirroring.archivetypes." + archiveTypeName).getConstructor(classParams); 114 } catch (Exception e1) { 115 logger.debug("Unable to load ArchiveType for section " + section.getName(), e1); 116 reply.addComment(conn.jprintf(ArchiveCommandHandler.class, "archive.badarchivetype", env)); 117 return reply; 118 } 119 Object [] objectParams = { archive, section }; 120 try { 121 archiveType = (ArchiveType) constructor.newInstance(objectParams); 122 } catch (Exception e2) { 123 logger.debug("Unable to load ArchiveType for section " + section.getName(), e2); 124 reply.addComment(conn.jprintf(ArchiveCommandHandler.class, "archive.badarchivetype", env)); 125 return reply; 126 } 127 } 128 if (archiveType == null) { 129 archiveType = archive.getArchiveType(section); 130 } 131 if (archiveTypeName == null) { 132 archiveTypeName = archiveType.getClass().getName(); 133 } 134 HashSet slaveSet = new HashSet (); 135 synchronized(archiveType) { 136 if (archiveType.isBusy()) { 137 env.add("section", section.getName()); 138 reply.addComment( 139 conn.jprintf(ArchiveCommandHandler.class, "archive.wait1", env)); 140 reply.addComment( 141 conn.jprintf(ArchiveCommandHandler.class, "archive.wait2", env)); 142 return reply; 143 } 144 while (st.hasMoreTokens()) { 145 String slavename = st.nextToken(); 146 try { 147 RemoteSlave rslave = 148 conn.getConnectionManager().getSlaveManager().getSlave( 149 slavename); 150 slaveSet.add(rslave); 151 } catch (ObjectNotFoundException e2) { 152 env.add("slavename", slavename); 153 reply.addComment( 154 conn.jprintf(ArchiveCommandHandler.class, "archive.badslave", env)); 155 } 156 } 157 archiveType.setDirectory(lrf); 158 } 159 if (!slaveSet.isEmpty()) 160 archiveType.setRSlaves(slaveSet); 161 ArchiveType usedArchiveType = ArchiveHandler.getArchiveTypeForDirectory(archiveType.getDirectory()); 162 if (usedArchiveType != null) { 163 env.add("archivetype",usedArchiveType); 164 reply.addComment(conn.jprintf(ArchiveCommandHandler.class, "archive.fail", env)); 165 return reply; 166 } 167 ArchiveHandler archiveHandler = new ArchiveHandler(archiveType); 168 archiveHandler.start(); 169 env.add("dirname", lrf.getPath()); 170 env.add("archivetypename", archiveTypeName); 171 reply.addComment(conn.jprintf(ArchiveCommandHandler.class, "archive.success", env)); 172 return reply; 173 } 174 175 private FtpReply doLISTARCHIVETYPES(BaseFtpConnection conn) { 176 FtpReply reply = new FtpReply(200); 177 int x = 0; 178 for (Iterator iter = ArchiveHandler.getArchiveHandlers().iterator(); iter.hasNext(); x++) { 179 ArchiveHandler archiveHandler = (ArchiveHandler) iter.next(); 180 reply.addComment(x + ". " + archiveHandler.getArchiveType()); 181 } 182 return reply; 183 } 184 185 public String [] getFeatReplies() { 186 return null; 187 } 188 189 public CommandHandler initialize( 190 BaseFtpConnection conn, 191 CommandManager initializer) { 192 return this; 193 } 194 195 public void load(CommandManagerFactory initializer) { 196 } 197 198 public void unload() { 199 } 200 201 } 202 | Popular Tags |