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.def;
019:
020: import org.drftpd.master.ConnectionManager;
021: import org.drftpd.remotefile.FileUtils;
022: import org.drftpd.remotefile.LinkedRemoteFileInterface;
023:
024: import org.drftpd.sections.SectionInterface;
025: import org.drftpd.sections.SectionManagerInterface;
026:
027: import java.io.FileNotFoundException;
028:
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Iterator;
033: import java.util.StringTokenizer;
034:
035: /**
036: * @author mog
037: * @version $Id: SectionManager.java 847 2004-12-02 03:32:41Z mog $
038: */
039: public class SectionManager implements SectionManagerInterface {
040: private ConnectionManager _cm;
041:
042: public SectionManager(ConnectionManager cm) {
043: _cm = cm;
044: }
045:
046: public ConnectionManager getConnectionManager() {
047: return _cm;
048: }
049:
050: public SectionInterface getSection(String string) {
051: try {
052: return new Section(_cm.getGlobalContext().getRoot()
053: .getFile(string));
054: } catch (FileNotFoundException e) {
055: return new Section(_cm.getGlobalContext().getRoot());
056: }
057: }
058:
059: public Collection getSections() {
060: ArrayList sections = new ArrayList();
061:
062: for (Iterator iter = _cm.getGlobalContext().getRoot()
063: .getDirectories().iterator(); iter.hasNext();) {
064: LinkedRemoteFileInterface dir = (LinkedRemoteFileInterface) iter
065: .next();
066: sections.add(new Section(dir));
067: }
068:
069: return sections;
070: }
071:
072: public SectionInterface lookup(String string) {
073: StringTokenizer st = new StringTokenizer(string, "/");
074:
075: if (!st.hasMoreTokens()) {
076: return new Section(_cm.getGlobalContext().getRoot());
077: }
078:
079: try {
080: return new Section(_cm.getGlobalContext().getRoot()
081: .getFile(st.nextToken()));
082: } catch (FileNotFoundException e) {
083: return new Section(_cm.getGlobalContext().getRoot());
084: }
085: }
086:
087: public void reload() {
088: }
089:
090: public SectionInterface lookup(LinkedRemoteFileInterface file) {
091: return lookup(file.getPath());
092: }
093:
094: public class Section implements SectionInterface {
095: private LinkedRemoteFileInterface _lrf;
096:
097: public Section(LinkedRemoteFileInterface lrf) {
098: _lrf = lrf;
099: }
100:
101: public LinkedRemoteFileInterface getFile() {
102: return _lrf;
103: }
104:
105: public Collection getFiles() {
106: return Collections.singletonList(_lrf);
107: }
108:
109: public LinkedRemoteFileInterface getFirstDirInSection(
110: LinkedRemoteFileInterface dir) {
111: try {
112: return FileUtils.getSubdirOfDirectory(getFile(), dir);
113: } catch (FileNotFoundException e) {
114: return dir;
115: }
116: }
117:
118: public String getName() {
119: return _lrf.getName();
120: }
121:
122: public String getPath() {
123: return _lrf.getPath();
124: }
125:
126: public LinkedRemoteFileInterface getBaseFile() {
127: return getFile();
128: }
129:
130: public String getBasePath() {
131: return getPath();
132: }
133: }
134: }
|