1 19 20 package org.openide.filesystems; 21 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.OutputStream ; 28 import java.io.Serializable ; 29 import java.lang.ref.Reference ; 30 import java.lang.ref.WeakReference ; 31 import java.util.Collection ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.StringTokenizer ; 35 import org.openide.util.Enumerations; 36 import org.openide.util.Lookup; 37 import org.openide.util.NbCollections; 38 import org.openide.util.SharedClassObject; 39 import org.openide.util.actions.SystemAction; 40 41 100 public abstract class AbstractFileSystem extends FileSystem { 101 102 static final long serialVersionUID = -3345098214331282438L; 103 104 105 private static SystemAction[] SYSTEM_ACTIONS; 106 107 108 private static final SystemAction[] NO_SYSTEM_ACTIONS = new SystemAction[] { }; 109 110 111 static transient private PathElements lastEnum; 112 113 114 private transient AbstractFileObject root; 115 116 117 private transient RefreshRequest refresher; 118 119 120 protected List list; 121 122 123 protected Change change; 124 125 129 protected Transfer transfer; 130 131 132 protected Info info; 133 134 135 protected Attr attr; 136 137 140 public void refresh(boolean expected) { 141 for (FileObject fo : NbCollections.iterable(getAbstractRoot().existingSubFiles(true))) { 142 fo.refresh(expected); 143 } 144 } 145 146 149 public abstract String getDisplayName(); 150 151 155 public FileObject getRoot() { 156 return getAbstractRoot(); 157 } 158 159 173 @Deprecated 174 public FileObject find(String aPackage, String name, String ext) { 175 Enumeration <String > st = NbCollections.checkedEnumerationByFilter(new StringTokenizer (aPackage, "."), String .class, true); 178 if ((name == null) || (ext == null)) { 179 FileObject fo = getAbstractRoot().find(st); 181 182 return ((fo != null) && fo.isFolder()) ? fo : null; 183 } else { 184 Enumeration <String > en = Enumerations.concat(st, Enumerations.singleton(name + '.' + ext)); 185 186 return getAbstractRoot().find(en); 188 } 189 } 190 191 201 public FileObject findResource(String name) { 202 if (name.length() == 0) { 203 return getAbstractRoot(); 204 } 205 206 208 PathElements local = lastEnum; 209 210 if ((local == null) || !local.getOriginalName().equals(name)) { 211 local = new PathElements(name); 212 lastEnum = local; 213 } 214 215 return getAbstractRoot().find(local.getEnumeration()); 216 } 217 218 223 protected <T extends FileObject> Reference <T> createReference(T fo) { 224 return (new WeakReference <T>(fo)); 225 } 226 227 231 protected final Reference <? extends FileObject> findReference(String resourceName) { 232 if (resourceName.length() == 0) { 233 return null; 234 } else { 235 Enumeration <String > tok = NbCollections.checkedEnumerationByFilter(new StringTokenizer (resourceName, "/"), String .class, true); 237 return getAbstractRoot().findRefIfExists(tok); 238 } 239 } 240 241 244 boolean isEnabledRefreshFolder() { 245 return (refresher != null); 246 } 247 248 252 public SystemAction[] getActions() { 253 if (!isEnabledRefreshFolder()) { 254 return NO_SYSTEM_ACTIONS; 255 } else { 256 if (SYSTEM_ACTIONS == null) { 257 try { 258 ClassLoader l = Lookup.getDefault().lookup(ClassLoader .class); 259 260 if (l == null) { 261 l = getClass().getClassLoader(); 262 } 263 264 Class <?> c = Class.forName("org.openide.actions.FileSystemRefreshAction", true, l); SystemAction ra = SharedClassObject.findObject(c.asSubclass(SystemAction.class), true); 266 267 SYSTEM_ACTIONS = new SystemAction[] { ra }; 269 } catch (Exception ex) { 270 SYSTEM_ACTIONS = NO_SYSTEM_ACTIONS; 274 } 275 } 276 277 return SYSTEM_ACTIONS; 278 } 279 } 280 281 286 protected synchronized final void setRefreshTime(int ms) { 287 if (refresher != null) { 288 refresher.stop(); 289 } 290 291 if ((ms <= 0) || (System.getProperty("netbeans.debug.heap") != null)) { 292 refresher = null; 293 } else { 294 refresher = new RefreshRequest(this, ms); 295 } 296 } 297 298 303 protected final int getRefreshTime() { 304 RefreshRequest r = refresher; 305 306 return (r == null) ? 0 : r.getRefreshTime(); 307 } 308 309 315 final synchronized AbstractFileObject refreshRootImpl() { 316 if (root != null) { 317 root.validFlag = false; 318 } 319 320 root = createFileObject(null, ""); 322 return root; 323 } 324 325 331 protected final FileObject refreshRoot() { 332 return refreshRootImpl(); 333 } 334 335 343 protected final void refreshResource(String name, boolean expected) { 344 AbstractFileObject fo = (AbstractFileObject) findResourceIfExists(name); 345 346 if (fo != null) { 347 fo.refresh(null, null, true, expected); 349 } 350 } 351 352 359 protected final Enumeration <? extends FileObject> existingFileObjects(FileObject fo) { 360 return existingFileObjects((AbstractFolder) fo); 361 } 362 363 final Enumeration <? extends FileObject> existingFileObjects(AbstractFolder fo) { 364 class OnlyValidAndDeep implements org.openide.util.Enumerations.Processor<Reference <AbstractFolder>,FileObject> { 365 public FileObject process(Reference <AbstractFolder> obj, Collection <Reference <AbstractFolder>> toAdd) { 366 AbstractFolder file = obj.get(); 367 368 if (file != null) { 369 AbstractFolder[] arr = file.subfiles(); 370 371 for (int i = 0; i < arr.length; i++) { 373 toAdd.add(new WeakReference <AbstractFolder>(arr[i])); 374 } 375 376 return file.isValid() ? file : null; 377 } 378 379 return null; 380 } 381 } 382 383 Reference <AbstractFolder> ref = new WeakReference <AbstractFolder>(fo); 384 Enumeration <Reference <AbstractFolder>> singleEn = org.openide.util.Enumerations.<Reference <AbstractFolder>>singleton(ref); 385 return org.openide.util.Enumerations.removeNulls( 386 org.openide.util.Enumerations.queue(singleEn, new OnlyValidAndDeep()) 387 ); 388 } 389 390 391 394 boolean isLastModifiedCacheEnabled() { 395 return true; 396 } 397 398 408 private FileObject findResourceIfExists(String name) { 409 if (name.length() == 0) { 410 return getAbstractRoot(); 411 } else { 412 Enumeration <String > tok = NbCollections.checkedEnumerationByFilter(new StringTokenizer (name, "/"), String .class, true); 414 return getAbstractRoot().findIfExists(tok); 415 } 416 } 417 418 424 AbstractFileObject createFileObject(AbstractFileObject parent, String name) { 425 return new AbstractFileObject(this, parent, name); 426 } 427 428 431 final AbstractFileObject getAbstractRoot() { 432 synchronized (this) { 433 if (root == null) { 434 return refreshRootImpl(); 435 } 436 } 437 438 return root; 439 } 440 441 443 private void writeObject(ObjectOutputStream oos) throws IOException { 444 ObjectOutputStream.PutField fields = oos.putFields(); 445 446 fields.put("change", change); fields.put("info", info); fields.put("attr", attr); fields.put("list", list); fields.put("transfer", transfer); oos.writeFields(); 452 453 oos.writeInt(getRefreshTime()); 454 } 455 456 458 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 459 ObjectInputStream.GetField fields = ois.readFields(); 460 461 Object o1 = readImpl("change", fields); Object o2 = readImpl("info", fields); Object o3 = readImpl("attr", fields); Object o4 = readImpl("list", fields); Object o5 = readImpl("transfer", fields); 467 change = (Change) o1; 468 info = (Info) o2; 469 attr = (Attr) o3; 470 list = (List) o4; 471 transfer = (Transfer) o5; 472 473 setRefreshTime(ois.readInt()); 474 } 475 476 480 484 static Object readImpl(String name, ObjectInputStream.GetField fields) 485 throws ClassNotFoundException , IOException { 486 Object o = fields.get(name, null); 487 488 if (o instanceof LocalFileSystem) { 489 return new LocalFileSystem.Impl((LocalFileSystem) o); 490 } else if (o instanceof JarFileSystem) { 491 return new JarFileSystem.Impl((JarFileSystem) o); 492 } 493 494 return o; 495 } 496 497 504 protected boolean checkVirtual(String name) { 505 return false; 506 } 507 508 513 protected boolean canWrite(String name) { 514 AbstractFileObject afo = (AbstractFileObject) this.findResource(name); 515 516 return (afo != null) ? afo.superCanWrite() : false; 517 } 518 519 524 protected boolean canRead(String name) { 525 AbstractFileObject afo = (AbstractFileObject) this.findResource(name); 526 527 return (afo != null) ? afo.superCanRead() : false; 528 } 529 530 536 protected void markImportant(String name, boolean important) { 537 if (!important && (info != null)) { 538 info.markUnimportant(name); 539 } 540 } 541 542 544 public interface List extends Serializable { 545 546 @Deprecated 547 long serialVersionUID = -6242105832891012528L; 548 549 555 public String [] children(String f); 556 } 557 558 560 public interface Change extends Serializable { 561 562 @Deprecated 563 long serialVersionUID = -5841597109944924596L; 564 565 569 public void createFolder(String name) throws IOException ; 570 571 577 public void createData(String name) throws IOException ; 578 579 585 public void rename(String oldName, String newName) 586 throws IOException ; 587 588 593 public void delete(String name) throws IOException ; 594 } 595 596 600 public interface Transfer extends Serializable { 601 602 @Deprecated 603 long serialVersionUID = -8945397853892302838L; 604 605 614 public boolean move(String name, Transfer target, String targetName) 615 throws IOException ; 616 617 626 public boolean copy(String name, Transfer target, String targetName) 627 throws IOException ; 628 } 629 630 632 public interface Info extends Serializable { 633 634 @Deprecated 635 long serialVersionUID = -2438286177948307985L; 636 637 642 public Date lastModified(String name); 643 644 648 public boolean folder(String name); 649 650 654 public boolean readOnly(String name); 655 656 665 public String mimeType(String name); 666 667 673 public long size(String name); 674 675 681 public InputStream inputStream(String name) throws FileNotFoundException ; 682 683 689 public OutputStream outputStream(String name) throws IOException ; 690 691 697 public void lock(String name) throws IOException ; 698 699 702 public void unlock(String name); 703 704 709 public void markUnimportant(String name); 710 } 711 712 714 public interface Attr extends Serializable { 715 716 @Deprecated 717 long serialVersionUID = 5978845941846736946L; 718 719 724 public Object readAttribute(String name, String attrName); 725 726 732 public void writeAttribute(String name, String attrName, Object value) 733 throws IOException ; 734 735 739 public Enumeration <String > attributes(String name); 740 741 745 public void renameAttributes(String oldName, String newName); 746 747 751 public void deleteAttributes(String name); 752 } 753 } 754 | Popular Tags |