1 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 ; 37 import java.io.InputStream ; 38 import java.io.OutputStream ; 39 import java.io.Reader ; 40 import java.io.Writer ; 41 import java.util.Map ; 42 43 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 79 public static Path lookup(String 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 97 public static Path getPwd() 98 { 99 Path pwd = ENV_PWD.get(); 100 101 if (pwd == null) { 102 if (PWD == null) { 103 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 139 public static Path getPwd(ClassLoader loader) 140 { 141 return ENV_PWD.get(loader); 142 } 143 144 147 public static void setPwd(Path pwd) 148 { 149 setPwd(pwd, Thread.currentThread().getContextClassLoader()); 150 } 151 152 155 public static void setPwd(Path pwd, ClassLoader loader) 156 { 157 ENV_PWD.set(pwd, loader); 158 } 159 160 163 public static Path lookup() 164 { 165 return getPwd(); 166 } 167 168 176 public static Path lookup(String url, Map <String ,Object > attr) 177 { 178 return getPwd().lookup(url, attr); 179 } 180 181 191 public static Path lookupNative(String url) 192 { 193 return getPwd().lookupNative(url, null); 194 } 195 196 202 public static Path lookupNative(String url, Map <String ,Object > attr) 203 { 204 return getPwd().lookupNative(url, attr); 205 } 206 207 public static ReadWritePair openReadWrite(InputStream is, OutputStream 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 218 public static ReadStream openRead(InputStream 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 is, WriteStream ws) 228 { 229 VfsStream s = new VfsStream(is, null); 230 return new ReadStream(s, ws); 231 } 232 233 236 public static ReadStream openRead(Reader 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 e) { 246 } 247 248 return is; 249 } 250 251 254 public static ReadStream openRead(String path) 255 throws IOException 256 { 257 return Vfs.lookup(path).openRead(); 258 } 259 260 public static ReadStream openString(String string) 261 { 262 return com.caucho.vfs.StringReader.open(string); 263 } 264 265 public static WriteStream openWrite(OutputStream 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 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 e) { 282 } 283 284 return os; 285 } 286 287 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 e) { 299 } 300 301 return os; 302 } 303 304 public static WriteStream openWrite(String path) 305 throws IOException 306 { 307 return lookup(path).openWrite(); 308 } 309 310 public static WriteStream openAppend(String path) 311 throws IOException 312 { 313 return lookup(path).openAppend(); 314 } 315 316 319 public static void initJNI() 320 { 321 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("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 |