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.slaveselection.filter;
019:
020: import java.net.InetAddress;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.Properties;
024: import java.util.StringTokenizer;
025:
026: import net.sf.drftpd.ObjectNotFoundException;
027:
028: import org.apache.oro.text.GlobCompiler;
029: import org.apache.oro.text.regex.Pattern;
030: import org.apache.oro.text.regex.Perl5Matcher;
031: import org.drftpd.PropertyHelper;
032: import org.drftpd.master.RemoteSlave;
033: import org.drftpd.master.SlaveManager;
034: import org.drftpd.remotefile.LinkedRemoteFileInterface;
035: import org.drftpd.usermanager.User;
036:
037: /**
038: * Example slaveselection entry:
039: * <pre>
040: * <n>.filter=matchdir
041: * <n>.assign=<slavename>+100000
042: * <n>.match=<path glob match>
043: * </pre>
044: *
045: * @author mog
046: * @version $Id: MatchdirFilter.java 847 2004-12-02 03:32:41Z mog $
047: */
048: public class MatchdirFilter extends Filter {
049: private ArrayList<AssignSlave> _assigns;
050: private FilterChain _fc;
051: private Pattern _p;
052: private Perl5Matcher _m = new Perl5Matcher();
053:
054: public MatchdirFilter(FilterChain fc, int i, Properties p) {
055: _fc = fc;
056:
057: try {
058: _assigns = parseAssign(PropertyHelper.getProperty(p, i
059: + ".assign"), fc.getGlobalContext()
060: .getSlaveManager());
061: _p = new GlobCompiler().compile(PropertyHelper.getProperty(
062: p, i + ".match"));
063: } catch (Exception e) {
064: if (e instanceof RuntimeException) {
065: throw (RuntimeException) e;
066: }
067: throw new RuntimeException(e);
068: }
069: }
070:
071: static ArrayList<AssignSlave> parseAssign(String assign,
072: SlaveManager sm) throws ObjectNotFoundException {
073: StringTokenizer st = new StringTokenizer(assign, ", ");
074: ArrayList<AssignSlave> assigns = new ArrayList<AssignSlave>();
075:
076: while (st.hasMoreTokens()) {
077: assigns.add(new AssignSlave(st.nextToken(), sm));
078: }
079:
080: return assigns;
081: }
082:
083: static void doAssign(ArrayList<AssignSlave> assigns,
084: ScoreChart scorechart) {
085: for (AssignSlave assign : assigns) {
086: if (assign.isAll()) {
087: for (Iterator iterator = scorechart.getSlaveScores()
088: .iterator(); iterator.hasNext();) {
089: ScoreChart.SlaveScore score = (ScoreChart.SlaveScore) iterator
090: .next();
091:
092: if (assign.getScore() == 0) {
093: iterator.remove();
094: } else {
095: score.addScore(assign.getScore());
096: }
097: }
098: } else {
099: if (assign.getScore() == 0) {
100: scorechart.removeSlaveScore(assign.getRSlave());
101: } else {
102: try {
103: scorechart.getSlaveScore(assign.getRSlave())
104: .addScore(assign.getScore());
105:
106: //not in scorechart, this is OK
107: } catch (ObjectNotFoundException e) {
108: }
109: }
110: }
111: }
112: }
113:
114: public void process(ScoreChart scorechart, User user,
115: InetAddress source, char direction,
116: LinkedRemoteFileInterface file, RemoteSlave sourceSlave) {
117: if (_m.matches(file.getPath(), _p)) {
118: doAssign(_assigns, scorechart);
119: }
120: }
121:
122: static class AssignSlave {
123: private RemoteSlave _rslave;
124: private long _score;
125:
126: public AssignSlave(String s, SlaveManager slaveManager)
127: throws ObjectNotFoundException {
128: boolean isAdd;
129: int pos = s.lastIndexOf("+");
130:
131: if (pos != -1) {
132: isAdd = true;
133: } else {
134: pos = s.lastIndexOf("-");
135:
136: if (pos == -1) {
137: throw new IllegalArgumentException(s
138: + " is not a valid assign slave expression");
139: }
140:
141: isAdd = false;
142: }
143:
144: String slavename = s.substring(0, pos);
145:
146: if (!slavename.equalsIgnoreCase("all")) {
147: _rslave = slaveManager.getRemoteSlave(slavename);
148: }
149:
150: String assign = s.substring(pos + 1);
151:
152: if (assign.equals("remove")) {
153: _score = 0;
154: isAdd = false;
155: } else {
156: _score = Long.parseLong(s.substring(pos + 1));
157:
158: if (!isAdd) {
159: _score = -_score;
160: }
161: }
162: }
163:
164: public RemoteSlave getRSlave() {
165: return _rslave;
166: }
167:
168: public long getScore() {
169: return _score;
170: }
171:
172: public boolean isAll() {
173: return _rslave == null;
174: }
175: }
176: }
|