1 22 package org.jboss.virtual ; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.Serializable ; 27 import java.net.MalformedURLException ; 28 import java.net.URI ; 29 import java.net.URISyntaxException ; 30 import java.net.URL ; 31 import java.util.Collections ; 32 import java.util.List ; 33 import java.util.Set ; 34 import java.util.concurrent.atomic.AtomicBoolean ; 35 36 import org.jboss.util.collection.WeakSet; 37 import org.jboss.virtual .plugins.vfs.helpers.FilterVirtualFileVisitor; 38 import org.jboss.virtual .plugins.vfs.helpers.MatchAllVirtualFileFilter; 39 import org.jboss.virtual .spi.VFSContext; 40 import org.jboss.virtual .spi.VirtualFileHandler; 41 42 49 public class VirtualFile implements Serializable 50 { 51 private static final long serialVersionUID = 1L; 52 53 54 private final VirtualFileHandler handler; 55 56 57 private AtomicBoolean closed = new AtomicBoolean (false); 58 59 60 private transient final Set <InputStream > streams = Collections.synchronizedSet(new WeakSet()); 61 62 68 public VirtualFile(VirtualFileHandler handler) 69 { 70 if (handler == null) 71 throw new IllegalArgumentException ("Null handler"); 72 this.handler = handler; 73 } 74 75 81 protected VirtualFileHandler getHandler() 82 { 83 if (closed.get()) 84 throw new IllegalStateException ("The virtual file is closed"); 85 return handler; 86 } 87 88 94 public String getName() 95 { 96 return getHandler().getName(); 97 } 98 99 105 public String getPathName() 106 { 107 return getHandler().getPathName(); 108 } 109 110 118 public URL toURL() throws MalformedURLException , URISyntaxException 119 { 120 return getHandler().toURL(); 121 } 122 123 130 public URI toURI() throws URISyntaxException 131 { 132 return getHandler().toURI(); 133 } 134 135 142 public long getLastModified() throws IOException 143 { 144 return getHandler().getLastModified(); 145 } 146 147 154 public long getSize() throws IOException 155 { 156 return getHandler().getSize(); 157 } 158 159 167 public boolean isLeaf() throws IOException 168 { 169 return getHandler().isLeaf(); 170 } 171 172 179 public boolean isHidden() throws IOException 180 { 181 return getHandler().isHidden(); 182 } 183 184 191 public InputStream openStream() throws IOException 192 { 193 InputStream result = getHandler().openStream(); 194 streams.add(result); 195 return result; 196 } 197 198 201 public void closeStreams() 202 { 203 for (InputStream stream : streams) 205 { 206 if (stream != null) 207 { 208 try 209 { 210 stream.close(); 211 } 212 catch (IOException ignored) 213 { 214 } 215 } 216 } 217 streams.clear(); 218 } 219 220 223 public void close() 224 { 225 if (closed.getAndSet(true) == false) 226 { 227 closeStreams(); 228 handler.close(); 229 } 230 } 231 232 238 public VFS getVFS() 239 { 240 VFSContext context = getHandler().getVFSContext(); 241 return context.getVFS(); 242 } 243 244 251 public VirtualFile getParent() throws IOException 252 { 253 VirtualFileHandler parent = getHandler().getParent(); 254 if (parent != null) 255 return parent.getVirtualFile(); 256 return null; 257 } 258 259 266 public List <VirtualFile> getChildren() throws IOException 267 { 268 return getChildren(null); 269 } 270 271 279 public List <VirtualFile> getChildren(VirtualFileFilter filter) throws IOException 280 { 281 if (isLeaf()) 282 throw new IllegalStateException ("File cannot contain children: " + this); 283 284 if (filter == null) 285 filter = MatchAllVirtualFileFilter.INSTANCE; 286 FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, null); 287 visit(visitor); 288 return visitor.getMatched(); 289 } 290 291 300 public List <VirtualFile> getChildrenRecursively() throws IOException 301 { 302 return getChildrenRecursively(null); 303 } 304 305 315 public List <VirtualFile> getChildrenRecursively(VirtualFileFilter filter) throws IOException 316 { 317 if (isLeaf()) 318 throw new IllegalStateException ("File cannot contain children: " + this); 319 320 if (filter == null) 321 filter = MatchAllVirtualFileFilter.INSTANCE; 322 FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, VisitorAttributes.RECURSE); 323 visit(visitor); 324 return visitor.getMatched(); 325 } 326 327 335 public void visit(VirtualFileVisitor visitor) throws IOException 336 { 337 if (isLeaf()) 338 throw new IllegalStateException ("File cannot contain children: " + this); 339 340 getVFS().visit(this, visitor); 341 } 342 343 352 public VirtualFile findChild(String path) throws IOException 353 { 354 VirtualFileHandler handler = getHandler(); 355 356 if (handler.isLeaf()) 357 throw new IllegalStateException ("File cannot contain children: " + this); 358 359 path = VFSUtils.fixName(path); 360 VirtualFileHandler child = handler.findChild(path); 361 return child.getVirtualFile(); 362 } 363 364 @Override 365 public String toString() 366 { 367 return handler.toString(); 368 } 369 370 @Override 371 public int hashCode() 372 { 373 return handler.hashCode(); 374 } 375 376 @Override 377 public boolean equals(Object obj) 378 { 379 if (obj == this) 380 return true; 381 if (obj == null || obj instanceof VirtualFile == false) 382 return false; 383 VirtualFile other = (VirtualFile) obj; 384 return handler.equals(other.handler); 385 } 386 387 @Override 388 protected void finalize() throws Throwable 389 { 390 close(); 391 } 392 } 393 | Popular Tags |