KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > PathWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.util.L10N;
33
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Wraps a path object.
42  */

43 public abstract class PathWrapper extends Path {
44   protected final static L10N L = new L10N(PathWrapper.class);
45
46   private final Path _path;
47
48   /**
49    * Creates a new Path object.
50    *
51    * @param path the new Path root.
52    */

53   protected PathWrapper(Path path)
54   {
55     super(path);
56
57     _path = path;
58   }
59
60   /**
61    * Returns the wrapped path.
62    */

63   public Path getWrappedPath()
64   {
65     return _path;
66   }
67
68   /**
69    * Returns a new path relative to the current one.
70    *
71    * <p>Path only handles scheme:xxx. Subclasses of Path will specialize
72    * the xxx.
73    *
74    * @param userPath relative or absolute path, essentially any url.
75    * @param newAttributes attributes for the new path.
76    *
77    * @return the new path or null if the scheme doesn't exist
78    */

79    public Path lookup(String JavaDoc userPath, Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes)
80    {
81      return getWrappedPath().lookup(userPath, newAttributes);
82    }
83
84   /**
85    * Returns a new path relative to the current one.
86    *
87    * <p>Path only handles scheme:xxx. Subclasses of Path will specialize
88    * the xxx.
89    *
90    * @param userPath relative or absolute path, essentially any url.
91    * @param newAttributes attributes for the new path.
92    *
93    * @return the new path or null if the scheme doesn't exist
94    */

95   public Path lookupImpl(String JavaDoc userPath, Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes)
96   {
97     return getWrappedPath().lookupImpl(userPath, newAttributes);
98   }
99
100   /**
101    * Looks up a native path, adding attributes.
102    */

103   public Path lookupNative(String JavaDoc name, Map JavaDoc<String JavaDoc,Object JavaDoc> attributes)
104   {
105     return getWrappedPath().lookupNative(name, attributes);
106   }
107
108   /**
109    * Looks up all the resources matching a name. (Generally only useful
110    * with MergePath.
111    */

112   public ArrayList JavaDoc<Path> getResources(String JavaDoc name)
113   {
114     return getWrappedPath().getResources(name);
115   }
116
117   /**
118    * Looks up all the existing resources. (Generally only useful
119    * with MergePath.
120    */

121   public ArrayList JavaDoc<Path> getResources()
122   {
123     return getWrappedPath().getResources();
124   }
125
126   /**
127    * Returns the parent path.
128    */

129   public Path getParent()
130   {
131     return getWrappedPath().getParent();
132   }
133
134   /**
135    * Path-specific lookup. Path implementations will override this.
136    *
137    * @param userPath the user's lookup() path.
138    * @param newAttributes the attributes for the new path.
139    * @param newPath the lookup() path
140    * @param offset offset into newPath to start lookup.
141    *
142    * @return the found path
143    */

144   protected Path schemeWalk(String JavaDoc userPath,
145                 Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes,
146                 String JavaDoc newPath, int offset)
147   {
148     return getWrappedPath().schemeWalk(userPath, newAttributes,
149                        newPath, offset);
150   }
151
152   /**
153    * Returns the full url for the given path.
154    */

155   public String JavaDoc getURL()
156   {
157     return getWrappedPath().getURL();
158   }
159
160   /**
161    * Returns the url scheme
162    */

163   public String JavaDoc getScheme()
164   {
165     return getWrappedPath().getScheme();
166   }
167
168   /**
169    * Returns the hostname
170    */

171   public String JavaDoc getHost()
172   {
173     return getWrappedPath().getHost();
174   }
175
176   /**
177    * Returns the port.
178    */

179   public int getPort()
180   {
181     return getWrappedPath().getPort();
182   }
183
184   /**
185    * Returns the path. e.g. for HTTP, returns the part after the
186    * host and port.
187    */

188   public String JavaDoc getPath()
189   {
190     return getWrappedPath().getPath();
191   }
192
193   /**
194    * Returns the last segment of the path.
195    *
196    * <p>e.g. for http://www.caucho.com/products/index.html, getTail()
197    * returns 'index.html'
198    */

199   public String JavaDoc getTail()
200   {
201     return getWrappedPath().getTail();
202   }
203
204   /**
205    * Returns the query string of the path.
206    */

207   public String JavaDoc getQuery()
208   {
209     return getWrappedPath().getQuery();
210   }
211
212   /**
213    * Returns the native representation of the path.
214    *
215    * On Windows, getNativePath() returns 'd:\\foo\bar.html',
216    * getPath() returns '/d:/foo/bar.html'
217    */

218   public String JavaDoc getNativePath()
219   {
220     return getWrappedPath().getNativePath();
221   }
222
223   /**
224    * Returns the last string used as a lookup, if available. This allows
225    * parsers to give intelligent error messages, with the user's path
226    * instead of the whole path.
227    *
228    * The following will print '../test.html':
229    * <code><pre>
230    * Path path = Pwd.lookup("/some/dir").lookup("../test.html");
231    * System.out.println(path.getUserPath());
232    * </pre></code>
233    *
234    */

235   public String JavaDoc getUserPath()
236   {
237     return getWrappedPath().getUserPath();
238   }
239
240   /**
241    * Sets the user path. Useful for temporary files caching another
242    * URL.
243    */

244   public void setUserPath(String JavaDoc userPath)
245   {
246     getWrappedPath().setUserPath(userPath);
247   }
248
249   /**
250    * Returns the full path, including the restricted root.
251    *
252    * <p>For the following, path.getPath() returns '/file.html', while
253    * path.getFullPath() returns '/chroot/file.html'.
254    * <code><pre>
255    * Path chroot = Pwd.lookup("/chroot").createRoot();
256    * Path path = chroot.lookup("/file.html");
257    * </pre></code>
258    */

259   public String JavaDoc getFullPath()
260   {
261     return getWrappedPath().getFullPath();
262   }
263
264   /**
265    * For union paths like MergePath, return the relative path into
266    * that path.
267    */

268   public String JavaDoc getRelativePath()
269   {
270     return getWrappedPath().getRelativePath();
271   }
272
273   /**
274    * Tests if the file exists.
275    */

276   public boolean exists()
277   {
278     return getWrappedPath().exists();
279   }
280
281   /**
282    * Returns the mime-type of the file.
283    * <p>Mime-type ignorant filesystems return 'application/octet-stream'
284    */

285   public String JavaDoc getContentType()
286   {
287     return getWrappedPath().getContentType();
288   }
289
290   /**
291    * Tests if the path refers to a directory.
292    */

293   public boolean isDirectory()
294   {
295     return getWrappedPath().isDirectory();
296   }
297
298   /**
299    * Tests if the path refers to a file.
300    */

301   public boolean isFile()
302   {
303     return getWrappedPath().isFile();
304   }
305
306   /**
307    * Tests if the path refers to an object.
308    */

309   public boolean isObject()
310   {
311     return getWrappedPath().isObject();
312   }
313
314   /**
315    * Returns the length of the file in bytes.
316    * @return 0 for non-files
317    */

318   public long getLength()
319   {
320     return getWrappedPath().getLength();
321   }
322
323   /**
324    * Returns the last modified time of the file. According to the jdk,
325    * this may not correspond to the system time.
326    * @return 0 for non-files.
327    */

328   public long getLastModified()
329   {
330     return getWrappedPath().getLastModified();
331   }
332
333   public void setLastModified(long time)
334   {
335     getWrappedPath().setLastModified(time);
336   }
337
338   /**
339    * Returns the last access time of the file.
340    *
341    * @return 0 for non-files.
342    */

343   public long getLastAccessTime()
344   {
345     return getWrappedPath().getLastAccessTime();
346   }
347
348   /**
349    * Returns the create time of the file.
350    *
351    * @return 0 for non-files.
352    */

353   public long getCreateTime()
354   {
355     return getWrappedPath().getCreateTime();
356   }
357
358   /**
359    * Tests if the file can be read.
360    */

361   public boolean canRead()
362   {
363     return getWrappedPath().canRead();
364   }
365
366   /**
367    * Tests if the file can be written.
368    */

369   public boolean canWrite()
370   {
371     return getWrappedPath().canWrite();
372   }
373
374   /**
375    * Changes the permissions
376    */

377   public boolean chmod(int value)
378   {
379     return getWrappedPath().chmod(value);
380   }
381
382   /**
383    * @return The contents of this directory or null if the path does not
384    * refer to a directory.
385    */

386   public String JavaDoc []list() throws IOException JavaDoc
387   {
388     return getWrappedPath().list();
389   }
390
391   /**
392    * Returns a jdk1.2 Iterator for the contents of this directory.
393    */

394   public Iterator JavaDoc<String JavaDoc> iterator() throws IOException JavaDoc
395   {
396     return getWrappedPath().iterator();
397   }
398
399   /**
400    * Creates the directory named by this path.
401    * @return true if successful.
402    */

403   public boolean mkdir() throws IOException JavaDoc
404   {
405     return getWrappedPath().mkdir();
406   }
407
408   /**
409    * Creates the directory named by this path and any parent directories.
410    * @return true if successful.
411    */

412   public boolean mkdirs() throws IOException JavaDoc
413   {
414     return getWrappedPath().mkdirs();
415   }
416
417   /**
418    * Removes the file or directory named by this path.
419    * @return true if successful.
420    */

421   public boolean remove() throws IOException JavaDoc
422   {
423     return getWrappedPath().remove();
424   }
425
426   /**
427    * Removes the all files and directories below this path.
428    *
429    * @return true if successful.
430    */

431   public boolean removeAll() throws IOException JavaDoc
432   {
433     return getWrappedPath().removeAll();
434   }
435
436   /**
437    * Renames the file or directory to the name given by the path.
438    * @return true if successful
439    */

440   public boolean renameTo(Path path) throws IOException JavaDoc
441   {
442     return getWrappedPath().renameTo(path);
443   }
444
445   /**
446    * Creates a restricted root, like the Unix chroot call.
447    * Restricted roots cannot access schemes, so file:/etc/passwd cannot
448    * be used.
449    *
450    * <p>createRoot is useful for restricting JavaScript scripts without
451    * resorting to the dreadfully slow security manager.
452    */

453   public Path createRoot()
454   {
455     return getWrappedPath().createRoot();
456   }
457
458   public Path createRoot(SchemeMap schemeMap)
459   {
460     return getWrappedPath().createRoot(schemeMap);
461   }
462
463   /**
464    * Binds the context to the current path. Later lookups will return
465    * the new context instead of the current path. Essentially, this is a
466    * software symbolic link.
467    */

468   public void bind(Path context)
469   {
470     getWrappedPath().bind(context);
471   }
472
473   /**
474    * unbinds a link.
475    */

476   public void unbind()
477   {
478     getWrappedPath().unbind();
479   }
480
481   /**
482    * Gets the object at the path. Normal filesystems will generally
483    * typically return null.
484    *
485    * <p>A bean filesystem or a mime-type aware filesystem could deserialize
486    * the contents of the file.
487    */

488   public Object JavaDoc getValue() throws Exception JavaDoc
489   {
490     return getWrappedPath().getValue();
491   }
492
493   /**
494    * Sets the object at the path.
495    *
496    * <p>Normal filesystems will generally do nothing. However, a bean
497    * filesystem or a mime-type aware filesystem could serialize the object
498    * and store it.
499    */

500   public void setValue(Object JavaDoc obj) throws Exception JavaDoc
501   {
502     getWrappedPath().setValue(obj);
503   }
504
505   /**
506    * Gets an attribute of the object.
507    */

508   public Object JavaDoc getAttribute(String JavaDoc name) throws IOException JavaDoc
509   {
510     return getWrappedPath().getAttribute(name);
511   }
512
513   /**
514    * Returns a iterator of all attribute names set for this object.
515    * @return null if path has no attributes.
516    */

517   public Iterator JavaDoc getAttributeNames() throws IOException JavaDoc
518   {
519     return getWrappedPath().getAttributeNames();
520   }
521
522   /**
523    * Opens a resin ReadWritePair for reading and writing.
524    *
525    * <p>A chat channel, for example, would open its socket using this
526    * interface.
527    */

528   public ReadWritePair openReadWrite() throws IOException JavaDoc
529   {
530     return getWrappedPath().openReadWrite();
531   }
532
533   /**
534    * Opens a resin ReadWritePair for reading and writing.
535    *
536    * <p>A chat channel, for example, would open its socket using this
537    * interface.
538    *
539    * @param is pre-allocated ReadStream to be initialized
540    * @param os pre-allocated WriteStream to be initialized
541    */

542   public void openReadWrite(ReadStream is, WriteStream os) throws IOException JavaDoc
543   {
544     getWrappedPath().openReadWrite(is, os);
545   }
546
547   /**
548    * Opens a resin stream for appending.
549    */

550   public WriteStream openAppend() throws IOException JavaDoc
551   {
552     return getWrappedPath().openAppend();
553   }
554
555   /**
556    * Opens a random-access stream.
557    */

558   public RandomAccessStream openRandomAccess() throws IOException JavaDoc
559   {
560     return getWrappedPath().openRandomAccess();
561   }
562
563   /**
564    * Creates the file named by this Path and returns true if the
565    * file is new.
566    */

567   public boolean createNewFile() throws IOException JavaDoc
568   {
569     return getWrappedPath().createNewFile();
570   }
571
572   /**
573    * Creates a unique temporary file as a child of this directory.
574    *
575    * @param prefix filename prefix
576    * @param suffix filename suffix, defaults to .tmp
577    * @return Path to the new file.
578    */

579   public Path createTempFile(String JavaDoc prefix, String JavaDoc suffix) throws IOException JavaDoc
580   {
581     return getWrappedPath().createTempFile(prefix, suffix);
582   }
583
584   /**
585    * Utility to write the contents of this path to the destination stream.
586    *
587    * @param os destination stream.
588    */

589   public void writeToStream(OutputStream JavaDoc os)
590     throws IOException JavaDoc
591   {
592     getWrappedPath().writeToStream(os);
593   }
594
595   /**
596    * Utility to write the contents of this path to the destination stream.
597    *
598    * @param os destination stream.
599    */

600   public void writeToStream(OutputStreamWithBuffer os)
601     throws IOException JavaDoc
602   {
603     getWrappedPath().writeToStream(os);
604   }
605
606   /**
607    * Returns the crc64 code.
608    */

609   public long getCrc64()
610   {
611     return getWrappedPath().getCrc64();
612   }
613
614   /**
615    * Returns the object at this path. Normally, only paths like JNDI
616    * will support this.
617    */

618   public Object JavaDoc getObject()
619     throws IOException JavaDoc
620   {
621     return getWrappedPath().getObject();
622   }
623
624   /**
625    * Sets the object at this path. Normally, only paths like JNDI
626    * will support this.
627    */

628   public void setObject(Object JavaDoc obj)
629     throws IOException JavaDoc
630   {
631     getWrappedPath().setObject(obj);
632   }
633
634   public long getInode()
635   {
636     return getWrappedPath().getInode();
637   }
638
639   public boolean isExecutable()
640   {
641     return getWrappedPath().isExecutable();
642   }
643
644   public boolean setExecutable(boolean isExecutable)
645   {
646     return getWrappedPath().setExecutable(isExecutable);
647   }
648
649   public int getGroup()
650   {
651     return getWrappedPath().getGroup();
652   }
653
654   public boolean changeGroup(int gid)
655     throws IOException JavaDoc
656   {
657     return getWrappedPath().changeGroup(gid);
658   }
659
660   public boolean changeGroup(String JavaDoc groupName)
661     throws IOException JavaDoc
662   {
663     return getWrappedPath().changeGroup(groupName);
664   }
665
666   public int getOwner()
667   {
668     return getWrappedPath().getOwner();
669   }
670
671   public boolean changeOwner(int uid)
672     throws IOException JavaDoc
673   {
674     return getWrappedPath().changeOwner(uid);
675   }
676
677   public boolean changeOwner(String JavaDoc ownerName)
678     throws IOException JavaDoc
679   {
680     return getWrappedPath().changeOwner(ownerName);
681   }
682
683   public long getDiskSpaceFree()
684   {
685     return getWrappedPath().getDiskSpaceFree();
686   }
687
688   public long getDiskSpaceTotal()
689   {
690     return getWrappedPath().getDiskSpaceTotal();
691   }
692
693   public int hashCode()
694   {
695     return getWrappedPath().hashCode();
696   }
697
698   public boolean equals(Object JavaDoc o)
699   {
700     return o.equals(getWrappedPath());
701   }
702
703   public String JavaDoc toString()
704   {
705     return getWrappedPath().toString();
706   }
707
708   public StreamImpl openReadImpl() throws IOException JavaDoc
709   {
710     return getWrappedPath().openReadImpl();
711   }
712
713   public StreamImpl openWriteImpl() throws IOException JavaDoc
714   {
715     return getWrappedPath().openWriteImpl();
716   }
717
718   public StreamImpl openReadWriteImpl() throws IOException JavaDoc
719   {
720     return getWrappedPath().openReadWriteImpl();
721   }
722
723   public StreamImpl openAppendImpl() throws IOException JavaDoc
724   {
725     return getWrappedPath().openAppendImpl();
726   }
727 }
728
Popular Tags