1 22 package org.jboss.virtual.plugins.context; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.ObjectStreamField ; 28 import java.io.ObjectInputStream.GetField; 29 import java.io.ObjectOutputStream.PutField; 30 import java.net.MalformedURLException ; 31 import java.net.URI ; 32 import java.net.URISyntaxException ; 33 import java.net.URL ; 34 import java.util.List ; 35 import java.util.concurrent.atomic.AtomicInteger ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.virtual.VFSUtils; 39 import org.jboss.virtual.VirtualFile; 40 import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer; 41 import org.jboss.virtual.spi.VFSContext; 42 import org.jboss.virtual.spi.VFSContextFactory; 43 import org.jboss.virtual.spi.VFSContextFactoryLocator; 44 import org.jboss.virtual.spi.VirtualFileHandler; 45 46 53 public abstract class AbstractVirtualFileHandler implements VirtualFileHandler 54 { 55 56 protected Logger log = Logger.getLogger(getClass()); 57 58 private static final long serialVersionUID = 1L; 59 60 private static final ObjectStreamField [] serialPersistentFields = { 61 new ObjectStreamField ("rootURI", URI .class), 62 new ObjectStreamField ("parent", VirtualFileHandler.class), 63 new ObjectStreamField ("name", String .class) 64 }; 65 66 69 private VFSContext context; 70 71 74 private VirtualFileHandler parent; 75 76 79 private String name; 80 81 82 private transient String vfsPath; 83 84 85 private transient AtomicInteger references = new AtomicInteger (0); 86 87 95 protected AbstractVirtualFileHandler(VFSContext context, VirtualFileHandler parent, String name) 96 { 97 if (context == null) 98 throw new IllegalArgumentException ("Null context"); 99 if (name == null) 100 throw new IllegalArgumentException ("Null name"); 101 this.context = context; 102 this.parent = parent; 103 this.name = VFSUtils.fixName(name); 104 } 105 106 public String getName() 107 { 108 return name; 109 } 110 111 public String getPathName() 112 { 113 if (vfsPath == null) 114 { 115 StringBuilder pathName = new StringBuilder (); 116 initPath(pathName); 117 vfsPath = pathName.toString(); 118 } 119 return vfsPath; 120 } 121 122 public URL toURL() throws MalformedURLException , URISyntaxException 123 { 124 return toURI().toURL(); 125 } 126 127 133 private boolean initPath(StringBuilder pathName) 134 { 135 if (parent != null) 136 { 137 if (parent instanceof AbstractVirtualFileHandler) 138 { 139 AbstractVirtualFileHandler handler = (AbstractVirtualFileHandler) parent; 140 if (handler.initPath(pathName)) 141 pathName.append('/'); 142 } 143 else 144 { 145 pathName.append(parent.getPathName()); 146 } 147 pathName.append(getName()); 148 return true; 149 } 150 return false; 151 } 152 153 public VirtualFile getVirtualFile() 154 { 155 checkClosed(); 156 increment(); 157 return new VirtualFile(this); 158 } 159 160 public VirtualFileHandler getParent() throws IOException 161 { 162 checkClosed(); 163 return parent; 164 } 165 166 public VFSContext getVFSContext() 167 { 168 checkClosed(); 169 return context; 170 } 171 172 177 private int increment() 178 { 179 return references.incrementAndGet(); 180 } 181 182 187 private int decrement() 188 { 189 return references.decrementAndGet(); 190 } 191 192 197 protected void checkClosed() throws IllegalStateException 198 { 199 if (references.get() < 0) 200 throw new IllegalStateException ("Closed " + this); 201 } 202 203 public void close() 204 { 205 if (decrement() == 0) 206 doClose(); 207 } 208 209 212 protected void doClose() 213 { 214 } 216 217 225 public VirtualFileHandler structuredFindChild(String path) throws IOException 226 { 227 checkClosed(); 228 229 String [] tokens = PathTokenizer.getTokens(path); 231 if (tokens == null || tokens.length == 0) 232 return this; 233 234 VirtualFileHandler current = this; 237 for (int i = 0; i < tokens.length; ++i) 238 { 239 if (current.isLeaf()) 240 throw new IOException ("File cannot have children: " + current); 241 if (current instanceof StructuredVirtualFileHandler) 242 { 243 StructuredVirtualFileHandler structured = (StructuredVirtualFileHandler) current; 244 current = structured.createChildHandler(tokens[i]); 245 } 246 else 247 { 248 String remainingPath = PathTokenizer.getRemainingPath(tokens, i); 249 return current.findChild(remainingPath); 250 } 251 } 252 253 return current; 255 } 256 257 265 public VirtualFileHandler simpleFindChild(String path) throws IOException 266 { 267 if (path == null) 268 throw new IllegalArgumentException ("Null path"); 269 270 if (path.length() == 0) 271 return this; 272 273 List <VirtualFileHandler> children = getChildren(false); 274 for (VirtualFileHandler child : children) 275 { 276 if (child.getName().equals(path)) 277 return child; 278 } 279 throw new IOException ("Child not found " + path + " for " + this); 280 } 281 282 @Override 283 public String toString() 284 { 285 StringBuilder buffer = new StringBuilder (); 286 buffer.append(getClass().getSimpleName()); 287 buffer.append('@'); 288 buffer.append(System.identityHashCode(this)); 289 buffer.append("[path=").append(getPathName()); 290 buffer.append(" context=").append(context.getRootURI()); 291 buffer.append(" real=").append(safeToURLString()); 292 buffer.append(']'); 293 return buffer.toString(); 294 } 295 296 @Override 297 public int hashCode() 298 { 299 return getPathName().hashCode(); 300 } 301 302 @Override 303 public boolean equals(Object obj) 304 { 305 if (this == obj) 306 return true; 307 if (obj == null || obj instanceof VirtualFileHandler == false) 308 return false; 309 VirtualFileHandler other = (VirtualFileHandler) obj; 310 if (getVFSContext().equals(other.getVFSContext()) == false) 311 return false; 312 if (getPathName().equals(other.getPathName()) == false) 313 return false; 314 return true; 315 } 316 317 @Override 318 protected void finalize() throws Throwable 319 { 320 close(); 321 } 322 323 328 private String safeToURLString() 329 { 330 try 331 { 332 return toURI().toString(); 333 } 334 catch (URISyntaxException ignored) 335 { 336 return "<unknown>"; 337 } 338 } 339 340 private void writeObject(ObjectOutputStream out) 341 throws IOException 342 { 343 PutField fields = out.putFields(); 344 fields.put("rootURI", this.getVFSContext().getRootURI()); 345 fields.put("parent", parent); 346 fields.put("name", name); 347 out.writeFields(); 348 } 349 private void readObject(ObjectInputStream in) 350 throws IOException , ClassNotFoundException 351 { 352 GetField fields = in.readFields(); 354 URI rootURI = (URI ) fields.get("rootURI", null); 355 this.parent = (VirtualFileHandler) fields.get("parent", null); 356 this.name = (String ) fields.get("name", null); 357 VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURI); 358 this.context = factory.getVFS(rootURI); 359 this.references = new AtomicInteger (0); 360 log = Logger.getLogger(getClass()); 362 } 363 } 364 | Popular Tags |