1 19 20 package org.openide.filesystems; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyVetoException ; 25 import java.beans.VetoableChangeListener ; 26 import java.io.IOException ; 27 import java.io.ObjectInput ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutput ; 30 import java.io.ObjectOutputStream ; 31 import java.io.Serializable ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.HashSet ; 35 import java.util.Hashtable ; 36 import java.util.Iterator ; 37 import java.util.Vector ; 38 import org.openide.util.io.NbMarshalledObject; 39 40 52 public class Repository implements Serializable { 53 static final long serialVersionUID = -6344768369160069704L; 54 55 56 private ArrayList <FileSystem> fileSystems; 57 private transient ArrayList <FileSystem> fileSystemsClone; 58 59 60 private FileSystem system; 61 62 63 private Hashtable <String , FileSystem> names; 64 private transient FCLSupport fclSupport; 65 66 69 72 private Hashtable <RepositoryListener,RepositoryListener> listeners = 73 new Hashtable <RepositoryListener,RepositoryListener>(); 74 75 76 private VetoableChangeListener vetoListener = new VetoableChangeListener () { 77 78 public void vetoableChange(PropertyChangeEvent ev) 79 throws PropertyVetoException { 80 if (ev.getPropertyName().equals("systemName")) { 81 final String ov = (String ) ev.getOldValue(); 82 final String nv = (String ) ev.getNewValue(); 83 84 if (names.get(nv) != null) { 85 throw new PropertyVetoException ("system name already exists: " + ov + " -> " + nv, ev); } 87 } 88 } 89 }; 90 91 92 private PropertyChangeListener propListener = new PropertyChangeListener () { 93 94 public void propertyChange(PropertyChangeEvent ev) { 95 if (ev.getPropertyName().equals("systemName")) { 96 String ov = (String ) ev.getOldValue(); 98 String nv = (String ) ev.getNewValue(); 99 FileSystem fs = (FileSystem) ev.getSource(); 100 101 if (fs.isValid()) { 102 names.remove(ov); 104 } 105 106 names.put(nv, fs); 108 109 fs.setValid(true); 111 } 112 } 113 }; 114 115 120 public Repository(FileSystem def) { 121 this.system = def; 122 init(); 123 } 124 125 132 public static Repository getDefault() { 133 return ExternalUtil.getRepository(); 134 } 135 136 138 private void init() { 139 fileSystems = new ArrayList <FileSystem>(); 141 names = new Hashtable <String , FileSystem>(); 142 addFileSystem(system); 143 } 144 145 149 public final FileSystem getDefaultFileSystem() { 150 return system; 151 } 152 153 163 @Deprecated 164 public final void addFileSystem(FileSystem fs) { 165 166 boolean fireIt = false; 167 168 synchronized (this) { 169 if (!fs.assigned && !fileSystems.contains(fs)) { 171 fs.setRepository(this); 173 fileSystems.add(fs); 174 fileSystemsClone = new ArrayList <FileSystem>(fileSystems); 175 176 String systemName = fs.getSystemName(); 177 178 boolean isReg = names.get(systemName) == null; 179 180 if (isReg && !systemName.equals("")) { 182 names.put(systemName, fs); 184 fs.setValid(true); 185 } else { 186 fs.setValid(false); 188 } 189 190 fs.assigned = true; 192 193 fs.addPropertyChangeListener(propListener); 195 fs.addVetoableChangeListener(vetoListener); 196 197 fs.addNotify(); 199 200 fireIt = true; 202 } 203 } 204 205 if (fireIt) { 207 fireFileSystem(fs, true); 208 } 209 } 210 211 215 @Deprecated 216 public final void removeFileSystem(FileSystem fs) { 217 boolean fireIt = false; 218 219 synchronized (this) { 220 if (fs.isDefault()) { 221 return; 222 } 223 224 if (fireIt = fileSystems.remove(fs)) { 225 fs.setRepository(null); 226 fileSystemsClone = new ArrayList <FileSystem>(fileSystems); 227 228 if (fs.isValid()) { 230 names.remove(fs.getSystemName()); 232 fs.setValid(false); 233 } 234 235 fs.removePropertyChangeListener(propListener); 237 fs.removeVetoableChangeListener(vetoListener); 238 239 fs.removeNotify(); 241 } 242 243 fs.assigned = false; 245 } 246 247 if (fireIt) { 249 fireFileSystem(fs, false); 250 } 251 } 252 253 259 @Deprecated 260 public final void reorder(int[] perm) { 261 synchronized (this) { 262 if (perm == null) { 263 throw new IllegalArgumentException ("null permutation"); } else if (perm.length != fileSystems.size()) { 265 throw new IllegalArgumentException ( 266 "permutation is wrong size: " + perm.length + " elements but should be " + fileSystems.size() 267 ); } else if (!isPermutation(perm)) { 269 StringBuffer message = new StringBuffer ("permutation is not really a permutation:"); 271 for (int i = 0; i < perm.length; i++) { 272 message.append(' '); 273 message.append(perm[i]); 274 } 275 276 throw new IllegalArgumentException (message.toString()); 277 } 278 279 ArrayList <FileSystem> newList = new ArrayList <FileSystem>(fileSystems.size()); 280 int len = perm.length; 281 282 for (int i = 0; i < len; i++) { 283 newList.add(fileSystems.get(perm[i])); 284 } 285 286 fileSystems = newList; 287 fileSystemsClone = new ArrayList <FileSystem>(fileSystems); 288 } 289 290 fireFileSystemReordered(perm); 291 } 292 293 294 private static boolean isPermutation(int[] perm) { 295 final int len = perm.length; 296 boolean[] bool = new boolean[len]; 297 298 try { 299 for (int i = 0; i < len; i++) { 300 if (bool[perm[i]]) { 301 return false; 302 } else { 303 bool[perm[i]] = true; 304 } 305 } 306 307 return true; 308 } catch (IndexOutOfBoundsException e) { 309 return false; 310 } 311 } 312 313 317 @Deprecated 318 public final Enumeration <? extends FileSystem> getFileSystems() { 319 ArrayList <FileSystem> tempFileSystems = fileSystemsClone; 320 321 return java.util.Collections.enumeration(tempFileSystems); 322 } 323 324 328 @Deprecated 329 public final Enumeration <? extends FileSystem> fileSystems() { 330 return getFileSystems(); 331 } 332 333 338 @Deprecated 339 public final FileSystem[] toArray() { 340 ArrayList <FileSystem> tempFileSystems = fileSystemsClone; 341 342 FileSystem[] fss = new FileSystem[tempFileSystems.size()]; 343 tempFileSystems.toArray(fss); 344 345 return fss; 346 } 347 348 354 @Deprecated 355 public final FileSystem findFileSystem(String systemName) { 356 FileSystem fs = names.get(systemName); 357 358 return fs; 359 } 360 361 368 @Deprecated 369 public final synchronized void writeExternal(ObjectOutput oos) 370 throws IOException { 371 Iterator iter = fileSystems.iterator(); 372 373 while (iter.hasNext()) { 374 FileSystem fs = (FileSystem) iter.next(); 375 376 if (!fs.isDefault() && !fs.isPersistent()) { 377 oos.writeObject(new NbMarshalledObject(fs)); 378 } 379 } 380 381 oos.writeObject(null); 382 } 383 384 391 @Deprecated 392 public final synchronized void readExternal(ObjectInput ois) 393 throws IOException , ClassNotFoundException { 394 ArrayList <FileSystem> temp = new ArrayList <FileSystem>(10); 395 396 for (;;) { 397 Object obj = ois.readObject(); 398 399 if (obj == null) { 400 break; 402 } 403 404 FileSystem fs; 405 406 if (obj instanceof FileSystem) { 407 fs = (FileSystem) obj; 408 } else { 409 try { 410 NbMarshalledObject mar = (NbMarshalledObject) obj; 411 fs = (FileSystem) mar.get(); 412 } catch (IOException ex) { 413 ExternalUtil.exception(ex); 414 fs = null; 415 } catch (ClassNotFoundException ex) { 416 ExternalUtil.exception(ex); 417 fs = null; 418 } 419 } 420 421 if (fs != null) { 422 temp.add(fs); 424 } 425 } 426 427 Enumeration <? extends FileSystem> ee = getFileSystems(); 428 FileSystem fs; 429 430 while (ee.hasMoreElements()) { 431 fs = ee.nextElement(); 432 433 if (!fs.isPersistent()) { 434 removeFileSystem(fs); 435 } 436 } 437 438 system.assigned = false; 440 init(); 441 442 for (Iterator iter = temp.iterator(); iter.hasNext();) 444 addFileSystem((FileSystem) iter.next()); 445 } 446 447 462 @Deprecated 463 public final FileObject find(String aPackage, String name, String ext) { 464 assert FileUtil.assertDeprecatedMethod(); 465 466 Enumeration <? extends FileSystem> en = getFileSystems(); 467 468 while (en.hasMoreElements()) { 469 FileSystem fs = en.nextElement(); 470 FileObject fo = fs.find(aPackage, name, ext); 471 472 if (fo != null) { 473 return fo; 475 } 476 } 477 478 return null; 479 } 480 481 487 @Deprecated 488 public final FileObject findResource(String name) { 489 assert FileUtil.assertDeprecatedMethod(); 490 491 Enumeration <? extends FileSystem> en = getFileSystems(); 492 493 while (en.hasMoreElements()) { 494 FileSystem fs = en.nextElement(); 495 FileObject fo = fs.findResource(name); 496 497 if (fo != null) { 498 return fo; 500 } 501 } 502 503 return null; 504 } 505 506 511 @Deprecated 512 public final Enumeration <? extends FileObject> findAllResources(String name) { 513 assert FileUtil.assertDeprecatedMethod(); 514 515 Vector <FileObject> v = new Vector <FileObject>(8); 516 Enumeration <? extends FileSystem> en = getFileSystems(); 517 518 while (en.hasMoreElements()) { 519 FileSystem fs = en.nextElement(); 520 FileObject fo = fs.findResource(name); 521 522 if (fo != null) { 523 v.addElement(fo); 524 } 525 } 526 527 return v.elements(); 528 } 529 530 542 @Deprecated 543 public final Enumeration <? extends FileObject> findAll(String aPackage, String name, String ext) { 544 assert FileUtil.assertDeprecatedMethod(); 545 546 Enumeration <? extends FileSystem> en = getFileSystems(); 547 Vector <FileObject> ret = new Vector <FileObject>(); 548 549 while (en.hasMoreElements()) { 550 FileSystem fs = (FileSystem) en.nextElement(); 551 FileObject fo = fs.find(aPackage, name, ext); 552 553 if (fo != null) { 554 ret.addElement(fo); 555 } 556 } 557 558 return ret.elements(); 559 } 560 561 566 private void fireFileSystem(FileSystem fs, boolean add) { 567 RepositoryEvent ev = new RepositoryEvent(this, fs, add); 568 for (RepositoryListener list : new HashSet <RepositoryListener>(listeners.values())) { 569 if (add) { 570 list.fileSystemAdded(ev); 571 } else { 572 list.fileSystemRemoved(ev); 573 } 574 } 575 } 576 577 580 private void fireFileSystemReordered(int[] perm) { 581 RepositoryReorderedEvent ev = new RepositoryReorderedEvent(this, perm); 582 for (RepositoryListener list : new HashSet <RepositoryListener>(listeners.values())) { 583 list.fileSystemPoolReordered(ev); 584 } 585 } 586 587 591 @Deprecated 592 public final void addRepositoryListener(RepositoryListener list) { 593 listeners.put(list, list); 594 } 595 596 600 @Deprecated 601 public final void removeRepositoryListener(RepositoryListener list) { 602 listeners.remove(list); 603 } 604 605 607 private Object writeReplace() { 608 return new Replacer(); 609 } 610 611 final FCLSupport getFCLSupport() { 612 synchronized (FCLSupport.class) { 613 if (fclSupport == null) { 614 fclSupport = new FCLSupport(); 615 } 616 } 617 618 return fclSupport; 619 } 620 621 628 @Deprecated 629 public final void addFileChangeListener(FileChangeListener fcl) { 630 getFCLSupport().addFileChangeListener(fcl); 631 } 632 633 640 @Deprecated 641 public final void removeFileChangeListener(FileChangeListener fcl) { 642 getFCLSupport().removeFileChangeListener(fcl); 643 } 644 645 private static class Replacer implements java.io.Serializable { 646 647 static final long serialVersionUID = -3814531276726840241L; 648 649 Replacer() { 650 } 651 652 private void writeObject(ObjectOutputStream oos) 653 throws IOException { 654 ExternalUtil.getRepository().writeExternal(oos); 655 } 656 657 private void readObject(ObjectInputStream ois) 658 throws IOException , ClassNotFoundException { 659 ExternalUtil.getRepository().readExternal(ois); 660 } 661 662 663 public Object readResolve() { 664 return ExternalUtil.getRepository(); 665 } 666 } 667 } 668 | Popular Tags |