KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > remotefile > JDOMRemoteFile


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 net.sf.drftpd.remotefile;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import net.sf.drftpd.FatalException;
27 import net.sf.drftpd.master.RemoteSlave;
28
29 import org.apache.log4j.Level;
30 import org.apache.log4j.Logger;
31 import org.jdom.Element;
32 import org.jdom.output.XMLOutputter;
33
34 /**
35  * @author mog
36  * @version $Id: JDOMRemoteFile.java,v 1.29 2004/05/12 00:45:10 mog Exp $
37  */

38 public class JDOMRemoteFile implements RemoteFileInterface {
39
40     private static final Logger logger =
41         Logger.getLogger(JDOMRemoteFile.class.getName());
42
43     Hashtable JavaDoc allSlaves;
44     private long checkSum;
45     protected List JavaDoc files = null;
46     private String JavaDoc group;
47     protected long lastModified;
48
49     protected long length;
50
51     // protected String path;
52
protected String JavaDoc name;
53     private String JavaDoc owner;
54     protected Collection JavaDoc slaves;
55     private long xfertime;
56     
57     /**
58      * Constructor for JDOMRemoteFileTree.
59      */

60     public JDOMRemoteFile(Element element, Collection JavaDoc rslaves) {
61         this(element, RemoteSlave.rslavesToHashtable(rslaves));
62     }
63
64     public JDOMRemoteFile(Element element, Hashtable JavaDoc allSlaves) {
65         try {
66         this.allSlaves = allSlaves;
67         this.name = element.getAttributeValue("name");
68         if (element.getName().equals("directory")) {
69             this.files = element.getChild("contents").getChildren();
70         }
71         else if (element.getName().equals("file")) {
72             try {
73             this.xfertime = Long.parseLong(element.getChildText("xfertime"));
74             } catch(NumberFormatException JavaDoc ex) {
75                 this.xfertime = 0L;
76             }
77             try {
78                 this.checkSum =
79                 Long.parseLong(element.getChildText("checksum"), 16);
80             } catch(NumberFormatException JavaDoc e) {
81             }
82             this.length = Long.parseLong(element.getChildText("size"));
83
84             this.slaves = new ArrayList JavaDoc();
85             for (Iterator JavaDoc iter =
86                 element.getChild("slaves").getChildren("slave").iterator();
87                 iter.hasNext();
88                 ) {
89                 Element slaveElement = (Element) iter.next();
90                 String JavaDoc slaveName = slaveElement.getText();
91                 if(slaveName == null) throw new NullPointerException JavaDoc(slaveElement+" : slaveElement.getText() returned null");
92                 RemoteSlave rslave = (RemoteSlave) this.allSlaves.get(slaveName);
93                 if (rslave == null) {
94                     logger.log(
95                         Level.WARN,
96                         slaveName
97                             + " not in slavelist, not adding file: "
98                             + getName());
99                     continue;
100                 }
101                 // don't add duplicate slaves. shouldn't happen.
102
if(!this.slaves.contains(rslave)) this.slaves.add(rslave);
103             }
104         }
105         this.owner = element.getChild("user").getText();
106         this.group = element.getChild("group").getText();
107         this.lastModified =
108             Long.parseLong(element.getChild("lastModified").getText());
109
110         } catch(NumberFormatException JavaDoc ex) {
111             throw new FatalException(this+" has missing fields, try switching to files.xml.bak", ex);
112         }
113     }
114
115     public long getCheckSumCached() {
116         return this.checkSum;
117     }
118
119     public Collection JavaDoc getFiles() {
120         ArrayList JavaDoc listFiles = new ArrayList JavaDoc(files.size());
121         for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
122             listFiles.add(
123                 new JDOMRemoteFile((Element) i.next(), this.allSlaves));
124         }
125         return listFiles;
126     }
127
128     public String JavaDoc getGroupname() {
129         return this.group;
130     }
131
132     public RemoteFileInterface getLink() {
133         throw new UnsupportedOperationException JavaDoc();
134     }
135
136     public String JavaDoc getLinkPath() {
137         throw new UnsupportedOperationException JavaDoc();
138     }
139
140     public String JavaDoc getName() {
141         return name;
142     }
143
144     public String JavaDoc getParent() {
145         throw new UnsupportedOperationException JavaDoc("JDOMRemoteFile.getParent() not implemented");
146     }
147
148     public String JavaDoc getPath() {
149         throw new UnsupportedOperationException JavaDoc("JDOMRemoteFile.getPath() not implemented");
150     }
151
152     public Collection JavaDoc getSlaves() {
153         return this.slaves;
154     }
155
156     public String JavaDoc getUsername() {
157         return this.owner;
158     }
159
160     public long getXfertime() {
161         return xfertime;
162     }
163
164     public boolean isDeleted() {
165         return false;
166     }
167
168     public boolean isDirectory() {
169         return files != null;
170     }
171     public boolean isFile() {
172         return files == null;
173     }
174
175     public boolean isLink() {
176         return false;
177     }
178
179     public long lastModified() {
180         return this.lastModified;
181     }
182
183     public long length() {
184         return this.length;
185     }
186
187     public RemoteFileInterface[] listFiles() {
188         ArrayList JavaDoc listFiles = new ArrayList JavaDoc();
189         for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
190             Element fileElement = (Element)i.next();
191             
192             if(fileElement.getName().equals("file") && fileElement.getChild("slaves").getChildren().size() == 0) {
193                 logger.warn(new XMLOutputter().outputString(fileElement)+" has no slaves! skipping");
194                 continue;
195             }
196             listFiles.add(new JDOMRemoteFile(fileElement, this.allSlaves));
197         }
198         return (RemoteFileInterface[])listFiles.toArray(new JDOMRemoteFile[0]);
199     }
200
201     public String JavaDoc toString() {
202         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
203         ret.append("[" + getClass().getName() + "[");
204         //ret.append(slaves);
205
if (isDirectory())
206             ret.append("[directory: " + getFiles().size()+ "]");
207         if (isFile())
208             ret.append("[file: true]");
209         //ret.append("isFile(): " + isFile() + " ");
210
ret.append(getName());
211         return ret.toString();
212     }
213
214 }
215
Popular Tags