| 1 18 package org.drftpd.sections.conf; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 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 38 public class SectionManager implements SectionManagerInterface { 39 private static final Class [] CONSTRUCTOR_SIG = 40 new Class [] { SectionManager.class, int.class, Properties .class }; 41 private PlainSection _emptySection = new PlainSection(this, "", "/"); 42 private ConnectionManager _mgr; 43 private Hashtable _sections = new Hashtable (); 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 string) { 55 SectionInterface s = (SectionInterface) _sections.get(string); 56 if(s != null) return s; 57 return _emptySection; 58 } 59 60 public Collection getSections() { 61 return Collections.unmodifiableCollection(_sections.values()); 62 } 63 64 public SectionInterface lookup(String string) { 65 int matchlen = 0; 66 SectionInterface match = _emptySection; 67 68 for (Iterator 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 p = new Properties (); 81 try { 82 p.load(new FileInputStream ("conf/sections.conf")); 83 } catch (IOException e) { 84 throw new FatalException(e); 85 } 86 Hashtable sections = new Hashtable (); 87 for (int i = 1;; i++) { 88 String name = p.getProperty(i + ".name"); 89 if (name == null) 90 break; 91 String type = p.getProperty(i + ".type", "plain"); 92 try { 93 Class 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 [] { this, new Integer (i), p }); 103 sections.put(name, section); 104 } catch (Exception e1) { 105 throw new FatalException( 106 "Unknown section type: " + i + ".type = " + type, 107 e1); 108 } 109 } 110 _sections = sections; 111 } 112 } 113 | Popular Tags |