1 17 package net.sf.drftpd.event.listeners; 18 import java.io.FileInputStream ; 19 import java.io.IOException ; 20 import java.lang.reflect.Constructor ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Properties ; 26 import net.sf.drftpd.event.Event; 27 import net.sf.drftpd.event.FtpListener; 28 import net.sf.drftpd.master.ConnectionManager; 29 import net.sf.drftpd.master.config.FtpConfig; 30 31 import org.apache.log4j.Logger; 32 import org.drftpd.mirroring.ArchiveHandler; 33 import org.drftpd.mirroring.ArchiveType; 34 import org.drftpd.sections.SectionInterface; 35 39 public class Archive implements FtpListener, Runnable { 40 private Properties _props; 41 private static final Logger logger = Logger.getLogger(Archive.class); 42 public static Logger getLogger() { 43 return logger; 44 } 45 private HashMap _archiveTypes; 46 private ConnectionManager _cm; 47 private long _cycleTime; 48 private ArrayList _exemptList = new ArrayList (); 49 private boolean _isStopped = false; 50 private Thread thread = null; 51 private ArrayList _archiveHandlers; 52 public Archive() { 53 logger.info("Archive plugin loaded successfully"); 54 _archiveHandlers = new ArrayList (); 55 } 56 public Properties getProperties() { 57 return _props; 58 } 59 public void actionPerformed(Event event) { 60 if (event.getCommand().equals("RELOAD")) { 61 reload(); 62 return; 63 } 64 } 65 69 public boolean checkExclude(SectionInterface section) { 70 return _exemptList.contains(section.getName()); 71 } 72 75 public ArchiveType getArchiveType(SectionInterface section) { 76 ArchiveType archiveType = (ArchiveType) _archiveTypes.get(section); 77 if (archiveType == null) 78 throw new IllegalStateException ( 79 "Could not find an archive type for " 80 + section.getName() 81 + ", check you make sure default.archiveType is defined in archive.conf and the section is not excluded"); 82 return archiveType; 83 } 84 87 public ConnectionManager getConnectionManager() { 88 return _cm; 89 } 90 93 public long getCycleTime() { 94 return _cycleTime; 95 } 96 public void init(ConnectionManager connectionManager) { 97 _cm = connectionManager; 98 _cm.loadJobManager(); 99 reload(); 100 startArchive(); 101 } 102 private boolean isStopped() { 103 return _isStopped; 104 } 105 private void reload() { 106 _props = new Properties (); 107 try { 108 _props.load(new FileInputStream ("conf/archive.conf")); 109 } catch (IOException e) { 110 throw new RuntimeException (e); 111 } 112 _cycleTime = 60000 * Long.parseLong(FtpConfig.getProperty(_props, 113 "cycleTime")); 114 _exemptList = new ArrayList (); 115 for (int i = 1;; i++) { 116 String path = _props.getProperty("exclude." + i); 117 if (path == null) 118 break; 119 _exemptList.add(path); 120 } 121 _archiveTypes = new HashMap (); 122 Class [] classParams = {Archive.class, SectionInterface.class}; 123 for (Iterator iter = getConnectionManager().getSectionManager() 124 .getSections().iterator(); iter.hasNext();) { 125 SectionInterface section = (SectionInterface) iter.next(); 126 if (checkExclude(section)) 127 continue; 130 ArchiveType archiveType = null; 131 String name = null; 132 try { 133 name = FtpConfig.getProperty(_props, section.getName() 134 + ".archiveType"); 135 } catch (NullPointerException e) { 136 name = FtpConfig.getProperty(_props, "default.archiveType"); 137 } 138 Constructor constructor = null; 139 try { 140 constructor = Class.forName( 141 "org.drftpd.mirroring.archivetypes." 142 + name).getConstructor(classParams); 143 } catch (Exception e1) { 144 throw new RuntimeException ("Unable to load ArchiveType for section " + section.getName(), e1); 145 } 146 Object [] objectParams = { this, section }; 147 try { 148 archiveType = (ArchiveType) constructor.newInstance(objectParams); 149 } catch (Exception e2) { 150 throw new RuntimeException ("Unable to load ArchiveType for section " + section.getName(), e2); 151 } 152 _archiveTypes.put(section, archiveType); 153 logger.debug("added archiveType for section " + section.getName()); 154 } 155 } 156 public void run() { 157 while (true) { 158 if (isStopped()) { 159 logger.debug("Stopping ArchiveStarter thread"); 160 return; 161 } 162 for (Iterator iter = _archiveHandlers.iterator(); iter.hasNext();) { 163 ArchiveHandler archiveHandler = (ArchiveHandler) iter.next(); 164 if (!archiveHandler.isAlive()) { 165 iter.remove(); 166 } 167 } 168 Collection sectionsToCheck = getConnectionManager() 169 .getSectionManager().getSections(); 170 for (Iterator iter = sectionsToCheck.iterator(); iter.hasNext();) { 171 SectionInterface section = (SectionInterface) iter.next(); 172 if (checkExclude(section)) 173 continue; 174 ArchiveType archiveType = getArchiveType(section); 175 if (archiveType.isBusy()) continue; ArchiveHandler archiveHandler = new ArchiveHandler(archiveType); 178 archiveHandler.start(); 179 } 180 try { 181 Thread.sleep(_cycleTime); 182 } catch (InterruptedException e) { 183 } 184 } 185 } 186 public void startArchive() { 187 if (thread != null) { 188 stopArchive(); 189 thread.interrupt(); 190 while (thread.isAlive()) { 191 Thread.yield(); 192 } 193 } 194 _isStopped = false; 195 thread = new Thread (this, "ArchiveStarter"); 196 thread.start(); 197 } 198 public void stopArchive() { 199 _isStopped = true; 200 } 201 public void unload() { 202 stopArchive(); 203 } 204 } | Popular Tags |