KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > mirroring > archivetypes > MoveReleaseOffSlavesToMostFreeSlaves


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * DrFTPD; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
15  * Suite 330, Boston, MA 02111-1307 USA
16  */

17 package org.drftpd.mirroring.archivetypes;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import net.sf.drftpd.NoAvailableSlaveException;
26 import net.sf.drftpd.ObjectNotFoundException;
27 import net.sf.drftpd.event.listeners.Archive;
28 import net.sf.drftpd.master.RemoteSlave;
29 import net.sf.drftpd.master.config.FtpConfig;
30 import net.sf.drftpd.mirroring.Job;
31 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
32 import org.apache.log4j.Logger;
33 import org.drftpd.mirroring.ArchiveType;
34 import org.drftpd.sections.SectionInterface;
35 /**
36  * @author zubov
37  * @version $Id: MoveReleaseOffSlavesToMostFreeSlaves.java,v 1.1 2004/05/22 15:08:43 zubov Exp $
38  */

39 public class MoveReleaseOffSlavesToMostFreeSlaves extends ArchiveType {
40     private static final Logger logger = Logger
41             .getLogger(MoveReleaseOffSlavesToMostFreeSlaves.class);
42     private HashSet JavaDoc _offOfSlaves;
43     private int _numOfSlaves;
44     public MoveReleaseOffSlavesToMostFreeSlaves(Archive archive,
45             SectionInterface section) {
46         super(archive, section);
47         Properties JavaDoc props = new Properties JavaDoc();
48         try {
49             props.load(new FileInputStream JavaDoc("conf/archive.conf"));
50         } catch (IOException JavaDoc e) {
51             throw new RuntimeException JavaDoc(e);
52         }
53         _offOfSlaves = new HashSet JavaDoc();
54         for (int i = 1;; i++) {
55             String JavaDoc slavename = null;
56             try {
57                 slavename = FtpConfig.getProperty(props, getSection().getName()
58                         + ".offOfSlave." + i);
59             } catch (NullPointerException JavaDoc e) {
60                 break; // done
61
}
62             try {
63                 _offOfSlaves.add(_parent.getConnectionManager()
64                         .getSlaveManager().getSlave(slavename));
65             } catch (ObjectNotFoundException e) {
66                 logger.debug("Unable to get slave " + slavename
67                         + " from the SlaveManager");
68             }
69         }
70         if (_offOfSlaves.isEmpty()) {
71             throw new NullPointerException JavaDoc(
72                     "Cannot continue, 0 slaves found to move off MoveReleaseOffSlavesToMostFreeSlaves for for section "
73                             + getSection().getName());
74         }
75         try {
76             _numOfSlaves = Integer.parseInt(FtpConfig.getProperty(props,
77                     getSection().getName() + ".numOfSlaves"));
78         } catch (NullPointerException JavaDoc e) {
79             _numOfSlaves = 1;
80         }
81     }
82     public void cleanup(ArrayList JavaDoc jobList) {
83         for (Iterator JavaDoc iter = jobList.iterator(); iter.hasNext();) {
84             Job job = (Job) iter.next();
85             job.getFile().deleteOthers(getRSlaves());
86         }
87     }
88     public HashSet JavaDoc findDestinationSlaves() {
89         HashSet JavaDoc set = _parent.getConnectionManager().getSlaveManager().findSlavesBySpace(_numOfSlaves,_offOfSlaves,false);
90         if (set.isEmpty())
91             return null;
92         return set;
93         
94     }
95     protected boolean isArchivedDir(LinkedRemoteFileInterface lrf)
96             throws IncompleteDirectoryException, OfflineSlaveException {
97         for (Iterator JavaDoc iter = lrf.getFiles().iterator(); iter.hasNext();) {
98             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
99                     .next();
100             if (file.isDirectory()) {
101                 if (!isArchivedDir(file)) {
102                     return false;
103                 }
104             } else {
105                 try {
106                     for (Iterator JavaDoc iter2 = file.getAvailableSlaves().iterator(); iter2
107                             .hasNext();) {
108                         RemoteSlave rslave = (RemoteSlave) iter2.next();
109                         if (_offOfSlaves.contains(rslave))
110                             return false;
111                     }
112                 } catch (NoAvailableSlaveException e) {
113                     throw new OfflineSlaveException(
114                             "There were no available slaves for "
115                                     + file.getPath());
116                 }
117             }
118         }
119         return true;
120     }
121     public void waitForSendOfFiles(ArrayList JavaDoc jobQueue) {
122         while (true) {
123             for (Iterator JavaDoc iter = jobQueue.iterator(); iter.hasNext();) {
124                 Job job = (Job) iter.next();
125                 if (job.isDone()) {
126                     logger.debug(
127                         "File "
128                             + job.getFile().getPath()
129                             + " is done being sent");
130                     iter.remove();
131                 }
132             }
133             try {
134                 Thread.sleep(10000);
135             } catch (InterruptedException JavaDoc e) {
136             }
137             if (jobQueue.isEmpty()) {
138                 break;
139             }
140         }
141     }
142     public String JavaDoc toString() {
143         return "MoveReleaseOffSlavesToMostFreeSlaves=[directory=[" + getDirectory().getPath() + "]dest=[" + outputSlaves(getRSlaves()) + "]numOfSlaves=[" + _numOfSlaves + "]]";
144     }
145 }
Popular Tags