KickJava   Java API By Example, From Geeks To Geeks.

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


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.loader.EnvironmentLocal;
33 import com.caucho.server.hmux.HmuxPath;
34 import com.caucho.util.CharBuffer;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.Reader JavaDoc;
40 import java.io.Writer JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * Facade to create useful Path and Stream objects.
45  *
46  * <code><pre>
47  * Path path = Vfs.lookup("foo.html");
48  * </pre><code>
49  *
50  * <p>The default scheme is the file scheme. Other schemes are
51  * available using the full url.
52  *
53  * <code><pre>
54  * Path mail = Vfs.lookup("mailto:drofnats@foo.com.test?subject='hi'");
55  * Stream body = mail.openWrite();
56  * body.writeln("How's it going?");
57  * body.close();
58  * </pre><code>
59  */

60 public final class Vfs {
61   private static final EnvironmentLocal<Path> ENV_PWD
62     = new EnvironmentLocal<Path>("caucho.vfs.pwd");
63   
64   private static final SchemeMap DEFAULT_SCHEME_MAP;
65   
66   private static final EnvironmentLocal<SchemeMap> _localSchemeMap
67     = new EnvironmentLocal<SchemeMap>();
68   
69   static FilesystemPath PWD;
70
71   private Vfs() {}
72   
73   /**
74    * Returns a new path relative to the current directory.
75    *
76    * @param url a relative or absolute url
77    * @return the new path.
78    */

79   public static Path lookup(String JavaDoc url)
80   {
81     Path pwd = getPwd();
82
83     if (! url.startsWith("/"))
84       return pwd.lookup(url, null);
85     else
86       return PWD.lookup(url, null);
87   }
88
89   public static FilesystemPath getGlobalPwd()
90   {
91     return PWD;
92   }
93   
94   /**
95    * Returns a path for the current directory.
96    */

97   public static Path getPwd()
98   {
99     Path pwd = ENV_PWD.get();
100     
101     if (pwd == null) {
102       if (PWD == null) {
103     /* JNI set later
104     PWD = JniFilePath.create();
105
106     if (PWD == null)
107       PWD = new FilePath(null);
108     */

109     PWD = new FilePath(null);
110       }
111       pwd = PWD;
112       ENV_PWD.setGlobal(pwd);
113     }
114
115     return pwd;
116   }
117
118   public static SchemeMap getLocalScheme()
119   {
120     synchronized (_localSchemeMap) {
121       SchemeMap map = _localSchemeMap.getLevel();
122
123       if (map == null) {
124     map = _localSchemeMap.get().copy();
125     
126     if (map == null)
127       map = DEFAULT_SCHEME_MAP.copy();
128
129     _localSchemeMap.set(map);
130       }
131
132       return map;
133     }
134   }
135
136   /**
137    * Returns a path for the current directory.
138    */

139   public static Path getPwd(ClassLoader JavaDoc loader)
140   {
141     return ENV_PWD.get(loader);
142   }
143
144   /**
145    * Sets a path for the current directory in the current environment.
146    */

147   public static void setPwd(Path pwd)
148   {
149     setPwd(pwd, Thread.currentThread().getContextClassLoader());
150   }
151
152   /**
153    * Sets a path for the current directory in the current environment.
154    */

155   public static void setPwd(Path pwd, ClassLoader JavaDoc loader)
156   {
157     ENV_PWD.set(pwd, loader);
158   }
159
160   /**
161    * Returns a path for the current directory.
162    */

163   public static Path lookup()
164   {
165     return getPwd();
166   }
167
168   /**
169    * Returns a new path, including attributes.
170    * <p>For example, an application may want to set locale headers
171    * for an HTTP request.
172    *
173    * @param url the relative url
174    * @param attr attributes used in searching for the url
175    */

176   public static Path lookup(String JavaDoc url, Map JavaDoc<String JavaDoc,Object JavaDoc> attr)
177   {
178     return getPwd().lookup(url, attr);
179   }
180
181   /**
182    * Returns a path using the native filesystem conventions.
183    * <p>For example, on windows
184    *
185    * <code><pre>
186    * Path path = Vfs.lookup("d:\\temp\\test.html");
187    * </pre></code>
188    *
189    * @param url a relative path using the native filesystem conventions.
190    */

191   public static Path lookupNative(String JavaDoc url)
192   {
193     return getPwd().lookupNative(url, null);
194   }
195
196   /**
197    * Returns a native filesystem path with attributes.
198    *
199    * @param url a relative path using the native filesystem conventions.
200    * @param attr attributes used in searching for the url
201    */

202   public static Path lookupNative(String JavaDoc url, Map JavaDoc<String JavaDoc,Object JavaDoc> attr)
203   {
204     return getPwd().lookupNative(url, attr);
205   }
206
207   public static ReadWritePair openReadWrite(InputStream JavaDoc is, OutputStream JavaDoc os)
208   {
209     VfsStream s = new VfsStream(is, os);
210     WriteStream writeStream = new WriteStream(s);
211     ReadStream readStream = new ReadStream(s, writeStream);
212     return new ReadWritePair(readStream, writeStream);
213   }
214
215   /**
216    * Creates new ReadStream from an InputStream
217    */

218   public static ReadStream openRead(InputStream JavaDoc is)
219   {
220     if (is instanceof ReadStream)
221       return (ReadStream) is;
222     
223     VfsStream s = new VfsStream(is, null);
224     return new ReadStream(s);
225   }
226
227   public static ReadStream openRead(InputStream JavaDoc is, WriteStream ws)
228   {
229     VfsStream s = new VfsStream(is, null);
230     return new ReadStream(s, ws);
231   }
232
233   /**
234    * Creates a ReadStream from a Reader
235    */

236   public static ReadStream openRead(Reader JavaDoc reader)
237   {
238     if (reader instanceof ReadStream.StreamReader)
239       return ((ReadStream.StreamReader) reader).getStream();
240     
241     ReaderWriterStream s = new ReaderWriterStream(reader, null);
242     ReadStream is = new ReadStream(s);
243     try {
244       is.setEncoding("utf-8");
245     } catch (Exception JavaDoc e) {
246     }
247
248     return is;
249   }
250
251   /**
252    * Create a ReadStream from a string. utf-8 is used as the encoding
253    */

254   public static ReadStream openRead(String JavaDoc path)
255     throws IOException JavaDoc
256   {
257     return Vfs.lookup(path).openRead();
258   }
259
260   public static ReadStream openString(String JavaDoc string)
261   {
262     return com.caucho.vfs.StringReader.open(string);
263   }
264
265   public static WriteStream openWrite(OutputStream JavaDoc os)
266   {
267     if (os instanceof WriteStream)
268       return ((WriteStream) os);
269     
270     VfsStream s = new VfsStream(null, os);
271     return new WriteStream(s);
272   }
273
274   public static WriteStream openWrite(Writer JavaDoc writer)
275   {
276     ReaderWriterStream s = new ReaderWriterStream(null, writer);
277     WriteStream os = new WriteStream(s);
278     
279     try {
280       os.setEncoding("utf-8");
281     } catch (Exception JavaDoc e) {
282     }
283
284     return os;
285   }
286
287   /**
288    * Creates a write stream to a CharBuffer. This is the standard way
289    * to write to a string.
290    */

291   public static WriteStream openWrite(CharBuffer cb)
292   {
293     com.caucho.vfs.StringWriter s = new com.caucho.vfs.StringWriter(cb);
294     WriteStream os = new WriteStream(s);
295     
296     try {
297       os.setEncoding("utf-8");
298     } catch (Exception JavaDoc e) {
299     }
300
301     return os;
302   }
303
304   public static WriteStream openWrite(String JavaDoc path)
305     throws IOException JavaDoc
306   {
307     return lookup(path).openWrite();
308   }
309
310   public static WriteStream openAppend(String JavaDoc path)
311     throws IOException JavaDoc
312   {
313     return lookup(path).openAppend();
314   }
315
316   /**
317    * Initialize the JNI.
318    */

319   public static void initJNI()
320   {
321     // order matters because of static init and license checking
322
FilesystemPath jniFilePath = JniFilePath.create();
323
324     if (jniFilePath != null) {
325       DEFAULT_SCHEME_MAP.put("file", jniFilePath);
326       
327       SchemeMap localMap = _localSchemeMap.get();
328       if (localMap != null)
329     localMap.put("file", jniFilePath);
330       
331       localMap = _localSchemeMap.get(ClassLoader.getSystemClassLoader());
332       if (localMap != null)
333     localMap.put("file", jniFilePath);
334       
335       Vfs.PWD = jniFilePath;
336       Vfs.setPwd(jniFilePath);
337     }
338   }
339
340   static {
341     DEFAULT_SCHEME_MAP = new SchemeMap();
342
343     Path.setDefaultSchemeMap(DEFAULT_SCHEME_MAP);
344
345     FilePath pwd = new FilePath(null);
346     PWD = pwd;
347     setPwd(pwd);
348     ENV_PWD.setGlobal(pwd);
349     ENV_PWD.set(pwd);
350     
351     _localSchemeMap.setGlobal(DEFAULT_SCHEME_MAP);
352     
353     DEFAULT_SCHEME_MAP.put("file", pwd);
354     
355     DEFAULT_SCHEME_MAP.put("memory", new MemoryScheme());
356     
357     DEFAULT_SCHEME_MAP.put("jar", new JarScheme(null));
358     DEFAULT_SCHEME_MAP.put("mailto",
359                  new MailtoPath(null, null, null, null));
360     DEFAULT_SCHEME_MAP.put("http", new HttpPath("127.0.0.1", 0));
361     DEFAULT_SCHEME_MAP.put("https", new HttpsPath("127.0.0.1", 0));
362     DEFAULT_SCHEME_MAP.put("hmux", new HmuxPath("127.0.0.1", 0));
363     DEFAULT_SCHEME_MAP.put("tcp", new TcpPath(null, null, null, "127.0.0.1", 0));
364     DEFAULT_SCHEME_MAP.put("tcps", new TcpsPath(null, null, null, "127.0.0.1", 0));
365     // DEFAULT_SCHEME_MAP.put("log", new LogPath(null, "/", null, "/"));
366
DEFAULT_SCHEME_MAP.put("merge", new MergePath());
367
368     StreamImpl stdout = StdoutStream.create();
369     StreamImpl stderr = StderrStream.create();
370     DEFAULT_SCHEME_MAP.put("stdout", stdout.getPath());
371     DEFAULT_SCHEME_MAP.put("stderr", stderr.getPath());
372     VfsStream nullStream = new VfsStream(null, null);
373     DEFAULT_SCHEME_MAP.put("null", new ConstPath(null, nullStream));
374     DEFAULT_SCHEME_MAP.put("jndi", new JndiPath());
375     
376     DEFAULT_SCHEME_MAP.put("config", new ConfigPath());
377     DEFAULT_SCHEME_MAP.put("spy", new SpyScheme());
378
379   }
380 }
381
Popular Tags