KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > mirroring > ArchiveHandler


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;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import net.sf.drftpd.event.listeners.Archive;
27 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
28
29 import org.apache.log4j.Logger;
30 import org.drftpd.sections.SectionInterface;
31
32 /*
33  * @author zubov
34  * @version $Id: ArchiveHandler.java,v 1.7 2004/05/20 14:09:00 zubov Exp $
35  */

36 public class ArchiveHandler extends Thread JavaDoc {
37     protected static Logger logger = Archive.getLogger();
38
39     private ArchiveType _archiveType;
40     
41     private static ArrayList JavaDoc _archiveHandlers = new ArrayList JavaDoc();
42
43     public ArchiveHandler(ArchiveType archiveType) {
44         _archiveType = archiveType;
45         setName(
46             _archiveType.getClass().getName()
47                 + " archiving "
48                 + _archiveType.getSection().getName());
49     }
50     
51     public static ArchiveType getArchiveTypeForDirectory(LinkedRemoteFileInterface lrf) {
52         for (Iterator JavaDoc iter = _archiveHandlers.iterator(); iter.hasNext();) {
53             ArchiveHandler archiveHandler = (ArchiveHandler) iter.next();
54             ArchiveType archiveType = archiveHandler.getArchiveType();
55             if (lrf == archiveType.getDirectory()) {
56                 return archiveType;
57             }
58         }
59         return null;
60     }
61
62     public ArchiveType getArchiveType() {
63         return _archiveType;
64     }
65
66     public SectionInterface getSection() {
67         return _archiveType.getSection();
68     }
69
70     public void run() {
71         try {
72         synchronized (_archiveType) {
73             if (_archiveType.getDirectory() == null) {
74                 _archiveType.setDirectory(_archiveType.getOldestNonArchivedDir());
75             }
76         }
77         if (_archiveType.getDirectory() == null)
78             return; // all done
79
if (_archiveType.getRSlaves() == null) {
80             Set JavaDoc destSlaves = _archiveType.findDestinationSlaves();
81             if ( destSlaves == null ) {
82                 _archiveType.setDirectory(null);
83                 return; // no available slaves to use
84
}
85             _archiveType.setRSlaves(Collections.unmodifiableSet(destSlaves));
86         }
87         ArchiveType dupeArchiveType;
88         synchronized(_archiveHandlers) {
89             dupeArchiveType = getArchiveTypeForDirectory(_archiveType.getDirectory());
90             if (dupeArchiveType == null) {
91                 addArchiveHandler(this);
92             }
93         }
94         if (dupeArchiveType != null) {
95             logger.info(_archiveType.getDirectory() + " is already being archived by " + dupeArchiveType);
96             return;
97         }
98         ArrayList JavaDoc jobs = _archiveType.send();
99         _archiveType.waitForSendOfFiles(new ArrayList JavaDoc(jobs));
100         _archiveType.cleanup(jobs);
101         logger.info(
102             "Done archiving " + getArchiveType().getDirectory().getPath());
103         _archiveType.setDirectory(null);
104         _archiveType.setRSlaves(null);
105         } catch (Exception JavaDoc e) {
106             logger.debug("",e);
107         }
108         if(!removeArchiveHandler(this)) {
109             logger.debug("This is a serious bug, unable to remove the ArchiveHandler!");
110         }
111     }
112
113     private static void addArchiveHandler(ArchiveHandler handler) {
114         _archiveHandlers.add(handler);
115     }
116     private static boolean removeArchiveHandler(ArchiveHandler handler) {
117         return _archiveHandlers.remove(handler);
118     }
119
120     public static Collection JavaDoc getArchiveHandlers() {
121         return Collections.unmodifiableCollection(_archiveHandlers);
122     }
123 }
124
Popular Tags