1 18 package org.drftpd.slaveselection.filter; 19 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.net.InetAddress ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 28 import org.drftpd.slaveselection.SlaveSelectionManagerInterface; 29 30 import net.sf.drftpd.FatalException; 31 import net.sf.drftpd.NoAvailableSlaveException; 32 import net.sf.drftpd.master.RemoteSlave; 33 import net.sf.drftpd.master.SlaveManagerImpl; 34 import net.sf.drftpd.master.usermanager.User; 35 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 36 37 41 public class FilterChain { 42 private String _cfgfileName; 43 private ArrayList _filters; 44 private SlaveSelectionManagerInterface _sm; 45 46 protected FilterChain() { 47 } 48 49 public FilterChain(SlaveSelectionManagerInterface sm, Properties p) { 50 _sm = sm; 51 reload(p); 52 } 53 54 public FilterChain(SlaveSelectionManagerInterface sm, String cfgFileName) 55 throws FileNotFoundException , IOException { 56 _sm = sm; 57 _cfgfileName = cfgFileName; 58 reload(); 59 } 60 61 public RemoteSlave getBestSlave( 62 ScoreChart sc, 63 User user, 64 InetAddress peer, 65 char direction, 66 LinkedRemoteFileInterface file) 67 throws NoAvailableSlaveException { 68 for (Iterator iter = _filters.iterator(); iter.hasNext();) { 69 Filter filter = (Filter) iter.next(); 70 filter.process(sc, user, peer, direction, file); 71 } 72 RemoteSlave rslave = sc.getBestSlave(); 73 rslave.setLastDirection(direction, System.currentTimeMillis()); 74 if (rslave == null) throw new NoAvailableSlaveException("This is not supposed to be thrown"); 75 return rslave; 76 } 77 78 public void reload() throws FileNotFoundException , IOException { 79 Properties p = new Properties (); 80 p.load(new FileInputStream (_cfgfileName)); 81 reload(p); 82 } 83 84 public void reload(Properties p) { 85 ArrayList filters = new ArrayList (); 86 int i = 1; 87 for (;; i++) { 88 String type = p.getProperty(i + ".filter"); 89 if (type == null) 90 break; 91 if (type.indexOf('.') == -1) { 92 type = 93 "org.drftpd.slaveselection.filter." 94 + type.substring(0, 1).toUpperCase() 95 + type.substring(1) 96 + "Filter"; 97 } 98 try { 99 Class [] SIG = 100 new Class [] { 101 FilterChain.class, 102 int.class, 103 Properties .class }; 104 105 Filter filter = 106 (Filter) Class.forName(type).getConstructor( 107 SIG).newInstance( 108 new Object [] { this, new Integer (i), p }); 109 filters.add(filter); 110 } catch (Exception e) { 111 throw new FatalException(i + ".filter = " + type, e); 112 } 113 } 114 if (i == 1) 115 throw new IllegalArgumentException (); 116 filters.trimToSize(); 117 _filters = filters; 118 } 119 120 public SlaveManagerImpl getSlaveManager() { 121 return _sm.getSlaveManager(); 122 } 123 } 124 | Popular Tags |