KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Map JavaDoc;
33
34 class BindPath extends FilesystemPath {
35   private Node _node;
36   private Path _backing;
37   
38   BindPath(Path backing)
39   {
40     this(null, "/", null, "/", null, backing);
41
42     _root = this;
43     if (backing instanceof FilesystemPath)
44       _separatorChar = ((FilesystemPath) backing)._separatorChar;
45   }
46
47   /**
48    * @param path canonical path
49    */

50   private BindPath(BindPath root,
51            String JavaDoc userPath, Map JavaDoc<String JavaDoc,Object JavaDoc> attributes,
52            String JavaDoc path, Node node, Path backing)
53   {
54     super(root, userPath, path);
55
56     if (backing == null)
57       throw new IllegalArgumentException JavaDoc("backing must not be null");
58
59     if (node == null)
60       node = new Node("", backing);
61
62     if (backing == null)
63       backing = node._backing;
64
65     _node = node;
66     _backing = backing;
67     
68     if (backing instanceof FilesystemPath)
69       _separatorChar = ((FilesystemPath) backing)._separatorChar;
70   }
71
72   public Path fsWalk(String JavaDoc userPath,
73             Map JavaDoc<String JavaDoc,Object JavaDoc> attributes,
74             String JavaDoc path)
75   {
76     Node ptr = _node;
77
78     int offset = 0;
79     while (offset + 1 < path.length()) {
80       if (ptr.firstChild == null)
81     return ptr._backing.lookup(path.substring(offset), attributes);
82
83       int p = path.indexOf(_separatorChar, offset + 1);
84       String JavaDoc segment;
85       if (p == -1)
86     segment = path.substring(offset + 1);
87       else
88     segment = path.substring(offset + 1, p);
89
90       Node next = ptr.findChild(segment);
91
92       if (next == null)
93     return ptr._backing.lookup(path.substring(offset), attributes);
94
95       offset = p;
96       ptr = next;
97     }
98     
99     return new BindPath(this, userPath, attributes, path, _node, null);
100   }
101
102   public String JavaDoc getScheme()
103   {
104     return _root.getScheme();
105   }
106
107   public boolean exists()
108   {
109     return _backing.exists();
110   }
111
112   public boolean isDirectory()
113   {
114     return _backing.isDirectory();
115   }
116
117   public boolean isFile()
118   {
119     return _backing.isFile();
120   }
121
122   public long getLength()
123   {
124     return _backing.getLength();
125   }
126
127   public long getLastModified()
128   {
129     return _backing.getLastModified();
130   }
131
132   public boolean canRead()
133   {
134     return _backing.canRead();
135   }
136
137   public boolean canWrite()
138   {
139     return _backing.canWrite();
140   }
141   
142   public String JavaDoc []list() throws IOException JavaDoc
143   {
144     String JavaDoc []list = _backing.list();
145     
146     if (_node.firstChild == null)
147       return list;
148
149     String JavaDoc []newList = new String JavaDoc[list.length + _node.size()];
150
151     int i = 0;
152     for (Node ptr = _node.firstChild; ptr != null; ptr = ptr.next)
153       newList[i++] = ptr.name;
154
155     for (int j = 0; j < list.length; j++)
156       newList[i++] = list[j++];
157
158     return newList;
159   }
160   
161   public boolean mkdir()
162     throws IOException JavaDoc
163   {
164     return _backing.mkdir();
165   }
166   
167   public boolean mkdirs()
168     throws IOException JavaDoc
169   {
170     return _backing.mkdirs();
171   }
172   
173   public boolean remove()
174     throws IOException JavaDoc
175   {
176     return _backing.remove();
177   }
178   
179   public boolean renameTo(Path path)
180     throws IOException JavaDoc
181   {
182     return _backing.renameTo(path);
183   }
184
185   public StreamImpl openReadImpl() throws IOException JavaDoc
186   {
187     return _backing.openReadImpl();
188   }
189
190   public StreamImpl openWriteImpl() throws IOException JavaDoc
191   {
192     return _backing.openWriteImpl();
193   }
194
195   public StreamImpl openReadWriteImpl() throws IOException JavaDoc
196   {
197     return _backing.openReadWriteImpl();
198   }
199
200   public StreamImpl openAppendImpl() throws IOException JavaDoc
201   {
202     return _backing.openAppendImpl();
203   }
204
205   void bind(String JavaDoc path, Path context)
206   {
207     Node ptr = _node;
208
209     int offset = 0;
210     while (offset + 1 < path.length()) {
211       int p = path.indexOf(_separatorChar, offset + 1);
212       String JavaDoc segment;
213       if (p == -1)
214     segment = path.substring(offset + 1);
215       else
216     segment = path.substring(offset + 1, p);
217
218       Node next = ptr.findChild(segment);
219
220       if (next == null)
221     next = ptr.addChild(segment, ptr._backing);
222     }
223
224     ptr._backing = context;
225   }
226
227   public int hashCode()
228   {
229     return _backing.hashCode();
230   }
231
232   public boolean equals(Object JavaDoc b)
233   {
234     return _backing.equals(b);
235   }
236
237   public String JavaDoc toString()
238   {
239     return _backing.toString();
240   }
241
242   static class Node {
243     Node parent;
244     Node firstChild;
245     Node next;
246
247     String JavaDoc name;
248     Path _backing;
249
250     Node(String JavaDoc name, Path backing)
251     {
252       this.name = name;
253       this._backing = backing;
254     }
255
256     int size()
257     {
258       int size = 0;
259
260       for (Node ptr = firstChild; ptr != null; ptr = ptr.next)
261     size++;
262
263       return size;
264     }
265
266     Node findChild(String JavaDoc name)
267     {
268       for (Node ptr = firstChild; ptr != null; ptr = ptr.next) {
269     if (ptr.name.equals(name))
270       return ptr;
271       }
272
273       return null;
274     }
275
276     Node addChild(String JavaDoc name, Path backing)
277     {
278       for (Node ptr = firstChild; ptr != null; ptr = ptr.next) {
279     if (ptr.name.equals(name)) {
280       ptr._backing = backing;
281       return ptr;
282     }
283       }
284
285       Node node = new Node(name, backing);
286       node.next = firstChild;
287       node.parent = this;
288       firstChild = node;
289
290       return node;
291     }
292   }
293 }
294
295
Popular Tags