KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.sf.drftpd.NoAvailableSlaveException;
25 import net.sf.drftpd.ObjectNotFoundException;
26 import net.sf.drftpd.event.listeners.Archive;
27 import net.sf.drftpd.master.RemoteSlave;
28 import net.sf.drftpd.master.config.FtpConfig;
29 import net.sf.drftpd.mirroring.Job;
30 import net.sf.drftpd.mirroring.JobManager;
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: StripeFilesOffSpecificSlaves.java,v 1.7 2004/05/22 15:08:43 zubov Exp $
38  */

39 public class StripeFilesOffSpecificSlaves extends ArchiveType {
40     private static final Logger logger = Logger
41             .getLogger(StripeFilesOffSpecificSlaves.class);
42     private HashSet JavaDoc _destSlaves;
43     private HashSet JavaDoc _offOfSlaves;
44     int _numOfSlaves = 1;
45     public StripeFilesOffSpecificSlaves(Archive archive, 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 StripeFilesOffSpecificSlave 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         _destSlaves = new HashSet JavaDoc();
82         for (int i = 1;; i++) {
83             String JavaDoc slavename = null;
84             try {
85                 slavename = FtpConfig.getProperty(props, getSection().getName() + ".slavename." + i);
86             } catch (NullPointerException JavaDoc e) {
87                 break; // done
88
}
89             try {
90                 RemoteSlave rslave = _parent.getConnectionManager().getSlaveManager().getSlave(slavename);
91                 if (!_offOfSlaves.contains(rslave)) {
92                     _destSlaves.add(rslave);
93                 }
94             } catch (ObjectNotFoundException e) {
95                 logger.debug("Unable to get slave " + slavename + " from the SlaveManager");
96             }
97         }
98         if (_destSlaves.isEmpty()) {
99             _destSlaves = null; // used as a flag for dynamic slaves
100
} else {
101             if (_destSlaves.size() < _numOfSlaves) {
102                 throw new IllegalStateException JavaDoc("Cannot continue, numOfSlave cannot be less than the # of slaveName's");
103             }
104         }
105     }
106     public void cleanup(ArrayList JavaDoc jobList) {
107         for (Iterator JavaDoc iter = jobList.iterator(); iter.hasNext();) {
108             Job job = (Job) iter.next();
109             job.getFile().deleteOthers(getRSlaves());
110         }
111     }
112     public HashSet JavaDoc findDestinationSlaves() {
113         if (_destSlaves != null)
114             return _destSlaves;
115         HashSet JavaDoc availableSlaves;
116         try {
117             availableSlaves = new HashSet JavaDoc(_parent.getConnectionManager()
118                     .getSlaveManager().getAvailableSlaves());
119         } catch (NoAvailableSlaveException e) {
120             return null;
121         }
122         availableSlaves.removeAll(_offOfSlaves);
123         if (availableSlaves.isEmpty())
124             return null;
125         return availableSlaves;
126     }
127     protected boolean isArchivedDir(LinkedRemoteFileInterface lrf)
128             throws IncompleteDirectoryException, OfflineSlaveException {
129         for (Iterator JavaDoc iter = lrf.getFiles().iterator(); iter.hasNext();) {
130             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
131                     .next();
132             if (file.isDirectory()) {
133                 if (!isArchivedDir(file)) {
134                     return false;
135                 }
136             } else {
137                 try {
138                     for (Iterator JavaDoc iter2 = file.getAvailableSlaves().iterator(); iter2
139                             .hasNext();) {
140                         RemoteSlave rslave = (RemoteSlave) iter2.next();
141                         if (_offOfSlaves.contains(rslave))
142                             return false;
143                     }
144                 } catch (NoAvailableSlaveException e) {
145                     throw new OfflineSlaveException(
146                             "There were no available slaves for "
147                                     + file.getPath());
148                 }
149             }
150         }
151         return true;
152     }
153     
154     public ArrayList JavaDoc send() {
155         return recursiveSend(getDirectory());
156     }
157
158     private ArrayList JavaDoc recursiveSend(LinkedRemoteFileInterface lrf) {
159         ArrayList JavaDoc jobQueue = new ArrayList JavaDoc();
160         JobManager jm = _parent.getConnectionManager().getJobManager();
161         for (Iterator JavaDoc iter = lrf.getFiles().iterator(); iter.hasNext();) {
162             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
163                     .next();
164             if (file.isDirectory()) {
165                 jobQueue.addAll(recursiveSend(file));
166             } else {
167                 logger.info("Adding " + file.getPath() + " to the job queue with numOfSlaves = " + _numOfSlaves);
168                 Job job = new Job(file, getRSlaves(), this, null, 3, _numOfSlaves);
169                 jm.addJob(job);
170                 jobQueue.add(job);
171             }
172         }
173         return jobQueue;
174     }
175     
176     public void waitForSendOfFiles(ArrayList JavaDoc jobQueue) {
177         while (true) {
178             for (Iterator JavaDoc iter = jobQueue.iterator(); iter.hasNext();) {
179                 Job job = (Job) iter.next();
180                 if (job.isDone()) {
181                     logger.debug("File " + job.getFile().getPath()
182                             + " is done being sent");
183                     iter.remove();
184                 }
185             }
186             try {
187                 Thread.sleep(10000);
188             } catch (InterruptedException JavaDoc e) {
189             }
190             if (jobQueue.isEmpty()) {
191                 break;
192             }
193         }
194     }
195     public String JavaDoc toString() {
196         return "StripeFilesOffSpecificSlaves=[directory=[" + getDirectory().getPath() + "]dest=[" + outputSlaves(getRSlaves()) + "]offOfSlaves=[" + outputSlaves(_offOfSlaves) + "]numOfSlaves=[" + _numOfSlaves + "]]";
197     }
198 }
Popular Tags