KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > AbstractVFSContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.virtual.plugins.context;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
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 /**
42  * AbstractVFSContext.
43  *
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 1.1 $
47  */

48 public abstract class AbstractVFSContext implements VFSContext
49 {
50    /** The log */
51    protected final Logger log = Logger.getLogger(getClass());
52    
53    /** The VFS wrapper */
54    private VFS vfs;
55    
56    /** The root url */
57    private final URI JavaDoc rootURI;
58    /** Options associated with the root URL */
59    private Map JavaDoc<String JavaDoc, String JavaDoc> rootOptions;
60
61    /**
62     * Create a new AbstractVFSContext.
63     *
64     * @param rootURI the root url
65     * @throws IllegalArgumentException if rootURI is null
66     */

67    protected AbstractVFSContext(URI JavaDoc rootURI)
68    {
69       if (rootURI == null)
70          throw new IllegalArgumentException JavaDoc("Null rootURI");
71       this.rootURI = rootURI;
72       String JavaDoc query = rootURI.getQuery();
73       rootOptions = VFSUtils.parseURLQuery(query);
74    }
75    /**
76     * Create a new AbstractVFSContext.
77     *
78     * @param rootURL the root url
79     * @throws URISyntaxException
80     * @throws IllegalArgumentException if rootURI is null
81     */

82    protected AbstractVFSContext(URL JavaDoc rootURL)
83       throws URISyntaxException JavaDoc
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 JavaDoc getRootURI()
96    {
97       return rootURI;
98    }
99
100    public Map JavaDoc<String JavaDoc, String JavaDoc> getOptions()
101    {
102       return rootOptions;
103    }
104
105    public List JavaDoc<VirtualFileHandler> getChildren(VirtualFileHandler parent, boolean ignoreErrors) throws IOException JavaDoc
106    {
107       if (parent == null)
108          throw new IllegalArgumentException JavaDoc("Null parent");
109       return parent.getChildren(ignoreErrors);
110    }
111
112    public VirtualFileHandler findChild(VirtualFileHandler parent, String JavaDoc path) throws IOException JavaDoc
113    {
114       if (parent == null)
115          throw new IllegalArgumentException JavaDoc("Null parent");
116       if (path == null)
117          throw new IllegalArgumentException JavaDoc("Null path");
118       return parent.findChild(path);
119    }
120
121    public void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor) throws IOException JavaDoc
122    {
123       if (handler == null)
124          throw new IllegalArgumentException JavaDoc("Null handler");
125       if (visitor == null)
126          throw new IllegalArgumentException JavaDoc("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    /**
139     * Visit. the file system, recursive death checking is left to the visitor
140     * or otherwise a stack overflow.
141     *
142     * @param handler the reference handler
143     * @param visitor the visitor
144     * @param includeRoot whether to visit the root
145     * @param leavesOnly whether to visit leaves only
146     * @param ignoreErrors whether to ignore errors
147     * @param includeHidden whether to include hidden files
148     * @throws IOException for any problem accessing the virtual file system
149     */

150    protected void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor,
151          boolean includeRoot, boolean leavesOnly, boolean ignoreErrors,
152          boolean includeHidden, VirtualFileFilter recurseFilter)
153       throws IOException JavaDoc
154    {
155       // Visit the root when asked
156
if (includeRoot)
157          visitor.visit(handler);
158       
159       // Visit the children
160
boolean trace = log.isTraceEnabled();
161       List JavaDoc<VirtualFileHandler> children;
162       try
163       {
164           children = getChildren(handler, ignoreErrors);
165       }
166       catch (IOException JavaDoc e)
167       {
168          if (ignoreErrors == false)
169             throw e;
170          if( trace )
171             log.trace("Ignored: " + e);
172          return;
173       }
174       
175       // Look through each child
176
for (VirtualFileHandler child : children)
177       {
178          // Ignore hidden if asked
179
if (includeHidden == false && child.isHidden())
180          {
181             if( trace )
182                log.trace("Ignoring hidden file: "+child);
183             continue;
184          }
185          
186          // Visit the leaf or non-leaves when asked
187
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          // Recurse when asked
196
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 JavaDoc e)
204             {
205                log.debug("Original: " + child, e);
206                throw new IOException JavaDoc("Stack overflow, the file system is too complicated? " + child);
207             }
208          }
209       }
210    }
211    
212    @Override JavaDoc
213    public String JavaDoc toString()
214    {
215       StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
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 JavaDoc
226    public int hashCode()
227    {
228       return rootURI.hashCode();
229    }
230
231    @Override JavaDoc
232    public boolean equals(Object JavaDoc 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