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;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Set;
023:
024: import net.sf.drftpd.mirroring.Job;
025:
026: import org.apache.log4j.Logger;
027: import org.drftpd.master.RemoteSlave;
028: import org.drftpd.plugins.Archive;
029: import org.drftpd.sections.SectionInterface;
030:
031: /**
032: * @author zubov
033: * @version $Id: ArchiveHandler.java 1334 2005-10-18 00:42:13Z zubov $
034: */
035: public class ArchiveHandler extends Thread {
036: protected final static Logger logger = Logger
037: .getLogger(ArchiveHandler.class);
038: private ArchiveType _archiveType;
039:
040: public ArchiveHandler(ArchiveType archiveType) {
041: super (archiveType.getClass().getName() + " archiving "
042: + archiveType.getSection().getName());
043: _archiveType = archiveType;
044: }
045:
046: public ArchiveType getArchiveType() {
047: return _archiveType;
048: }
049:
050: public SectionInterface getSection() {
051: return _archiveType.getSection();
052: }
053:
054: public void run() {
055: try {
056: synchronized (_archiveType._parent) {
057: if (_archiveType.getDirectory() == null) {
058: _archiveType.setDirectory(_archiveType
059: .getOldestNonArchivedDir());
060: }
061:
062: if (_archiveType.getDirectory() == null) {
063: return; // all done
064: }
065: try {
066: _archiveType._parent.addArchiveHandler(this );
067: } catch (DuplicateArchiveException e) {
068: logger.warn("Directory -- "
069: + _archiveType.getDirectory()
070: + " -- is already being archived ");
071: return;
072: }
073: }
074:
075: if (_archiveType.getRSlaves() == null) {
076: Set<RemoteSlave> destSlaves = _archiveType
077: .findDestinationSlaves();
078:
079: if (destSlaves == null) {
080: _archiveType.setDirectory(null);
081: return; // no available slaves to use
082: }
083:
084: _archiveType.setRSlaves(Collections
085: .unmodifiableSet(destSlaves));
086: }
087:
088: ArrayList<Job> jobs = _archiveType.send();
089: _archiveType.waitForSendOfFiles(new ArrayList<Job>(jobs));
090: if (_archiveType.getDirectory().isDeleted()) {
091: // all files will be deleted too, no need to removejobs, JobManager will do that
092: logger.info("Done archiving "
093: + getArchiveType().getDirectory().getPath()
094: + ", it was deleted during archival");
095: return;
096: }
097: _archiveType.cleanup(jobs);
098: logger.info("Done archiving "
099: + getArchiveType().getDirectory().getPath());
100: } catch (Exception e) {
101: logger.warn("", e);
102: } finally {
103: _archiveType._parent.removeArchiveHandler(this);
104: }
105: }
106: }
|