KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > sections > conf > DatedSection


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.sections.conf;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import net.sf.drftpd.master.config.FtpConfig;
27 import net.sf.drftpd.remotefile.LinkedRemoteFile;
28 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
29
30 import org.drftpd.remotefile.FileUtils;
31 import org.drftpd.sections.SectionInterface;
32
33 /**
34  * @author mog
35  * @version $Id: DatedSection.java,v 1.7 2004/04/25 17:46:19 mog Exp $
36  */

37 public class DatedSection implements SectionInterface {
38     private String JavaDoc _basePath;
39     private SimpleDateFormat JavaDoc _dateFormat;
40
41     private SectionManager _mgr;
42     private String JavaDoc _name;
43
44     public DatedSection(SectionManager mgr, int i, Properties JavaDoc p) {
45         _mgr = mgr;
46         _name = FtpConfig.getProperty(p, i + ".name");
47         _basePath = FtpConfig.getProperty(p, i + ".path");
48         if(!_basePath.endsWith("/")) _basePath += "/";
49         _dateFormat =
50             new SimpleDateFormat JavaDoc(FtpConfig.getProperty(p, i + ".dated"));
51         getBaseFile();
52     }
53
54     public LinkedRemoteFile getBaseFile() {
55         try {
56             return _mgr.getConnectionManager().getRoot().lookupFile(_basePath);
57         } catch (FileNotFoundException JavaDoc e) {
58             return _mgr.getConnectionManager().getRoot().createDirectories(_basePath);
59         }
60     }
61
62     public LinkedRemoteFileInterface getFile() {
63         String JavaDoc dateDir = _dateFormat.format(new Date JavaDoc());
64         try {
65             return getBaseFile().lookupFile(dateDir);
66         } catch (FileNotFoundException JavaDoc e) {
67             return getBaseFile().createDirectories(dateDir);
68         }
69     }
70
71     public Collection JavaDoc getFiles() {
72         return getBaseFile().getDirectories();
73     }
74
75     public LinkedRemoteFileInterface getFirstDirInSection(LinkedRemoteFileInterface dir) {
76         try {
77             return FileUtils.getSubdirOfDirectory(getFile(), dir);
78         } catch (FileNotFoundException JavaDoc e) {
79             return dir;
80         }
81     }
82
83     public String JavaDoc getName() {
84         return _name;
85     }
86
87     public String JavaDoc getPath() {
88         return getFile().getPath();
89     }
90 }
91
Popular Tags