KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Properties JavaDoc;
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 /**
38  * @author mog
39  * @version $Id: FilterChain.java,v 1.4 2004/05/18 20:28:18 zubov Exp $
40  */

41 public class FilterChain {
42     private String JavaDoc _cfgfileName;
43     private ArrayList JavaDoc _filters;
44     private SlaveSelectionManagerInterface _sm;
45
46     protected FilterChain() {
47     }
48     
49     public FilterChain(SlaveSelectionManagerInterface sm, Properties JavaDoc p) {
50         _sm = sm;
51         reload(p);
52     }
53
54     public FilterChain(SlaveSelectionManagerInterface sm, String JavaDoc cfgFileName)
55         throws FileNotFoundException JavaDoc, IOException JavaDoc {
56         _sm = sm;
57         _cfgfileName = cfgFileName;
58         reload();
59     }
60
61     public RemoteSlave getBestSlave(
62         ScoreChart sc,
63         User user,
64         InetAddress JavaDoc peer,
65         char direction,
66         LinkedRemoteFileInterface file)
67         throws NoAvailableSlaveException {
68         for (Iterator JavaDoc 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 JavaDoc, IOException JavaDoc {
79         Properties JavaDoc p = new Properties JavaDoc();
80         p.load(new FileInputStream JavaDoc(_cfgfileName));
81         reload(p);
82     }
83
84     public void reload(Properties JavaDoc p) {
85         ArrayList JavaDoc filters = new ArrayList JavaDoc();
86         int i = 1;
87         for (;; i++) {
88             String JavaDoc 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 JavaDoc[] SIG =
100                     new Class JavaDoc[] {
101                         FilterChain.class,
102                         int.class,
103                         Properties JavaDoc.class };
104
105                 Filter filter =
106                     (Filter) Class.forName(type).getConstructor(
107                         SIG).newInstance(
108                         new Object JavaDoc[] { this, new Integer JavaDoc(i), p });
109                 filters.add(filter);
110             } catch (Exception JavaDoc e) {
111                 throw new FatalException(i + ".filter = " + type, e);
112             }
113         }
114         if (i == 1)
115             throw new IllegalArgumentException JavaDoc();
116         filters.trimToSize();
117         _filters = filters;
118     }
119
120     public SlaveManagerImpl getSlaveManager() {
121         return _sm.getSlaveManager();
122     }
123 }
124
Popular Tags