1 16 package org.apache.commons.vfs.provider; 17 18 import org.apache.commons.vfs.FileChangeEvent; 19 import org.apache.commons.vfs.FileListener; 20 import org.apache.commons.vfs.FileName; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystemException; 23 import org.apache.commons.vfs.FileType; 24 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.security.cert.Certificate ; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 39 public class DelegateFileObject 40 extends AbstractFileObject 41 implements FileListener 42 { 43 private FileObject file; 44 private final Set children = new HashSet (); 45 private boolean ignoreEvent; 46 47 public DelegateFileObject(final FileName name, 48 final AbstractFileSystem fileSystem, 49 final FileObject file) throws FileSystemException 50 { 51 super(name, fileSystem); 52 this.file = file; 53 if (file != null) 54 { 55 file.getFileSystem().addListener(file, this); 56 } 57 } 58 59 62 public void attachChild(final FileName baseName, final FileType type) throws Exception 63 { 64 final FileType oldType = doGetType(); 65 if (children.add(baseName)) 66 { 67 childrenChanged(baseName, type); 68 } 69 maybeTypeChanged(oldType); 70 } 71 72 75 public void setFile(final FileObject file) throws Exception 76 { 77 final FileType oldType = doGetType(); 78 79 if (file != null) 80 { 81 file.getFileSystem().addListener(file, this); 82 } 83 this.file = file; 84 maybeTypeChanged(oldType); 85 } 86 87 91 private void maybeTypeChanged(final FileType oldType) throws Exception 92 { 93 final FileType newType = doGetType(); 94 if (oldType == FileType.IMAGINARY && newType != FileType.IMAGINARY) 95 { 96 handleCreate(newType); 97 } 98 else if (oldType != FileType.IMAGINARY && newType == FileType.IMAGINARY) 99 { 100 handleDelete(); 101 } 102 } 103 104 108 protected FileType doGetType() throws FileSystemException 109 { 110 if (file != null) 111 { 112 return file.getType(); 113 } 114 else if (children.size() > 0) 115 { 116 return FileType.FOLDER; 117 } 118 else 119 { 120 return FileType.IMAGINARY; 121 } 122 } 123 124 127 protected boolean doIsReadable() throws FileSystemException 128 { 129 if (file != null) 130 { 131 return file.isReadable(); 132 } 133 else 134 { 135 return true; 136 } 137 } 138 139 142 protected boolean doIsWriteable() throws FileSystemException 143 { 144 if (file != null) 145 { 146 return file.isWriteable(); 147 } 148 else 149 { 150 return false; 151 } 152 } 153 154 157 protected boolean doIsHidden() throws FileSystemException 158 { 159 if (file != null) 160 { 161 return file.isHidden(); 162 } 163 else 164 { 165 return false; 166 } 167 } 168 169 172 protected String [] doListChildren() throws Exception 173 { 174 if (file != null) 175 { 176 final FileObject[] children = file.getChildren(); 177 final String [] childNames = new String [children.length]; 178 for (int i = 0; i < children.length; i++) 179 { 180 childNames[i] = children[i].getName().getBaseName(); 181 } 182 return childNames; 183 } 184 else 185 { 186 return (String []) children.toArray(new String [children.size()]); 187 } 188 } 189 190 193 protected void doCreateFolder() throws Exception 194 { 195 ignoreEvent = true; 196 try 197 { 198 file.createFolder(); 199 } 200 finally 201 { 202 ignoreEvent = false; 203 } 204 } 205 206 209 protected void doDelete() throws Exception 210 { 211 ignoreEvent = true; 212 try 213 { 214 file.delete(); 215 } 216 finally 217 { 218 ignoreEvent = false; 219 } 220 } 221 222 226 protected long doGetContentSize() throws Exception 227 { 228 return file.getContent().getSize(); 229 } 230 231 234 protected Map doGetAttributes() 235 throws Exception 236 { 237 return file.getContent().getAttributes(); 238 } 239 240 243 protected void doSetAttribute(final String atttrName, 244 final Object value) 245 throws Exception 246 { 247 file.getContent().setAttribute(atttrName, value); 248 } 249 250 253 protected Certificate [] doGetCertificates() throws Exception 254 { 255 return file.getContent().getCertificates(); 256 } 257 258 261 protected long doGetLastModifiedTime() throws Exception 262 { 263 return file.getContent().getLastModifiedTime(); 264 } 265 266 269 protected void doSetLastModifiedTime(final long modtime) 270 throws Exception 271 { 272 file.getContent().setLastModifiedTime(modtime); 273 } 274 275 278 protected InputStream doGetInputStream() throws Exception 279 { 280 return file.getContent().getInputStream(); 281 } 282 283 286 protected OutputStream doGetOutputStream(boolean bAppend) throws Exception 287 { 288 return file.getContent().getOutputStream(bAppend); 289 } 290 291 294 public void fileCreated(final FileChangeEvent event) throws Exception 295 { 296 if (!ignoreEvent) 297 { 298 handleCreate(file.getType()); 299 } 300 } 301 302 305 public void fileDeleted(final FileChangeEvent event) throws Exception 306 { 307 if (!ignoreEvent) 308 { 309 handleDelete(); 310 } 311 } 312 313 318 public void fileChanged(FileChangeEvent event) throws Exception 319 { 320 if (!ignoreEvent) 321 { 322 handleChanged(); 323 } 324 } 325 326 329 public void close() throws FileSystemException 330 { 331 super.close(); 332 333 if (file != null) 334 { 335 file.close(); 336 } 337 } 338 } 339 | Popular Tags |