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.plugins;
019:
020: import java.io.FileNotFoundException;
021: import java.util.regex.Matcher;
022: import java.util.regex.Pattern;
023:
024: import net.sf.drftpd.NoAvailableSlaveException;
025: import net.sf.drftpd.SlaveUnavailableException;
026:
027: import org.apache.log4j.Logger;
028: import org.drftpd.master.RemoteSlave;
029: import org.drftpd.remotefile.LinkedRemoteFileInterface;
030: import org.drftpd.slave.RemoteIOException;
031:
032: public class DIZFile {
033: private static final Logger logger = Logger
034: .getLogger(DIZFile.class);
035:
036: private LinkedRemoteFileInterface _file;
037:
038: private LinkedRemoteFileInterface _parent;
039:
040: private String _diz;
041:
042: private String _name;
043:
044: private int _total;
045:
046: public DIZFile(LinkedRemoteFileInterface file)
047: throws FileNotFoundException, NoAvailableSlaveException {
048: setFile(file);
049: setParent(file);
050: setName(file.getName());
051: setDiz(fetchDiz());
052: setTotal(fetchTotal());
053: }
054:
055: private void setFile(LinkedRemoteFileInterface file) {
056: _file = file;
057: }
058:
059: private void setParent(LinkedRemoteFileInterface file)
060: throws FileNotFoundException {
061: _parent = file.getParentFile();
062:
063: }
064:
065: private void setDiz(String diz) {
066: _diz = diz;
067: }
068:
069: private void setName(String name) {
070: _name = name;
071: }
072:
073: private void setTotal(int total) {
074: _total = total;
075: }
076:
077: public LinkedRemoteFileInterface getFile() {
078: return _file;
079: }
080:
081: public LinkedRemoteFileInterface getParent() {
082: return _parent;
083: }
084:
085: public String getDiz() {
086: return _diz;
087: }
088:
089: public String getName() {
090: return _name;
091: }
092:
093: public int getTotal() {
094: return _total;
095: }
096:
097: public String fetchDiz() throws NoAvailableSlaveException,
098: FileNotFoundException {
099: RemoteSlave aSlave = _file.getAvailableSlaves().iterator()
100: .next();
101: try {
102: return aSlave.fetchDIZFileFromIndex(aSlave
103: .issueDIZFileToSlave(_file));
104: } catch (RemoteIOException e) {
105: if (e.getCause() instanceof FileNotFoundException) {
106: throw (FileNotFoundException) e.getCause();
107: }
108: aSlave.setOffline(e);
109: throw new NoAvailableSlaveException();
110: } catch (SlaveUnavailableException e) {
111: throw new NoAvailableSlaveException();
112: }
113: }
114:
115: public int fetchTotal() {
116: Integer total;
117: Matcher m;
118: Pattern p;
119: String regex;
120:
121: regex = "[\\[\\(\\<\\:\\s][0-9oOxX]*/([0-9oOxX]*[0-9])[\\]\\)\\>\\s]";
122:
123: p = Pattern.compile(regex);
124:
125: // Compare the diz file to the pattern compiled above
126: m = p.matcher(_diz);
127:
128: // We found the pattern in this diz file!
129: if (m.find()) {
130: total = new Integer(m.group(1).replaceAll("[oOxX]", "0"));
131: } else {
132: logger.warn("Could not retrieve total from dizFile");
133: setTotal(0);
134: return 0;
135: }
136:
137: setTotal(total.intValue());
138:
139: return getTotal();
140: }
141: }
|