1 18 package net.sf.drftpd.remotefile; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.List ; 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 38 public class JDOMRemoteFile implements RemoteFileInterface { 39 40 private static final Logger logger = 41 Logger.getLogger(JDOMRemoteFile.class.getName()); 42 43 Hashtable allSlaves; 44 private long checkSum; 45 protected List files = null; 46 private String group; 47 protected long lastModified; 48 49 protected long length; 50 51 protected String name; 53 private String owner; 54 protected Collection slaves; 55 private long xfertime; 56 57 60 public JDOMRemoteFile(Element element, Collection rslaves) { 61 this(element, RemoteSlave.rslavesToHashtable(rslaves)); 62 } 63 64 public JDOMRemoteFile(Element element, Hashtable 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 ex) { 75 this.xfertime = 0L; 76 } 77 try { 78 this.checkSum = 79 Long.parseLong(element.getChildText("checksum"), 16); 80 } catch(NumberFormatException e) { 81 } 82 this.length = Long.parseLong(element.getChildText("size")); 83 84 this.slaves = new ArrayList (); 85 for (Iterator iter = 86 element.getChild("slaves").getChildren("slave").iterator(); 87 iter.hasNext(); 88 ) { 89 Element slaveElement = (Element) iter.next(); 90 String slaveName = slaveElement.getText(); 91 if(slaveName == null) throw new NullPointerException (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 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 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 getFiles() { 120 ArrayList listFiles = new ArrayList (files.size()); 121 for (Iterator 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 getGroupname() { 129 return this.group; 130 } 131 132 public RemoteFileInterface getLink() { 133 throw new UnsupportedOperationException (); 134 } 135 136 public String getLinkPath() { 137 throw new UnsupportedOperationException (); 138 } 139 140 public String getName() { 141 return name; 142 } 143 144 public String getParent() { 145 throw new UnsupportedOperationException ("JDOMRemoteFile.getParent() not implemented"); 146 } 147 148 public String getPath() { 149 throw new UnsupportedOperationException ("JDOMRemoteFile.getPath() not implemented"); 150 } 151 152 public Collection getSlaves() { 153 return this.slaves; 154 } 155 156 public String 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 listFiles = new ArrayList (); 189 for (Iterator 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 toString() { 202 StringBuffer ret = new StringBuffer (); 203 ret.append("[" + getClass().getName() + "["); 204 if (isDirectory()) 206 ret.append("[directory: " + getFiles().size()+ "]"); 207 if (isFile()) 208 ret.append("[file: true]"); 209 ret.append(getName()); 211 return ret.toString(); 212 } 213 214 } 215 | Popular Tags |