001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.sections.conf;
019:
020: import net.sf.drftpd.FatalException;
021:
022: import org.drftpd.GlobalContext;
023:
024: import org.drftpd.master.ConnectionManager;
025: import org.drftpd.remotefile.LinkedRemoteFileInterface;
026: import org.drftpd.sections.SectionInterface;
027: import org.drftpd.sections.SectionManagerInterface;
028:
029: import java.io.FileInputStream;
030: import java.io.IOException;
031:
032: import java.util.Collection;
033: import java.util.Collections;
034: import java.util.Hashtable;
035: import java.util.Iterator;
036: import java.util.Properties;
037:
038: /**
039: * @author mog
040: * @version $Id: SectionManager.java 1562 2007-01-05 12:37:07Z zubov $
041: */
042: public class SectionManager implements SectionManagerInterface {
043: private static final Class[] CONSTRUCTOR_SIG = new Class[] {
044: SectionManager.class, int.class, Properties.class };
045: private PlainSection _emptySection = new PlainSection(this , "", "/");
046: private ConnectionManager _mgr;
047: private Hashtable<String, SectionInterface> _sections;
048:
049: public SectionManager(ConnectionManager mgr) {
050: _mgr = mgr;
051: reload();
052: }
053:
054: public ConnectionManager getConnectionManager() {
055: return _mgr;
056: }
057:
058: public SectionInterface getSection(String string) {
059: SectionInterface s = (SectionInterface) _sections.get(string);
060:
061: if (s != null) {
062: return s;
063: }
064:
065: return _emptySection;
066: }
067:
068: public Collection getSections() {
069: return Collections.unmodifiableCollection(_sections.values());
070: }
071:
072: public SectionInterface lookup(String string) {
073: int matchlen = 0;
074: SectionInterface match = _emptySection;
075:
076: for (Iterator iter = _sections.values().iterator(); iter
077: .hasNext();) {
078: SectionInterface section = (SectionInterface) iter.next();
079:
080: if (string.startsWith(section.getBasePath())
081: && (matchlen < section.getPath().length())) {
082: match = section;
083: matchlen = section.getPath().length();
084: }
085: }
086:
087: return match;
088: }
089:
090: public void reload() {
091: Properties p = new Properties();
092: FileInputStream stream = null;
093: try {
094: stream = new FileInputStream("conf/sections.conf");
095: p.load(stream);
096:
097: Hashtable<String, SectionInterface> sections = new Hashtable<String, SectionInterface>();
098:
099: for (int i = 1;; i++) {
100: String name = p.getProperty(i + ".name");
101: if (name == null)
102: break;
103:
104: String type = p.getProperty(i + ".type", "plain");
105:
106: try {
107: Class clazz = Class
108: .forName("org.drftpd.sections.conf."
109: + type.substring(0, 1)
110: .toUpperCase()
111: + type.substring(1) + "Section");
112: SectionInterface section = (SectionInterface) clazz
113: .getDeclaredConstructor(CONSTRUCTOR_SIG)
114: .newInstance(
115: new Object[] { this ,
116: new Integer(i), p });
117: sections.put(name, section);
118: } catch (Exception e1) {
119: throw new FatalException("Unknown section type: "
120: + i + ".type = " + type, e1);
121: }
122: }
123:
124: _sections = sections;
125: } catch (IOException e) {
126: throw new FatalException(e);
127: } finally {
128: if (stream != null) {
129: try {
130: stream.close();
131: } catch (IOException e) {
132: }
133: }
134: }
135: }
136:
137: public SectionInterface lookup(LinkedRemoteFileInterface file) {
138: return lookup(file.getPath());
139: }
140:
141: public GlobalContext getGlobalContext() {
142: return _mgr.getGlobalContext();
143: }
144: }
|