KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.ObjectStreamField JavaDoc;
28 import java.io.ObjectInputStream.GetField;
29 import java.io.ObjectOutputStream.PutField;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URI JavaDoc;
32 import java.net.URISyntaxException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
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 /**
47  * AbstractVirtualFileHandler.
48  *
49  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
50  * @author Scott.Stark@jboss.org
51  * @version $Revision: 1.1 $
52  */

53 public abstract class AbstractVirtualFileHandler implements VirtualFileHandler
54 {
55    /** The log */
56    protected Logger log = Logger.getLogger(getClass());
57    /** serialVersionUID */
58    private static final long serialVersionUID = 1L;
59    /** The class serial fields */
60    private static final ObjectStreamField JavaDoc[] serialPersistentFields = {
61       new ObjectStreamField JavaDoc("rootURI", URI JavaDoc.class),
62       new ObjectStreamField JavaDoc("parent", VirtualFileHandler.class),
63       new ObjectStreamField JavaDoc("name", String JavaDoc.class)
64    };
65
66    /** The VFS context
67     * @serialField rootURI URI the VFS context rootURI
68     */

69    private VFSContext context;
70    
71    /** The parent
72     * @serialField parent VirtualFileHandler the virtual file parent
73     */

74    private VirtualFileHandler parent;
75
76    /** The name
77     * @serialField name String the virtual file name
78     */

79    private String JavaDoc name;
80
81    /** The vfsPath */
82    private transient String JavaDoc vfsPath;
83
84    /** The reference count */
85    private transient AtomicInteger JavaDoc references = new AtomicInteger JavaDoc(0);
86
87    /**
88     * Create a new handler
89     *
90     * @param context the context
91     * @param parent the parent
92     * @param name the name
93     * @throws IllegalArgumentException if the context or name is null;
94     */

95    protected AbstractVirtualFileHandler(VFSContext context, VirtualFileHandler parent, String JavaDoc name)
96    {
97       if (context == null)
98          throw new IllegalArgumentException JavaDoc("Null context");
99       if (name == null)
100          throw new IllegalArgumentException JavaDoc("Null name");
101       this.context = context;
102       this.parent = parent;
103       this.name = VFSUtils.fixName(name);
104    }
105
106    public String JavaDoc getName()
107    {
108       return name;
109    }
110
111    public String JavaDoc getPathName()
112    {
113       if (vfsPath == null)
114       {
115          StringBuilder JavaDoc pathName = new StringBuilder JavaDoc();
116          initPath(pathName);
117          vfsPath = pathName.toString();
118       }
119       return vfsPath;
120    }
121
122    public URL JavaDoc toURL() throws MalformedURLException JavaDoc, URISyntaxException JavaDoc
123    {
124       return toURI().toURL();
125    }
126
127    /**
128     * Initialise the path into the path name
129     *
130     * @param pathName the path name
131     * @return whether it added anything
132     */

133    private boolean initPath(StringBuilder JavaDoc 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 JavaDoc
161    {
162       checkClosed();
163       return parent;
164    }
165    
166    public VFSContext getVFSContext()
167    {
168       checkClosed();
169       return context;
170    }
171
172    /**
173     * Increment the reference count
174     *
175     * @return the resulting count
176     */

177    private int increment()
178    {
179       return references.incrementAndGet();
180    }
181
182    /**
183     * Decrement the reference count
184     *
185     * @return the resulting count
186     */

187    private int decrement()
188    {
189       return references.decrementAndGet();
190    }
191
192    /**
193     * Check whether we are closed
194     *
195     * @throws IllegalStateException when closed
196     */

197    protected void checkClosed() throws IllegalStateException JavaDoc
198    {
199       if (references.get() < 0)
200          throw new IllegalStateException JavaDoc("Closed " + this);
201    }
202    
203    public void close()
204    {
205       if (decrement() == 0)
206          doClose();
207    }
208
209    /**
210     * The real close
211     */

212    protected void doClose()
213    {
214       // nothing
215
}
216
217    /**
218     * Structured implementation of find child
219     *
220     * @param path the path
221     * @return the handler
222     * @throws IOException for any error accessing the virtual file system
223     * @throws IllegalArgumentException for a null name
224     */

225    public VirtualFileHandler structuredFindChild(String JavaDoc path) throws IOException JavaDoc
226    {
227       checkClosed();
228
229       // Parse the path
230
String JavaDoc[] tokens = PathTokenizer.getTokens(path);
231       if (tokens == null || tokens.length == 0)
232          return this;
233
234       // Go through each context starting from ours
235
// check the parents are not leaves.
236
VirtualFileHandler current = this;
237       for (int i = 0; i < tokens.length; ++i)
238       {
239          if (current.isLeaf())
240             throw new IOException JavaDoc("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 JavaDoc remainingPath = PathTokenizer.getRemainingPath(tokens, i);
249             return current.findChild(remainingPath);
250          }
251       }
252       
253       // The last one is the result
254
return current;
255    }
256
257    /**
258     * Simple implementation of findChild
259     *
260     * @param path the path
261     * @return the handler
262     * @throws IOException for any error accessing the virtual file system
263     * @throws IllegalArgumentException for a null name
264     */

265    public VirtualFileHandler simpleFindChild(String JavaDoc path) throws IOException JavaDoc
266    {
267       if (path == null)
268          throw new IllegalArgumentException JavaDoc("Null path");
269
270       if (path.length() == 0)
271          return this;
272
273       List JavaDoc<VirtualFileHandler> children = getChildren(false);
274       for (VirtualFileHandler child : children)
275       {
276          if (child.getName().equals(path))
277             return child;
278       }
279       throw new IOException JavaDoc("Child not found " + path + " for " + this);
280    }
281
282    @Override JavaDoc
283    public String JavaDoc toString()
284    {
285       StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
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 JavaDoc
297    public int hashCode()
298    {
299       return getPathName().hashCode();
300    }
301
302    @Override JavaDoc
303    public boolean equals(Object JavaDoc 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 JavaDoc
318    protected void finalize() throws Throwable JavaDoc
319    {
320       close();
321    }
322    
323    /**
324     * Safely get a url version of the string
325     *
326     * @return the string or unknown if there is an error
327     */

328    private String JavaDoc safeToURLString()
329    {
330       try
331       {
332          return toURI().toString();
333       }
334       catch (URISyntaxException JavaDoc ignored)
335       {
336          return "<unknown>";
337       }
338    }
339
340    private void writeObject(ObjectOutputStream JavaDoc out)
341       throws IOException JavaDoc
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 JavaDoc in)
350       throws IOException JavaDoc, ClassNotFoundException JavaDoc
351    {
352       // Read in the serialPersistentFields
353
GetField fields = in.readFields();
354       URI JavaDoc rootURI = (URI JavaDoc) fields.get("rootURI", null);
355       this.parent = (VirtualFileHandler) fields.get("parent", null);
356       this.name = (String JavaDoc) fields.get("name", null);
357       VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURI);
358       this.context = factory.getVFS(rootURI);
359       this.references = new AtomicInteger JavaDoc(0);
360       // Restore the log
361
log = Logger.getLogger(getClass());
362    }
363 }
364
Popular Tags