KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.drftpd.event.listeners;
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.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import net.sf.drftpd.Bytes;
30 import net.sf.drftpd.event.Event;
31 import net.sf.drftpd.event.FtpListener;
32 import net.sf.drftpd.event.TransferEvent;
33 import net.sf.drftpd.master.ConnectionManager;
34 import net.sf.drftpd.master.SlaveManagerImpl;
35 import net.sf.drftpd.master.config.ExcludePath;
36 import net.sf.drftpd.master.config.FtpConfig;
37 import net.sf.drftpd.remotefile.LinkedRemoteFile;
38 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
39 import net.sf.drftpd.remotefile.RemoteFileLastModifiedComparator;
40
41 import org.apache.log4j.BasicConfigurator;
42 import org.apache.log4j.Logger;
43 import org.apache.oro.text.regex.MalformedPatternException;
44
45 /**
46  * @author zubov
47  * @version $Id: AutoFreeSpace.java,v 1.13 2004/05/12 00:45:05 mog Exp $
48  */

49 public class AutoFreeSpace implements FtpListener {
50     private static final Logger logger = Logger.getLogger(AutoFreeSpace.class);
51
52     public static void main(String JavaDoc args[]) throws IOException JavaDoc {
53         BasicConfigurator.configure();
54         List JavaDoc rslaves = SlaveManagerImpl.loadRSlaves();
55         LinkedRemoteFileInterface root =
56             ConnectionManager.loadMLSTFileDatabase(rslaves, null);
57
58         AutoFreeSpace space = new AutoFreeSpace();
59         space.deleteOldReleases(root.getDirectories(), 0);
60     }
61     private long _archiveAfter;
62
63     private ConnectionManager _cm;
64     private long _cycleTime;
65     private ArrayList JavaDoc _exemptList;
66     private long _keepFree;
67     private long _lastchecked = System.currentTimeMillis();
68     public AutoFreeSpace() {
69         reload();
70     }
71
72     public void actionPerformed(Event event) {
73         if (event.getCommand().equals("RELOAD"))
74             reload();
75         if (!(event instanceof TransferEvent) || !event.getCommand().equals("STOR"))
76             return;
77         TransferEvent transevent = (TransferEvent) event;
78         if(!transevent.isComplete()) return;
79         if (System.currentTimeMillis() - _lastchecked <= _cycleTime) {
80             return;
81         }
82         _lastchecked = System.currentTimeMillis();
83
84         // Collection slaveList = null;
85
// try {
86
// slaveList = _cm.getSlaveManager().getAvailableSlaves();
87
// } catch (NoAvailableSlaveException e) {
88
// // done, can't remove space from nonexistant slaves
89
// }
90
// while (true) {
91
// RemoteSlave rslave = _cm.getSlaveManager().findSmallestFreeSlave();
92
// SlaveStatus status = null;
93
// try {
94
// status = rslave.getStatus();
95
// if (_keepFree > status.getDiskSpaceAvailable()) {
96
// deleteOldReleases(rslave);
97
// } else {
98
// break;
99
// }
100
// } catch (RemoteException e1) {
101
// rslave.handleRemoteException(e1);
102
// } catch (NoAvailableSlaveException e1) {
103
// }
104
// }
105
}
106
107     /**
108      * @param lrf
109      * Returns true if lrf.getPath() is excluded
110      */

111
112     public boolean checkExclude(LinkedRemoteFileInterface lrf) {
113         for (Iterator JavaDoc iter = _exemptList.iterator(); iter.hasNext();) {
114             ExcludePath ep = (ExcludePath) iter.next();
115             if (ep.checkPath(lrf))
116                 return true;
117         }
118         return false;
119     }
120
121     /**
122      * Deletes the oldest directories until _keepFree space is available.
123      * @param dirs
124      */

125     private void deleteOldReleases(Collection JavaDoc rootDirs, long spaceAvailable) {
126         //Collection rootDirs = getConnectionManager().getSlaveManager().getRoot().getDirectories();
127
ArrayList JavaDoc dirs = new ArrayList JavaDoc();
128         for (Iterator JavaDoc iter = rootDirs.iterator(); iter.hasNext();) {
129             LinkedRemoteFile lrf = (LinkedRemoteFile) iter.next();
130             if (checkExclude(lrf)) {
131                 logger.debug(lrf.getPath() + " is excluded");
132                 continue;
133             }
134             dirs.addAll(lrf.getDirectories());
135         }
136         Collections.sort(dirs, new RemoteFileLastModifiedComparator(true));
137
138         // long spaceAvailable =
139
// getConnectionManager()
140
// .getSlaveManager()
141
// .getAllStatus()
142
// .getDiskSpaceAvailable();
143
if (spaceAvailable < _keepFree) {
144             for (Iterator JavaDoc iter = dirs.iterator(); iter.hasNext();) {
145                 logger.debug(
146                     Bytes.formatBytes(spaceAvailable)
147                         + " < "
148                         + Bytes.formatBytes(_keepFree));
149                 if (spaceAvailable >= _keepFree)
150                     break;
151                 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
152                 spaceAvailable += file.length();
153                 logger.info("delete " + file.getPath());
154                 file.delete();
155             }
156         }
157     }
158
159     public void init(ConnectionManager connectionManager) {
160         _cm = connectionManager;
161     }
162     private void reload() {
163         Properties JavaDoc props = new Properties JavaDoc();
164         try {
165             props.load(new FileInputStream JavaDoc("conf/autofreespace.conf"));
166         } catch (IOException JavaDoc e) {
167             throw new RuntimeException JavaDoc(e);
168         }
169         reload(props);
170     }
171     private void reload(Properties JavaDoc props) {
172         _cycleTime =
173             60000 * Long.parseLong(FtpConfig.getProperty(props, "cycleTime"));
174         _keepFree = Bytes.parseBytes(FtpConfig.getProperty(props, "keepFree"));
175         _archiveAfter =
176             60000
177                 * Long.parseLong(FtpConfig.getProperty(props, "archiveAfter"));
178         _lastchecked = System.currentTimeMillis();
179         _exemptList = new ArrayList JavaDoc();
180         for (int i = 1;; i++) {
181             String JavaDoc path = props.getProperty("exclude." + i);
182             if (path == null)
183                 break;
184             try {
185                 ExcludePath.makePermission(_exemptList, path);
186             } catch (MalformedPatternException e1) {
187                 throw new RuntimeException JavaDoc(e1);
188             }
189         }
190
191     }
192
193     public void unload() {
194
195     }
196
197 }
198
Popular Tags