1 29 30 package com.caucho.vfs; 31 32 import com.caucho.loader.DynamicClassLoader; 33 import com.caucho.server.util.CauchoSystem; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.Map ; 38 39 66 public class MergePath extends FilesystemPath { 67 private ArrayList <Path> _pathList; 68 69 private Path _bestPath; 70 71 74 public MergePath() 75 { 76 super(null, "/", "/"); 77 78 _root = this; 79 _pathList = new ArrayList <Path>(); 80 } 81 82 85 private MergePath(MergePath root, 86 String userPath, Map <String ,Object > attributes, 87 String path) 88 { 89 super(root, userPath, path); 90 } 91 92 100 protected Path schemeWalk(String userPath, 101 Map <String ,Object > attributes, 102 String filePath, 103 int offset) 104 { 105 int length = filePath.length(); 106 107 if (length <= offset || filePath.charAt(offset) != '(') 108 return super.schemeWalk(userPath, attributes, filePath, offset); 109 110 MergePath mergePath = new MergePath(); 111 mergePath.setUserPath(userPath); 112 113 int head = ++offset; 114 int tail = head; 115 while (tail < length) { 116 int ch = filePath.charAt(tail); 117 118 if (ch == ')') { 119 if (head + 1 != tail) { 120 String subPath = filePath.substring(head, tail); 121 122 if (subPath.startsWith("(") && subPath.endsWith(")")) 123 subPath = subPath.substring(1, subPath.length() - 1); 124 125 mergePath.addMergePath(Vfs.lookup(subPath)); 126 } 127 128 if (tail + 1 == length) 129 return mergePath; 130 else 131 return mergePath.fsWalk(userPath, attributes, filePath.substring(tail + 1)); 132 } 133 else if (ch == ';') { 134 String subPath = filePath.substring(head, tail); 135 136 if (subPath.startsWith("(") && subPath.endsWith(")")) 137 subPath = subPath.substring(1, subPath.length() - 1); 138 139 mergePath.addMergePath(Vfs.lookup(subPath)); 140 141 head = ++tail; 142 } 143 else if (ch == '(') { 144 int depth = 1; 145 146 for (tail++; tail < length; tail++) { 147 if (filePath.charAt(tail) == '(') 148 depth++; 149 else if (filePath.charAt(tail) == ')') { 150 tail++; 151 depth--; 152 if (depth == 0) 153 break; 154 } 155 } 156 157 if (depth != 0) 158 return new NotFoundPath(filePath); 159 } 160 else 161 tail++; 162 } 163 164 return new NotFoundPath(filePath); 165 } 166 167 172 public void addMergePath(Path path) 173 { 174 if (! (path instanceof MergePath)) { 175 if (path.isDirectory()) 177 path = path.lookup("./"); 178 179 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 180 181 185 pathList.add(path); 186 } 187 else if (((MergePath) path)._root == _root) 188 return; 189 else { 190 MergePath mergePath = (MergePath) path; 191 ArrayList <Path> subPaths = mergePath.getMergePaths(); 192 String pathName = "./" + mergePath._pathname + "/"; 193 194 for (int i = 0; i < subPaths.size(); i++) { 195 Path subPath = subPaths.get(i); 196 197 addMergePath(subPath.lookup(pathName)); 198 } 199 } 200 } 201 202 205 public void addClassPath() 206 { 207 addClassPath(Thread.currentThread().getContextClassLoader()); 208 } 209 210 215 public void addClassPath(ClassLoader loader) 216 { 217 String classpath = null; 218 219 if (loader instanceof DynamicClassLoader) 220 classpath = ((DynamicClassLoader) loader).getClassPath(); 221 else 222 classpath = CauchoSystem.getClassPath(); 223 224 addClassPath(classpath); 225 } 226 227 230 public void addLocalClassPath() 231 { 232 addLocalClassPath(Thread.currentThread().getContextClassLoader()); 233 } 234 235 240 public void addLocalClassPath(ClassLoader loader) 241 { 242 String classpath = null; 243 244 if (loader instanceof DynamicClassLoader) 245 classpath = ((DynamicClassLoader) loader).getLocalClassPath(); 246 else 247 classpath = System.getProperty("java.class.path"); 248 249 addClassPath(classpath); 250 } 251 252 257 public void addClassPath(String classpath) 258 { 259 char sep = CauchoSystem.getPathSeparatorChar(); 260 int head = 0; 261 int tail = 0; 262 while (head < classpath.length()) { 263 tail = classpath.indexOf(sep, head); 264 265 String segment = null; 266 if (tail < 0) { 267 segment = classpath.substring(head); 268 head = classpath.length(); 269 } 270 else { 271 segment = classpath.substring(head, tail); 272 head = tail + 1; 273 } 274 275 if (segment.equals("")) 276 continue; 277 else if (segment.endsWith(".jar") || segment.endsWith(".zip")) 278 addMergePath(JarPath.create(Vfs.lookup(segment))); 279 else 280 addMergePath(Vfs.lookup(segment)); 281 } 282 } 283 284 287 public ArrayList <Path> getMergePaths() 288 { 289 return ((MergePath) _root)._pathList; 290 } 291 292 296 public Path fsWalk(String userPath, 297 Map <String ,Object > attributes, 298 String path) 299 { 300 ArrayList <Path> pathList = getMergePaths(); 301 302 if (! userPath.startsWith("/") || pathList.size() == 0) 303 return new MergePath((MergePath) _root, userPath, attributes, path); 304 305 String bestPrefix = null; 306 for (int i = 0; i < pathList.size(); i++) { 307 Path subPath = pathList.get(i); 308 String prefix = subPath.getPath(); 309 310 if (path.startsWith(prefix) && 311 (bestPrefix == null || bestPrefix.length() < prefix.length())) { 312 bestPrefix = prefix; 313 } 314 } 315 316 if (bestPrefix != null) { 317 path = path.substring(bestPrefix.length()); 318 if (! path.startsWith("/")) 319 path = "/" + path; 320 321 return new MergePath((MergePath) _root, userPath, attributes, path); 322 } 323 324 return pathList.get(0).lookup(userPath, attributes); 325 } 326 327 330 public String getScheme() 331 { 332 return getBestPath().getScheme(); 333 } 334 335 338 public String getFullPath() 339 { 340 Path path = getBestPath(); 341 342 return path.getFullPath(); 343 } 344 345 348 public String getNativePath() 349 { 350 Path path = getBestPath(); 351 352 return path.getNativePath(); 353 } 354 355 358 public String getURL() 359 { 360 Path path = getBestPath(); 361 362 if (! path.exists()) 363 path = getWritePath(); 364 365 return path.getURL(); 366 } 367 368 371 public String getRelativePath() 372 { 373 if (_pathname.startsWith("/")) 374 return "." + _pathname; 375 else 376 return _pathname; 377 } 378 379 382 public boolean exists() 383 { 384 return getBestPath().exists(); 385 } 386 387 390 public boolean isDirectory() 391 { 392 return getBestPath().isDirectory(); 393 } 394 395 398 public boolean isFile() 399 { 400 return getBestPath().isFile(); 401 } 402 403 406 public long getLength() 407 { 408 return getBestPath().getLength(); 409 } 410 411 414 public long getLastModified() 415 { 416 return getBestPath().getLastModified(); 417 } 418 419 422 public boolean canRead() 423 { 424 return getBestPath().canRead(); 425 } 426 427 430 public boolean canWrite() 431 { 432 return getBestPath().canWrite(); 433 } 434 435 438 public ArrayList <Path> getResources(String pathName) 439 { 440 ArrayList <Path> list = new ArrayList <Path>(); 441 442 String pathname = _pathname; 443 if (pathname.startsWith("/")) 445 pathname = "." + pathname; 446 447 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 448 for (int i = 0; i < pathList.size(); i++) { 449 Path path = pathList.get(i); 450 451 path = path.lookup(pathname); 452 453 ArrayList <Path> subResources = path.getResources(pathName); 454 for (int j = 0; j < subResources.size(); j++) { 455 Path newPath = subResources.get(j); 456 457 if (! list.contains(newPath)) 458 list.add(newPath); 459 } 460 } 461 462 return list; 463 } 464 465 468 public ArrayList <Path> getResources() 469 { 470 ArrayList <Path> list = new ArrayList <Path>(); 471 472 String pathname = _pathname; 473 if (pathname.startsWith("/")) 475 pathname = "." + pathname; 476 477 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 478 for (int i = 0; i < pathList.size(); i++) { 479 Path path = pathList.get(i); 480 481 path = path.lookup(pathname); 482 483 ArrayList <Path> subResources = path.getResources(); 484 for (int j = 0; j < subResources.size(); j++) { 485 Path newPath = subResources.get(j); 486 487 if (! list.contains(newPath)) 488 list.add(newPath); 489 } 490 } 491 492 return list; 493 } 494 495 498 public String []list() throws IOException 499 { 500 ArrayList <String > list = new ArrayList <String >(); 501 502 String pathname = _pathname; 503 if (pathname.startsWith("/")) 505 pathname = "." + pathname; 506 507 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 508 for (int i = 0; i < pathList.size(); i++) { 509 Path path = pathList.get(i); 510 511 path = path.lookup(pathname); 512 513 if (path.isDirectory()) { 514 String []subList = path.list(); 515 for (int j = 0; j < subList.length; j++) { 516 if (! list.contains(subList[j])) 517 list.add(subList[j]); 518 } 519 } 520 } 521 522 return (String []) list.toArray(new String [list.size()]); 523 } 524 525 528 public boolean mkdir() 529 throws IOException 530 { 531 return getWritePath().mkdir(); 532 } 533 534 537 public boolean mkdirs() 538 throws IOException 539 { 540 return getWritePath().mkdirs(); 541 } 542 543 546 public boolean remove() 547 throws IOException 548 { 549 return getBestPath().remove(); 550 } 551 552 555 public boolean renameTo(Path path) 556 throws IOException 557 { 558 return getBestPath().renameTo(path); 559 } 560 561 564 public StreamImpl openReadImpl() throws IOException 565 { 566 StreamImpl stream = getBestPath().openReadImpl(); 567 stream.setPath(this); 568 return stream; 569 } 570 571 575 public StreamImpl openWriteImpl() throws IOException 576 { 577 StreamImpl stream = getWritePath().openWriteImpl(); 578 stream.setPath(this); 579 return stream; 580 } 581 582 586 public StreamImpl openReadWriteImpl() throws IOException 587 { 588 StreamImpl stream = getWritePath().openReadWriteImpl(); 589 stream.setPath(this); 590 return stream; 591 } 592 593 597 public StreamImpl openAppendImpl() throws IOException 598 { 599 StreamImpl stream = getWritePath().openAppendImpl(); 600 stream.setPath(this); 601 return stream; 602 } 603 604 607 public Path getWritePath() 608 { 609 String pathname = _pathname; 610 if (pathname.startsWith("/")) 612 pathname = "." + pathname; 613 614 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 615 616 if (pathList.size() == 0) 617 return new NotFoundPath(pathname); 618 else { 619 return pathList.get(0).lookup(pathname); 620 } 621 } 622 623 626 public Path getBestPath() 627 { 628 if (_bestPath != null) 629 return _bestPath; 630 631 String pathname = _pathname; 632 if (pathname.startsWith("/")) 634 pathname = "." + pathname; 635 636 ArrayList <Path> pathList = ((MergePath) _root)._pathList; 637 for (int i = 0; i < pathList.size(); i++) { 638 Path path = pathList.get(i); 639 640 Path realPath = path.lookup(pathname); 641 642 realPath.setUserPath(_userPath); 643 644 if (realPath.exists()) { 645 _bestPath = realPath; 646 return realPath; 647 } 648 } 649 650 pathname = _pathname; 651 for (int i = 0; i < pathList.size(); i++) { 652 Path path = pathList.get(i); 653 654 Path realPath = path.lookup(pathname); 655 656 realPath.setUserPath(_userPath); 657 658 if (realPath.exists()) { 659 _bestPath = realPath; 660 return realPath; 661 } 662 } 663 664 if (pathList.size() > 0) { 665 Path path = pathList.get(0); 666 667 if (pathname.startsWith("/")) 668 pathname = "." + pathname; 669 670 Path realPath = path.lookup(pathname); 671 672 realPath.setUserPath(_userPath); 673 674 return realPath; 675 } 676 677 return new NotFoundPath(_userPath); 678 } 679 680 683 public String toString() 684 { 685 return "MergePath[" + _pathname + "]"; 686 } 687 } 688 | Popular Tags |