001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify it under the
005: * terms of the GNU General Public License as published by the Free Software
006: * Foundation; either version 2 of the License, or (at your option) any later
007: * version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful, but WITHOUT ANY
010: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
011: * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License along with
014: * DrFTPD; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
015: * Suite 330, Boston, MA 02111-1307 USA
016: */
017: package org.drftpd.mirroring.archivetypes;
018:
019: import java.util.ArrayList;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Properties;
023: import java.util.Set;
024:
025: import net.sf.drftpd.NoAvailableSlaveException;
026: import net.sf.drftpd.mirroring.Job;
027:
028: import org.apache.log4j.Logger;
029: import org.drftpd.PropertyHelper;
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: * @version $Id: MoveReleaseOffSlavesToMostFreeSlaves.java 1398 2006-01-14 20:54:51Z zubov $
039: */
040: public class MoveReleaseOffSlavesToMostFreeSlaves extends ArchiveType {
041: private static final Logger logger = Logger
042: .getLogger(MoveReleaseOffSlavesToMostFreeSlaves.class);
043: private Set<RemoteSlave> _offOfSlaves;
044:
045: public MoveReleaseOffSlavesToMostFreeSlaves(Archive archive,
046: SectionInterface section, Properties props) {
047: super (archive, section, props);
048: _offOfSlaves = getOffOfSlaves(props);
049:
050: if (_offOfSlaves.isEmpty()) {
051: throw new NullPointerException(
052: "Cannot continue, 0 slaves found to move off MoveReleaseOffSlavesToMostFreeSlaves for for section "
053: + getSection().getName());
054: }
055:
056: if (_numOfSlaves < 1) {
057: throw new IllegalArgumentException(
058: "numOfSlaves has to be > 0 for section "
059: + section.getName());
060: }
061: }
062:
063: public HashSet<RemoteSlave> findDestinationSlaves() {
064: HashSet<RemoteSlave> set = _parent.getGlobalContext()
065: .getSlaveManager().findSlavesBySpace(_numOfSlaves,
066: _offOfSlaves, false);
067:
068: if (set.isEmpty()) {
069: return null;
070: }
071:
072: return set;
073: }
074:
075: protected boolean isArchivedDir(LinkedRemoteFileInterface lrf)
076: throws IncompleteDirectoryException, OfflineSlaveException {
077: for (Iterator iter = lrf.getFiles().iterator(); iter.hasNext();) {
078: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
079: .next();
080:
081: if (file.isDirectory()) {
082: if (!isArchivedDir(file)) {
083: return false;
084: }
085: } else {
086: try {
087: for (Iterator iter2 = file.getAvailableSlaves()
088: .iterator(); iter2.hasNext();) {
089: RemoteSlave rslave = (RemoteSlave) iter2.next();
090:
091: if (_offOfSlaves.contains(rslave)) {
092: return false;
093: }
094: }
095: } catch (NoAvailableSlaveException e) {
096: throw new OfflineSlaveException(
097: "There were no available slaves for "
098: + file.getPath());
099: }
100: }
101: }
102:
103: return true;
104: }
105:
106: public String toString() {
107: return "MoveReleaseOffSlavesToMostFreeSlaves=[directory=["
108: + getDirectory().getPath() + "]dest=["
109: + outputSlaves(getRSlaves()) + "]numOfSlaves=["
110: + _numOfSlaves + "]]";
111: }
112: }
|