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.mirroring.archivetypes;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Properties;
025:
026: import net.sf.drftpd.mirroring.Job;
027: import net.sf.drftpd.mirroring.JobManager;
028:
029: import org.apache.log4j.Logger;
030: import org.drftpd.master.RemoteSlave;
031: import org.drftpd.mirroring.ArchiveType;
032: import org.drftpd.plugins.Archive;
033: import org.drftpd.remotefile.LinkedRemoteFileInterface;
034: import org.drftpd.sections.SectionInterface;
035:
036: /**
037: * @author zubov
038: */
039: public class ConstantMirroring extends ArchiveType {
040: private static final Logger logger = Logger
041: .getLogger(ConstantMirroring.class);
042: private long _slaveDeadAfter;
043:
044: public ConstantMirroring(Archive archive, SectionInterface section,
045: Properties p) {
046: super (archive, section, p);
047: _slaveDeadAfter = 1000 * 60 * Integer.parseInt(p.getProperty(
048: section.getName() + ".slaveDeadAfter", "0"));
049: int size = 0;
050: if (_slaveList.isEmpty()) {
051: _slaveList = null;
052: size = findDestinationSlaves().size();
053: } else {
054: size = _slaveList.size();
055: }
056:
057: if (_numOfSlaves > size && _numOfSlaves < 1) {
058: throw new IllegalArgumentException(
059: "numOfSlaves has to be 1 <= numOfSlaves <= the size of the destination slave list for section "
060: + section.getName());
061: }
062: }
063:
064: public HashSet<RemoteSlave> findDestinationSlaves() {
065: return new HashSet<RemoteSlave>(_parent.getGlobalContext()
066: .getSlaveManager().getSlaves());
067: }
068:
069: protected boolean isArchivedDir(LinkedRemoteFileInterface lrf)
070: throws IncompleteDirectoryException, OfflineSlaveException {
071: for (Iterator iter = lrf.getFiles().iterator(); iter.hasNext();) {
072: LinkedRemoteFileInterface src = (LinkedRemoteFileInterface) iter
073: .next();
074:
075: if (src.isLink()) {
076: continue;
077: }
078:
079: if (src.isFile()) {
080: Collection<RemoteSlave> slaves = src.getSlaves();
081: for (Iterator<RemoteSlave> slaveIter = slaves
082: .iterator(); slaveIter.hasNext();) {
083: RemoteSlave rslave = slaveIter.next();
084: if (!rslave.isAvailable()) {
085: long offlineTime = System.currentTimeMillis()
086: - rslave.getLastTimeOnline();
087: if (offlineTime > _slaveDeadAfter) {
088: // slave is considered dead
089: slaveIter.remove();
090: }
091: }
092: }
093: if (getRSlaves() != null
094: && !getRSlaves().containsAll(slaves)) {
095: // if getRSlaves() is null, it means ConstantMirroring is configured with
096: // each slave as a destination slave
097: // this condition need not be tested
098: return false;
099: }
100:
101: if (slaves.size() != _numOfSlaves) {
102: return false;
103: }
104: } else if (src.isDirectory()) {
105: if (!isArchivedDir(src)) {
106: return false;
107: }
108: }
109: }
110:
111: return true;
112: }
113:
114: public String toString() {
115: return "ConstantMirroring=[directory=["
116: + getDirectory().getPath() + "]dest=["
117: + outputSlaves(getRSlaves()) + "]numOfSlaves=["
118: + _numOfSlaves + "]]";
119: }
120: }
|