KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import net.sf.drftpd.FatalException;
29 import net.sf.drftpd.master.ConnectionManager;
30
31 import org.drftpd.sections.SectionInterface;
32 import org.drftpd.sections.SectionManagerInterface;
33
34 /**
35  * @author mog
36  * @version $Id: SectionManager.java,v 1.4 2004/04/25 17:46:19 mog Exp $
37  */

38 public class SectionManager implements SectionManagerInterface {
39     private static final Class JavaDoc[] CONSTRUCTOR_SIG =
40         new Class JavaDoc[] { SectionManager.class, int.class, Properties JavaDoc.class };
41     private PlainSection _emptySection = new PlainSection(this, "", "/");
42     private ConnectionManager _mgr;
43     private Hashtable JavaDoc _sections = new Hashtable JavaDoc();
44     
45     public SectionManager(ConnectionManager mgr) {
46         _mgr = mgr;
47         reload();
48     }
49
50     public ConnectionManager getConnectionManager() {
51         return _mgr;
52     }
53
54     public SectionInterface getSection(String JavaDoc string) {
55         SectionInterface s = (SectionInterface) _sections.get(string);
56         if(s != null) return s;
57         return _emptySection;
58     }
59
60     public Collection JavaDoc getSections() {
61         return Collections.unmodifiableCollection(_sections.values());
62     }
63
64     public SectionInterface lookup(String JavaDoc string) {
65         int matchlen = 0;
66         SectionInterface match = _emptySection;
67
68         for (Iterator JavaDoc iter = _sections.values().iterator(); iter.hasNext();) {
69             SectionInterface section = (SectionInterface) iter.next();
70             if (string.startsWith(section.getPath())
71                 && matchlen < section.getPath().length()) {
72                 match = section;
73                 matchlen = section.getPath().length();
74             }
75         }
76         return match;
77     }
78
79     public void reload() {
80         Properties JavaDoc p = new Properties JavaDoc();
81         try {
82             p.load(new FileInputStream JavaDoc("conf/sections.conf"));
83         } catch (IOException JavaDoc e) {
84             throw new FatalException(e);
85         }
86         Hashtable JavaDoc sections = new Hashtable JavaDoc();
87         for (int i = 1;; i++) {
88             String JavaDoc name = p.getProperty(i + ".name");
89             if (name == null)
90                 break;
91             String JavaDoc type = p.getProperty(i + ".type", "plain");
92             try {
93                 Class JavaDoc clazz =
94                     Class.forName(
95                         "org.drftpd.sections.conf."
96                             + type.substring(0, 1).toUpperCase()
97                             + type.substring(1)
98                             + "Section");
99                 SectionInterface section =
100                     (SectionInterface) clazz.getDeclaredConstructor(
101                         CONSTRUCTOR_SIG).newInstance(
102                         new Object JavaDoc[] { this, new Integer JavaDoc(i), p });
103                 sections.put(name, section);
104             } catch (Exception JavaDoc e1) {
105                 throw new FatalException(
106                     "Unknown section type: " + i + ".type = " + type,
107                     e1);
108             }
109         }
110         _sections = sections;
111     }
112 }
113
Popular Tags