KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slaveselection > filter > MatchdirFilter


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.slaveselection.filter;
19
20 import java.net.InetAddress JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
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 /**
38  * Example slaveselection entry:
39  * <pre>
40  * <n>.filter=matchdir
41  * <n>.assignslave=<slavename>+100000
42  * <n>.match=<path glob match>
43  * </pre>
44  *
45  * @author mog
46  * @version $Id: MatchdirFilter.java,v 1.4 2004/03/30 13:59:49 mog Exp $
47  */

48 public class MatchdirFilter extends Filter {
49     private ArrayList JavaDoc _assigns;
50     private FilterChain _ssm;
51     private Pattern _p;
52     private Perl5Matcher _m = new Perl5Matcher();
53
54     public MatchdirFilter(FilterChain ssm, int i, Properties JavaDoc 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 JavaDoc e) {
62             if (e instanceof RuntimeException JavaDoc)
63                 throw (RuntimeException JavaDoc) e;
64             throw new RuntimeException JavaDoc(e);
65         }
66     }
67
68     private static class AssignSlave {
69         private RemoteSlave _rslave;
70         private long _score;
71         public AssignSlave(String JavaDoc 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 JavaDoc(
81                         s + " is not a valid assign slave expression");
82                 isAdd = false;
83             }
84
85             String JavaDoc slavename = s.substring(0, pos);
86             if (!slavename.equalsIgnoreCase("all")) {
87                 _rslave = slaveManager.getSlave(slavename);
88             }
89             String JavaDoc 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 JavaDoc assign) throws ObjectNotFoundException {
111         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(assign, ", ");
112         ArrayList JavaDoc assigns = new ArrayList JavaDoc();
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 JavaDoc iter = _assigns.iterator(); iter.hasNext();) {
121             AssignSlave assign = (AssignSlave) iter.next();
122             if (assign.isAll()) {
123                 for (Iterator JavaDoc 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                         //not in scorechart, this is OK
142
} catch (ObjectNotFoundException e) {}
143                     }
144             }
145         }
146     }
147     public void process(
148         ScoreChart scorechart,
149         User user,
150         InetAddress JavaDoc source,
151         char direction,
152         LinkedRemoteFileInterface file) {
153         if (_m.matches(file.getPath(), _p)) {
154             doAssign(scorechart);
155         }
156     }
157 }
158
Popular Tags