KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.drftpd.mirroring.archivetypes;
19
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ListIterator JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import net.sf.drftpd.event.listeners.Archive;
32 import net.sf.drftpd.master.RemoteSlave;
33 import net.sf.drftpd.master.config.FtpConfig;
34 import net.sf.drftpd.mirroring.Job;
35 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
36
37 import org.apache.log4j.Logger;
38 import org.drftpd.mirroring.ArchiveType;
39 import org.drftpd.sections.SectionInterface;
40
41 /**
42  * @author zubov
43  * @version $Id: FinishReleaseOnSlaves.java,v 1.2 2004/05/20 14:09:00 zubov Exp $
44  */

45 public class FinishReleaseOnSlaves extends ArchiveType {
46     private static final Logger logger = Logger.getLogger(FinishReleaseOnSlaves.class);
47     private int _numOfSlaves = 1;
48     
49     public FinishReleaseOnSlaves(Archive archive, SectionInterface section) {
50         super(archive,section);
51         Properties JavaDoc props = new Properties JavaDoc();
52         try {
53             props.load(new FileInputStream JavaDoc("conf/archive.conf"));
54         } catch (IOException JavaDoc e) {
55             throw new RuntimeException JavaDoc(e);
56         }
57         try {
58             _numOfSlaves = Integer.parseInt(FtpConfig.getProperty(props,getSection().getName() + ".numOfSlaves"));
59         } catch (NullPointerException JavaDoc e) {
60             _numOfSlaves = 1;
61         }
62     }
63
64     public class SlaveCount implements Comparable JavaDoc {
65         public int compareTo(Object JavaDoc o) {
66             SlaveCount count = (SlaveCount) o;
67             return getValue() - count.getValue();
68         }
69         public SlaveCount() {
70         }
71         private int _value = 1;
72         public void addOne() {
73             _value++;
74         }
75         public int getValue() {
76             return _value;
77         }
78         
79     }
80     public void findDestinationSlavesRecursive(LinkedRemoteFileInterface lrf,HashMap JavaDoc slaveMap) {
81         for (Iterator JavaDoc iter = lrf.getFiles().iterator(); iter.hasNext();) {
82             LinkedRemoteFileInterface file = null;
83             file = (LinkedRemoteFileInterface) iter.next();
84             if (file.isDirectory()) {
85                 findDestinationSlavesRecursive(file,slaveMap);
86                 continue;
87             }
88             Collection JavaDoc tempSlaveList =file.getSlaves();
89                 for (Iterator JavaDoc iter2 = tempSlaveList.iterator(); iter2.hasNext();) {
90                     RemoteSlave rslave = (RemoteSlave) iter2.next();
91                     if (rslave.isAvailable()) {
92                         SlaveCount i = (SlaveCount) slaveMap.get(rslave);
93                         if (i == null) {
94                             slaveMap.put(new SlaveCount(),rslave);
95                         }
96                         else i.addOne();
97                     }
98                 }
99         }
100
101     }
102
103     public HashSet JavaDoc findDestinationSlaves() {
104         HashMap JavaDoc slaveMap = new HashMap JavaDoc();
105         findDestinationSlavesRecursive(getDirectory(),slaveMap);
106         ArrayList JavaDoc sorted = new ArrayList JavaDoc(slaveMap.keySet());
107         Collections.sort(sorted);
108         HashSet JavaDoc returnMe = new HashSet JavaDoc();
109         for (ListIterator JavaDoc iter = sorted.listIterator(); iter.hasNext();) {
110             if (iter.nextIndex()==_numOfSlaves)
111                 break;
112             RemoteSlave rslave = (RemoteSlave) slaveMap.get(iter.next());
113             returnMe.add(rslave);
114         }
115         return returnMe;
116     }
117     public void cleanup(ArrayList JavaDoc jobList) {
118         for (Iterator JavaDoc iter = jobList.iterator(); iter.hasNext();) {
119             Job job = (Job) iter.next();
120             job.getFile().deleteOthers(getRSlaves());
121         }
122     }
123
124     protected boolean isArchivedDir(LinkedRemoteFileInterface lrf)
125             throws IncompleteDirectoryException, OfflineSlaveException {
126         return isArchivedToXSlaves(lrf,_numOfSlaves);
127     }
128
129     public void waitForSendOfFiles(ArrayList JavaDoc jobQueue) {
130         while (true) {
131             for (Iterator JavaDoc iter = jobQueue.iterator(); iter.hasNext();) {
132                 Job job = (Job) iter.next();
133                 if (job.isDone()) {
134                     logger.debug(
135                         "File "
136                             + job.getFile().getPath()
137                             + " is done being sent");
138                     iter.remove();
139                 }
140             }
141             try {
142                 Thread.sleep(10000);
143             } catch (InterruptedException JavaDoc e) {
144             }
145             if (jobQueue.isEmpty()) {
146                 break;
147             }
148         }
149     }
150
151     public String JavaDoc toString() {
152         return "FinishReleaseOnSlaves=[directory=[" + getDirectory().getPath() + "]dest=[" + outputSlaves(getRSlaves()) + "]]";
153     }
154 }
155
Popular Tags