KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > listeners > Archive


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 net.sf.drftpd.event.listeners;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
26 import net.sf.drftpd.event.Event;
27 import net.sf.drftpd.event.FtpListener;
28 import net.sf.drftpd.master.ConnectionManager;
29 import net.sf.drftpd.master.config.FtpConfig;
30
31 import org.apache.log4j.Logger;
32 import org.drftpd.mirroring.ArchiveHandler;
33 import org.drftpd.mirroring.ArchiveType;
34 import org.drftpd.sections.SectionInterface;
35 /**
36  * @author zubov
37  * @version $Id: Archive.java,v 1.27 2004/05/20 14:08:59 zubov Exp $
38  */

39 public class Archive implements FtpListener, Runnable JavaDoc {
40     private Properties JavaDoc _props;
41     private static final Logger logger = Logger.getLogger(Archive.class);
42     public static Logger getLogger() {
43         return logger;
44     }
45     private HashMap JavaDoc _archiveTypes;
46     private ConnectionManager _cm;
47     private long _cycleTime;
48     private ArrayList JavaDoc _exemptList = new ArrayList JavaDoc();
49     private boolean _isStopped = false;
50     private Thread JavaDoc thread = null;
51     private ArrayList JavaDoc _archiveHandlers;
52     public Archive() {
53         logger.info("Archive plugin loaded successfully");
54         _archiveHandlers = new ArrayList JavaDoc();
55     }
56     public Properties JavaDoc getProperties() {
57         return _props;
58     }
59     public void actionPerformed(Event event) {
60         if (event.getCommand().equals("RELOAD")) {
61             reload();
62             return;
63         }
64     }
65     /**
66      * @param lrf
67      * Returns true if lrf.getPath() is excluded
68      */

69     public boolean checkExclude(SectionInterface section) {
70         return _exemptList.contains(section.getName());
71     }
72     /**
73      * @return the correct ArchiveType for the @section - if the ArchiveType is still being used, it will return null
74      */

75     public ArchiveType getArchiveType(SectionInterface section) {
76         ArchiveType archiveType = (ArchiveType) _archiveTypes.get(section);
77         if (archiveType == null)
78             throw new IllegalStateException JavaDoc(
79                     "Could not find an archive type for "
80                             + section.getName()
81                             + ", check you make sure default.archiveType is defined in archive.conf and the section is not excluded");
82         return archiveType;
83     }
84     /**
85      * Returns the ConnectionManager
86      */

87     public ConnectionManager getConnectionManager() {
88         return _cm;
89     }
90     /**
91      * Returns the getCycleTime setting
92      */

93     public long getCycleTime() {
94         return _cycleTime;
95     }
96     public void init(ConnectionManager connectionManager) {
97         _cm = connectionManager;
98         _cm.loadJobManager();
99         reload();
100         startArchive();
101     }
102     private boolean isStopped() {
103         return _isStopped;
104     }
105     private void reload() {
106         _props = new Properties JavaDoc();
107         try {
108             _props.load(new FileInputStream JavaDoc("conf/archive.conf"));
109         } catch (IOException JavaDoc e) {
110             throw new RuntimeException JavaDoc(e);
111         }
112         _cycleTime = 60000 * Long.parseLong(FtpConfig.getProperty(_props,
113                 "cycleTime"));
114         _exemptList = new ArrayList JavaDoc();
115         for (int i = 1;; i++) {
116             String JavaDoc path = _props.getProperty("exclude." + i);
117             if (path == null)
118                 break;
119             _exemptList.add(path);
120         }
121         _archiveTypes = new HashMap JavaDoc();
122         Class JavaDoc[] classParams = {Archive.class, SectionInterface.class};
123         for (Iterator JavaDoc iter = getConnectionManager().getSectionManager()
124                 .getSections().iterator(); iter.hasNext();) {
125             SectionInterface section = (SectionInterface) iter.next();
126             if (checkExclude(section))
127                 // don't have to build an archiveType for sections that won't be
128
// archived
129
continue;
130             ArchiveType archiveType = null;
131             String JavaDoc name = null;
132             try {
133                 name = FtpConfig.getProperty(_props, section.getName()
134                         + ".archiveType");
135             } catch (NullPointerException JavaDoc e) {
136                 name = FtpConfig.getProperty(_props, "default.archiveType");
137             }
138             Constructor JavaDoc constructor = null;
139             try {
140                 constructor = Class.forName(
141                         "org.drftpd.mirroring.archivetypes."
142                                 + name).getConstructor(classParams);
143             } catch (Exception JavaDoc e1) {
144                 throw new RuntimeException JavaDoc("Unable to load ArchiveType for section " + section.getName(), e1);
145             }
146             Object JavaDoc[] objectParams = { this, section };
147             try {
148                 archiveType = (ArchiveType) constructor.newInstance(objectParams);
149             } catch (Exception JavaDoc e2) {
150                 throw new RuntimeException JavaDoc("Unable to load ArchiveType for section " + section.getName(), e2);
151             }
152             _archiveTypes.put(section, archiveType);
153             logger.debug("added archiveType for section " + section.getName());
154         }
155     }
156     public void run() {
157         while (true) {
158             if (isStopped()) {
159                 logger.debug("Stopping ArchiveStarter thread");
160                 return;
161             }
162             for (Iterator JavaDoc iter = _archiveHandlers.iterator(); iter.hasNext();) {
163                 ArchiveHandler archiveHandler = (ArchiveHandler) iter.next();
164                 if (!archiveHandler.isAlive()) {
165                     iter.remove();
166                 }
167             }
168             Collection JavaDoc sectionsToCheck = getConnectionManager()
169                     .getSectionManager().getSections();
170             for (Iterator JavaDoc iter = sectionsToCheck.iterator(); iter.hasNext();) {
171                 SectionInterface section = (SectionInterface) iter.next();
172                 if (checkExclude(section))
173                     continue;
174                 ArchiveType archiveType = getArchiveType(section);
175                 if (archiveType.isBusy()) // archiveType was not done with it's
176
continue; // current send, cannot process another
177
ArchiveHandler archiveHandler = new ArchiveHandler(archiveType);
178                 archiveHandler.start();
179             }
180             try {
181                 Thread.sleep(_cycleTime);
182             } catch (InterruptedException JavaDoc e) {
183             }
184         }
185     }
186     public void startArchive() {
187         if (thread != null) {
188             stopArchive();
189             thread.interrupt();
190             while (thread.isAlive()) {
191                 Thread.yield();
192             }
193         }
194         _isStopped = false;
195         thread = new Thread JavaDoc(this, "ArchiveStarter");
196         thread.start();
197     }
198     public void stopArchive() {
199         _isStopped = true;
200     }
201     public void unload() {
202         stopArchive();
203     }
204 }
Popular Tags