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.plugins;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Properties;
026:
027: import net.sf.drftpd.event.Event;
028: import net.sf.drftpd.event.FtpListener;
029: import net.sf.drftpd.event.TransferEvent;
030: import net.sf.drftpd.master.config.FtpConfig;
031: import net.sf.drftpd.mirroring.Job;
032:
033: import org.apache.log4j.Logger;
034: import org.apache.oro.text.GlobCompiler;
035: import org.apache.oro.text.regex.MalformedPatternException;
036: import org.drftpd.GlobalContext;
037: import org.drftpd.PropertyHelper;
038: import org.drftpd.master.ConnectionManager;
039: import org.drftpd.master.RemoteSlave;
040: import org.drftpd.permissions.GlobPathPermission;
041: import org.drftpd.remotefile.LinkedRemoteFileInterface;
042:
043: import com.Ostermiller.util.StringTokenizer;
044:
045: /**
046: * @author zubov
047: * @version $Id: Mirror.java 1312 2005-10-14 18:38:37Z zubov $
048: */
049: public class Mirror extends FtpListener {
050: private static final Logger logger = Logger.getLogger(Mirror.class);
051: private ConnectionManager _cm;
052: private ArrayList<GlobPathPermission> _perms;
053: private boolean _mirrorAllSFV;
054: private int _numberOfMirrors;
055:
056: public Mirror() {
057: reload();
058: logger.info("Mirror plugin loaded successfully");
059: }
060:
061: public void actionPerformed(Event event) {
062: if (event.getCommand().equals("RELOAD")) {
063: reload();
064: return;
065: }
066:
067: if (event.getCommand().equals("STOR")) {
068: actionPerformedSTOR((TransferEvent) event);
069: }
070: }
071:
072: public void actionPerformedSTOR(TransferEvent transevent) {
073:
074: LinkedRemoteFileInterface file = transevent.getDirectory();
075:
076: List<RemoteSlave> slaves = _cm.getGlobalContext()
077: .getSlaveManager().getSlaves();
078: for (GlobPathPermission perm : _perms) {
079: if (perm.checkPath(file)) {
080: for (Iterator<RemoteSlave> iter = slaves.iterator(); iter
081: .hasNext();) {
082: if (!perm.check(iter.next()))
083: iter.remove();
084: }
085: break;
086: }
087: }
088:
089: int numToMirror = _numberOfMirrors;
090:
091: if (_mirrorAllSFV
092: && file.getName().toLowerCase().endsWith(".sfv")) {
093: numToMirror = _cm.getGlobalContext().getSlaveManager()
094: .getSlaves().size() / 2;
095: }
096:
097: getGlobalContext().getJobManager().addJobToQueue(
098: new Job(file, slaves, 0, numToMirror));
099: logger
100: .info("Done adding " + file.getPath()
101: + " to the JobList");
102: }
103:
104: public void init(GlobalContext gctx) {
105: super .init(gctx);
106: }
107:
108: private void reload() {
109: Properties props = new Properties();
110:
111: try {
112: props.load(new FileInputStream("conf/mirror.conf"));
113: } catch (IOException e) {
114: throw new RuntimeException(e);
115: }
116:
117: _numberOfMirrors = Integer.parseInt(PropertyHelper.getProperty(
118: props, "numberOfMirrors"));
119: _mirrorAllSFV = PropertyHelper.getProperty(props,
120: "mirrorAllSFV").equals("true");
121: _perms = new ArrayList<GlobPathPermission>();
122:
123: for (int i = 1;; i++) {
124: StringTokenizer st;
125: try {
126: st = new com.Ostermiller.util.StringTokenizer(
127: PropertyHelper.getProperty(props, "pathperm."
128: + i));
129: } catch (NullPointerException e) {
130: break;
131: }
132: try {
133: _perms.add(new GlobPathPermission(new GlobCompiler()
134: .compile(st.nextToken()), FtpConfig
135: .makeUsers(st)));
136: } catch (MalformedPatternException e1) {
137: throw new RuntimeException(e1);
138: }
139: }
140: }
141:
142: public void unload() {
143: }
144: }
|