1 18 package org.drftpd.slaveselection.filter; 19 20 import java.net.InetAddress ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Properties ; 24 import java.util.StringTokenizer ; 25 26 import net.sf.drftpd.ObjectNotFoundException; 27 import net.sf.drftpd.master.RemoteSlave; 28 import net.sf.drftpd.master.SlaveManagerImpl; 29 import net.sf.drftpd.master.config.FtpConfig; 30 import net.sf.drftpd.master.usermanager.User; 31 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 32 33 import org.apache.oro.text.GlobCompiler; 34 import org.apache.oro.text.regex.Pattern; 35 import org.apache.oro.text.regex.Perl5Matcher; 36 37 48 public class MatchdirFilter extends Filter { 49 private ArrayList _assigns; 50 private FilterChain _ssm; 51 private Pattern _p; 52 private Perl5Matcher _m = new Perl5Matcher(); 53 54 public MatchdirFilter(FilterChain ssm, int i, Properties p) { 55 _ssm = ssm; 56 try { 57 parseAssign(FtpConfig.getProperty(p, i + ".assign")); 58 _p = 59 new GlobCompiler().compile( 60 FtpConfig.getProperty(p, i + ".match")); 61 } catch (Exception e) { 62 if (e instanceof RuntimeException ) 63 throw (RuntimeException ) e; 64 throw new RuntimeException (e); 65 } 66 } 67 68 private static class AssignSlave { 69 private RemoteSlave _rslave; 70 private long _score; 71 public AssignSlave(String s, SlaveManagerImpl slaveManager) 72 throws ObjectNotFoundException { 73 boolean isAdd; 74 int pos = s.indexOf("+"); 75 if (pos != -1) { 76 isAdd = true; 77 } else { 78 pos = s.indexOf("-"); 79 if (pos == -1) 80 throw new IllegalArgumentException ( 81 s + " is not a valid assign slave expression"); 82 isAdd = false; 83 } 84 85 String slavename = s.substring(0, pos); 86 if (!slavename.equalsIgnoreCase("all")) { 87 _rslave = slaveManager.getSlave(slavename); 88 } 89 String assign = s.substring(pos+1); 90 if(assign.equals("remove")) { 91 _score = 0; 92 isAdd = false; 93 } else { 94 _score = Long.parseLong(s.substring(pos + 1)); 95 if (!isAdd) 96 _score = -_score; 97 } 98 } 99 100 public RemoteSlave getRSlave() { 101 return _rslave; 102 } 103 public long getScore() { 104 return _score; 105 } 106 public boolean isAll() { 107 return _rslave == null; 108 } 109 } 110 private void parseAssign(String assign) throws ObjectNotFoundException { 111 StringTokenizer st = new StringTokenizer (assign, ", "); 112 ArrayList assigns = new ArrayList (); 113 while (st.hasMoreTokens()) { 114 assigns.add( 115 new AssignSlave(st.nextToken(), _ssm.getSlaveManager())); 116 } 117 _assigns = assigns; 118 } 119 private void doAssign(ScoreChart scorechart) { 120 for (Iterator iter = _assigns.iterator(); iter.hasNext();) { 121 AssignSlave assign = (AssignSlave) iter.next(); 122 if (assign.isAll()) { 123 for (Iterator iterator = scorechart.getSlaveScores().iterator(); 124 iterator.hasNext(); 125 ) { 126 ScoreChart.SlaveScore score = 127 (ScoreChart.SlaveScore) iterator.next(); 128 if (assign.getScore() == 0) { 129 iterator.remove(); 130 } else { 131 score.addScore(assign.getScore()); 132 } 133 } 134 } else { 135 if(assign.getScore() == 0) { 136 scorechart.removeSlaveScore(assign.getRSlave()); 137 } else { 138 try { 139 scorechart.getSlaveScore(assign.getRSlave()).addScore( 140 assign.getScore()); 141 } catch (ObjectNotFoundException e) {} 143 } 144 } 145 } 146 } 147 public void process( 148 ScoreChart scorechart, 149 User user, 150 InetAddress source, 151 char direction, 152 LinkedRemoteFileInterface file) { 153 if (_m.matches(file.getPath(), _p)) { 154 doAssign(scorechart); 155 } 156 } 157 } 158 | Popular Tags |