1 22 package org.jboss.virtual.plugins.context; 23 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.net.URL ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.jboss.logging.Logger; 32 import org.jboss.virtual.VFS; 33 import org.jboss.virtual.VFSUtils; 34 import org.jboss.virtual.VirtualFile; 35 import org.jboss.virtual.VirtualFileFilter; 36 import org.jboss.virtual.VisitorAttributes; 37 import org.jboss.virtual.spi.VFSContext; 38 import org.jboss.virtual.spi.VirtualFileHandler; 39 import org.jboss.virtual.spi.VirtualFileHandlerVisitor; 40 41 48 public abstract class AbstractVFSContext implements VFSContext 49 { 50 51 protected final Logger log = Logger.getLogger(getClass()); 52 53 54 private VFS vfs; 55 56 57 private final URI rootURI; 58 59 private Map <String , String > rootOptions; 60 61 67 protected AbstractVFSContext(URI rootURI) 68 { 69 if (rootURI == null) 70 throw new IllegalArgumentException ("Null rootURI"); 71 this.rootURI = rootURI; 72 String query = rootURI.getQuery(); 73 rootOptions = VFSUtils.parseURLQuery(query); 74 } 75 82 protected AbstractVFSContext(URL rootURL) 83 throws URISyntaxException 84 { 85 this(rootURL.toURI()); 86 } 87 88 public VFS getVFS() 89 { 90 if (vfs == null) 91 vfs = new VFS(this); 92 return vfs; 93 } 94 95 public URI getRootURI() 96 { 97 return rootURI; 98 } 99 100 public Map <String , String > getOptions() 101 { 102 return rootOptions; 103 } 104 105 public List <VirtualFileHandler> getChildren(VirtualFileHandler parent, boolean ignoreErrors) throws IOException 106 { 107 if (parent == null) 108 throw new IllegalArgumentException ("Null parent"); 109 return parent.getChildren(ignoreErrors); 110 } 111 112 public VirtualFileHandler findChild(VirtualFileHandler parent, String path) throws IOException 113 { 114 if (parent == null) 115 throw new IllegalArgumentException ("Null parent"); 116 if (path == null) 117 throw new IllegalArgumentException ("Null path"); 118 return parent.findChild(path); 119 } 120 121 public void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor) throws IOException 122 { 123 if (handler == null) 124 throw new IllegalArgumentException ("Null handler"); 125 if (visitor == null) 126 throw new IllegalArgumentException ("Null visitor"); 127 128 VisitorAttributes attributes = visitor.getAttributes(); 129 boolean includeRoot = attributes.isIncludeRoot(); 130 boolean leavesOnly = attributes.isLeavesOnly(); 131 boolean ignoreErrors = attributes.isIgnoreErrors(); 132 boolean includeHidden = attributes.isIncludeHidden(); 133 VirtualFileFilter recurseFilter = attributes.getRecurseFilter(); 134 visit(handler, visitor, includeRoot, leavesOnly, ignoreErrors, 135 includeHidden, recurseFilter); 136 } 137 138 150 protected void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor, 151 boolean includeRoot, boolean leavesOnly, boolean ignoreErrors, 152 boolean includeHidden, VirtualFileFilter recurseFilter) 153 throws IOException 154 { 155 if (includeRoot) 157 visitor.visit(handler); 158 159 boolean trace = log.isTraceEnabled(); 161 List <VirtualFileHandler> children; 162 try 163 { 164 children = getChildren(handler, ignoreErrors); 165 } 166 catch (IOException e) 167 { 168 if (ignoreErrors == false) 169 throw e; 170 if( trace ) 171 log.trace("Ignored: " + e); 172 return; 173 } 174 175 for (VirtualFileHandler child : children) 177 { 178 if (includeHidden == false && child.isHidden()) 180 { 181 if( trace ) 182 log.trace("Ignoring hidden file: "+child); 183 continue; 184 } 185 186 boolean isLeaf = child.isLeaf(); 188 if (leavesOnly == false || isLeaf) 189 visitor.visit(child); 190 else if( trace ) 191 { 192 log.trace("Skipping non-leaf file: "+child); 193 } 194 195 VirtualFile file = child.getVirtualFile(); 197 if ( isLeaf == false && recurseFilter != null && recurseFilter.accepts(file)) 198 { 199 try 200 { 201 visit(child, visitor, false, leavesOnly, ignoreErrors, includeHidden, recurseFilter); 202 } 203 catch (StackOverflowError e) 204 { 205 log.debug("Original: " + child, e); 206 throw new IOException ("Stack overflow, the file system is too complicated? " + child); 207 } 208 } 209 } 210 } 211 212 @Override 213 public String toString() 214 { 215 StringBuilder buffer = new StringBuilder (); 216 buffer.append(getClass().getSimpleName()); 217 buffer.append('@'); 218 buffer.append(System.identityHashCode(this)); 219 buffer.append('['); 220 buffer.append(rootURI); 221 buffer.append(']'); 222 return buffer.toString(); 223 } 224 225 @Override 226 public int hashCode() 227 { 228 return rootURI.hashCode(); 229 } 230 231 @Override 232 public boolean equals(Object obj) 233 { 234 if (this == obj) 235 return true; 236 if (obj == null || obj instanceof VFSContext == false) 237 return false; 238 VFSContext other = (VFSContext) obj; 239 return rootURI.equals(other.getRootURI()); 240 } 241 } 242 | Popular Tags |