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 net.sf.drftpd.FatalException;
021: import net.sf.drftpd.NoAvailableSlaveException;
022:
023: import org.drftpd.GlobalContext;
024:
025: import org.drftpd.master.RemoteSlave;
026: import org.drftpd.remotefile.LinkedRemoteFileInterface;
027: import org.drftpd.slaveselection.SlaveSelectionManagerInterface;
028: import org.drftpd.usermanager.User;
029:
030: import java.io.FileInputStream;
031: import java.io.FileNotFoundException;
032: import java.io.IOException;
033:
034: import java.net.InetAddress;
035:
036: import java.util.ArrayList;
037: import java.util.Properties;
038:
039: /**
040: * @author mog
041: * @version $Id: FilterChain.java 847 2004-12-02 03:32:41Z mog $
042: */
043: public class FilterChain {
044: private SlaveSelectionManagerInterface _ssm;
045: private String _cfgfileName;
046: private ArrayList<Filter> _filters;
047:
048: protected FilterChain() {
049: }
050:
051: public FilterChain(SlaveSelectionManagerInterface ssm, Properties p) {
052: _ssm = ssm;
053: reload(p);
054: }
055:
056: public FilterChain(SlaveSelectionManagerInterface ssm,
057: String cfgFileName) throws FileNotFoundException,
058: IOException {
059: _ssm = ssm;
060: _cfgfileName = cfgFileName;
061: reload();
062: }
063:
064: public void filter(ScoreChart sc, User user, InetAddress peer,
065: char direction, LinkedRemoteFileInterface file,
066: RemoteSlave sourceSlave) throws NoAvailableSlaveException {
067: for (Filter filter : _filters) {
068: filter
069: .process(sc, user, peer, direction, file,
070: sourceSlave);
071: }
072: }
073:
074: public RemoteSlave getBestSlave(ScoreChart sc, User user,
075: InetAddress peer, char direction,
076: LinkedRemoteFileInterface file, RemoteSlave sourceSlave)
077: throws NoAvailableSlaveException {
078: filter(sc, user, peer, direction, file, sourceSlave);
079: RemoteSlave rslave = sc.getBestSlave();
080: rslave.setLastDirection(direction, System.currentTimeMillis());
081: return rslave;
082: }
083:
084: public void reload() throws FileNotFoundException, IOException {
085: Properties p = new Properties();
086: FileInputStream fis = null;
087: try {
088: fis = new FileInputStream(_cfgfileName);
089: p.load(fis);
090: } finally {
091: if (fis != null) {
092: fis.close();
093: fis = null;
094: }
095: }
096: reload(p);
097: }
098:
099: public void reload(Properties p) {
100: ArrayList<Filter> filters = new ArrayList<Filter>();
101: int i = 1;
102:
103: for (;; i++) {
104: String type = p.getProperty(i + ".filter");
105:
106: if (type == null) {
107: break;
108: }
109:
110: if (type.indexOf('.') == -1) {
111: type = "org.drftpd.slaveselection.filter."
112: + type.substring(0, 1).toUpperCase()
113: + type.substring(1) + "Filter";
114: }
115:
116: try {
117: Class[] SIG = new Class[] { FilterChain.class,
118: int.class, Properties.class };
119:
120: Filter filter = (Filter) Class
121: .forName(type)
122: .getConstructor(SIG)
123: .newInstance(
124: new Object[] { this , new Integer(i), p });
125: filters.add(filter);
126: } catch (Exception e) {
127: throw new FatalException(i + ".filter = " + type, e);
128: }
129: }
130:
131: filters.trimToSize();
132: _filters = filters;
133: }
134:
135: public GlobalContext getGlobalContext() {
136: return _ssm.getGlobalContext();
137: }
138: }
|