1 19 20 package org.openide.filesystems; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.FilterOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectInputValidation ; 32 import java.io.OutputStream ; 33 import java.io.SyncFailedException ; 34 import org.openide.util.NbBundle; 35 import org.openide.util.Utilities; 36 37 42 public class LocalFileSystem extends AbstractFileSystem { 43 44 private static final long serialVersionUID = -5355566113542272442L; 45 46 50 private static final int REFRESH_TIME = Integer.getInteger( 51 "org.openide.filesystems.LocalFileSystem.REFRESH_TIME", 0 52 ).intValue(); private static final int SUCCESS = 0; 54 private static final int FAILURE = 1; 55 private static final int NOT_EXISTS = 3; 56 57 58 private File rootFile = new File ("."); 60 61 private boolean readOnly; 62 63 65 public LocalFileSystem() { 66 Impl impl = new Impl(this); 67 68 info = impl; 69 change = impl; 70 71 DefaultAttributes a = new InnerAttrs(this, info, change, impl); 72 attr = a; 73 list = a; 74 setRefreshTime(REFRESH_TIME); 75 } 76 77 82 @Deprecated 83 public LocalFileSystem(FileSystemCapability cap) { 84 this(); 85 setCapability(cap); 86 } 87 88 89 public String getDisplayName() { 90 return rootFile.getAbsolutePath(); 91 } 92 93 @SuppressWarnings ("deprecation") private void _setSystemName(String s) throws PropertyVetoException { 95 setSystemName(s); 96 } 97 98 104 public synchronized void setRootDirectory(File r) throws PropertyVetoException , IOException { 105 if (!r.exists() || r.isFile()) { 106 FSException.io("EXC_RootNotExist", r.getAbsolutePath()); } 108 109 String oldDisplayName = getDisplayName(); 110 _setSystemName(computeSystemName(r)); 111 112 rootFile = r; 113 114 firePropertyChange(PROP_ROOT, null, refreshRoot()); 115 firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, getDisplayName()); 116 } 117 118 121 public File getRootDirectory() { 122 return rootFile; 123 } 124 125 128 public void setReadOnly(boolean flag) { 129 if (flag != readOnly) { 130 readOnly = flag; 131 firePropertyChange( 132 PROP_READ_ONLY, (!flag) ? Boolean.TRUE : Boolean.FALSE, flag ? Boolean.TRUE : Boolean.FALSE 133 ); 134 } 135 } 136 137 140 public boolean isReadOnly() { 141 return readOnly; 142 } 143 144 148 @Deprecated 149 public void prepareEnvironment(FileSystem.Environment environment) { 150 environment.addClassPath(rootFile.getAbsolutePath()); 151 } 152 153 160 protected String computeSystemName(File rootFile) { 161 String retVal = rootFile.getAbsolutePath().replace(File.separatorChar, '/'); 162 163 return ((Utilities.isWindows() || (Utilities.getOperatingSystem() == Utilities.OS_OS2))) ? retVal.toLowerCase() 164 : retVal; 165 } 166 167 protected String [] children(String name) { 171 File f = getFile(name); 172 173 if (f.isDirectory()) { 174 return f.list(); 175 } else { 176 return null; 177 } 178 } 179 180 protected void createFolder(String name) throws java.io.IOException { 184 File f = getFile(name); 185 186 if (name.equals("")) { FSException.io("EXC_CannotCreateF", new Object [] { f.getName(), getDisplayName(), f.getAbsolutePath() }); } 189 190 if (f.exists()) { 191 FSException.io( 192 "EXC_FolderAlreadyExist", new Object [] { f.getName(), getDisplayName(), f.getAbsolutePath() } 193 ); } 195 196 boolean b = createRecursiveFolder(f); 197 198 if (!b) { 199 FSException.io("EXC_CannotCreateF", new Object [] { f.getName(), getDisplayName(), f.getAbsolutePath() }); } 201 } 202 203 206 boolean isEnabledRefreshFolder() { 207 return true; 208 } 209 210 214 private static boolean createRecursiveFolder(File f) { 215 if (f.exists()) { 216 return true; 217 } 218 219 if (!f.isAbsolute()) { 220 f = f.getAbsoluteFile(); 221 } 222 223 String par = f.getParent(); 224 225 if (par == null) { 226 return false; 227 } 228 229 if (!createRecursiveFolder(new File (par))) { 230 return false; 231 } 232 233 f.mkdir(); 234 235 return f.exists(); 236 } 237 238 protected void createData(String name) throws IOException { 239 File f = getFile(name); 240 boolean isError = true; 241 IOException creationException = null; 242 String annotationMsg = null; 243 244 try { 245 isError = f.createNewFile() ? false : true; 246 isError = isError ? true : (!f.exists()); 247 248 if (isError) { 249 Object [] msgParams; 250 msgParams = new Object [] { f.getName(), getDisplayName(), f.getAbsolutePath() }; 251 252 annotationMsg = NbBundle.getMessage(LocalFileSystem.class, "EXC_DataAlreadyExist", msgParams); creationException = new SyncFailedException (annotationMsg); 254 } 255 } catch (IOException iex) { 256 isError = true; 257 creationException = iex; 258 annotationMsg = iex.getLocalizedMessage(); 259 } 260 261 if (isError) { 262 ExternalUtil.annotate(creationException, annotationMsg); 263 throw creationException; 264 } 265 } 266 267 protected void rename(String oldName, String newName) 268 throws IOException { 269 File of = getFile(oldName); 270 File nf = getFile(newName); 271 272 if ((nf.exists() && !nf.equals(of)) || !of.renameTo(nf)) { 274 FSException.io("EXC_CannotRename", oldName, getDisplayName(), newName); } 276 } 277 278 protected void delete(String name) throws IOException { 279 File file = getFile(name); 280 281 if (deleteFile(file) != SUCCESS) { 282 if (file.exists()) { 283 FSException.io("EXC_CannotDelete", name, getDisplayName(), file.getAbsolutePath()); } else { 285 287 FileObject thisFo = findResource(name); 288 289 if (thisFo != null) { 290 if (thisFo.getParent() != null) { 291 thisFo.getParent().refresh(); 292 } 293 294 thisFo.refresh(); 295 296 if (thisFo.isValid()) { 297 FSException.io("EXC_CannotDelete", name, getDisplayName(), file.getAbsolutePath()); } 299 } 300 } 301 } 302 } 303 304 307 private static int deleteFile(File file) { 308 boolean ret = file.delete(); 309 310 if (ret) { 311 return SUCCESS; 312 } 313 314 if (!file.exists()) { 315 return NOT_EXISTS; 316 } 317 318 if (file.isDirectory()) { 319 File [] arr = file.listFiles(); 321 322 for (int i = 0; i < arr.length; i++) { 323 if (deleteFile(arr[i]) != SUCCESS) { 324 return FAILURE; 325 } 326 } 327 } 328 329 return (file.delete() ? SUCCESS : FAILURE); 331 } 332 333 protected java.util.Date lastModified(String name) { 337 return new java.util.Date (getFile(name).lastModified()); 338 } 339 340 protected boolean folder(String name) { 341 return getFile(name).isDirectory(); 342 } 343 344 protected boolean readOnly(String name) { 345 File f = getFile(name); 346 347 return !f.canWrite() && f.exists(); 348 } 349 350 protected String mimeType(String name) { 351 return null; 352 } 353 354 protected long size(String name) { 355 return getFile(name).length(); 356 } 357 358 361 385 386 protected InputStream inputStream(String name) throws java.io.FileNotFoundException { 390 FileInputStream fis; 391 File file = null; 392 393 try { 394 fis = new FileInputStream (file = getFile(name)); 395 } catch (FileNotFoundException exc) { 396 if ((file == null) || !file.exists()) { 397 ExternalUtil.annotate(exc, NbBundle.getMessage(LocalFileSystem.class, "EXC_FileOutsideModified")); 398 } 399 400 throw exc; 401 } 402 403 return fis; 404 } 405 406 protected OutputStream outputStream(final String name) 407 throws java.io.IOException { 408 OutputStream retVal = new FileOutputStream (getFile(name)); 409 410 if (Utilities.isMac()) { 412 retVal = getOutputStreamForMac42624(retVal, name); 413 } 414 415 return retVal; 416 } 417 418 private OutputStream getOutputStreamForMac42624(final OutputStream originalStream, final String name) { 419 final File f = getFile(name); 420 final long lModified = f.lastModified(); 421 OutputStream retVal = new FilterOutputStream (originalStream) { 422 public void close() throws IOException { 423 super.close(); 424 425 if ((f.length() == 0) && (f.lastModified() == lModified)) { 426 f.setLastModified(System.currentTimeMillis()); 427 } 428 } 429 }; 430 431 return retVal; 432 } 433 434 protected void lock(String name) throws IOException { 437 File file = getFile(name); 438 439 if ((!file.canWrite() && file.exists()) || isReadOnly()) { 440 FSException.io("EXC_CannotLock", name, getDisplayName(), file.getAbsolutePath()); } 442 } 443 444 protected void unlock(String name) { 445 } 446 447 protected void markUnimportant(String name) { 448 } 449 450 454 private File getFile(String name) { 455 return new File (rootFile, name); 457 } 458 459 464 private void readObject(java.io.ObjectInputStream in) 465 throws java.io.IOException , java.lang.ClassNotFoundException { 466 in.defaultReadObject(); 467 468 in.registerValidation( 469 new ObjectInputValidation () { 470 public void validateObject() { 471 if (attr.getClass() == DefaultAttributes.class) { 472 Impl impl = new Impl(LocalFileSystem.this); 473 attr = new InnerAttrs(LocalFileSystem.this, impl, impl, impl); 474 } 475 } 476 }, 0 477 ); 478 } 479 480 484 public static class Impl extends Object implements AbstractFileSystem.List, AbstractFileSystem.Info, 485 AbstractFileSystem.Change { 486 487 static final long serialVersionUID = -8432015909317698511L; 488 489 490 private LocalFileSystem fs; 491 492 495 public Impl(LocalFileSystem fs) { 496 this.fs = fs; 497 } 498 499 503 public String [] children(String name) { 504 return fs.children(name); 505 } 506 507 511 516 public void createFolder(String name) throws java.io.IOException { 517 fs.createFolder(name); 518 } 519 520 528 public void createData(String name) throws IOException { 529 fs.createData(name); 530 } 531 532 538 public void rename(String oldName, String newName) 539 throws IOException { 540 fs.rename(oldName, newName); 541 } 542 543 549 public void delete(String name) throws IOException { 550 fs.delete(name); 551 } 552 553 557 563 public java.util.Date lastModified(String name) { 564 return fs.lastModified(name); 565 } 566 567 572 public boolean folder(String name) { 573 return fs.folder(name); 574 } 575 576 581 public boolean readOnly(String name) { 582 return fs.readOnly(name); 583 } 584 585 592 public String mimeType(String name) { 593 return fs.mimeType(name); 594 } 595 596 603 public long size(String name) { 604 return fs.size(name); 605 } 606 607 614 public InputStream inputStream(String name) throws java.io.FileNotFoundException { 615 return fs.inputStream(name); 616 } 617 618 625 public OutputStream outputStream(String name) throws java.io.IOException { 626 return fs.outputStream(name); 627 } 628 629 634 public void lock(String name) throws IOException { 635 fs.lock(name); 636 } 637 638 643 public void unlock(String name) { 644 fs.unlock(name); 645 } 646 647 652 public void markUnimportant(String name) { 653 fs.markUnimportant(name); 654 } 655 } 656 657 660 private static class InnerAttrs extends DefaultAttributes { 661 static final long serialVersionUID = 1257351369229921993L; 662 LocalFileSystem lfs; 663 664 public InnerAttrs( 665 LocalFileSystem lfs, AbstractFileSystem.Info info, AbstractFileSystem.Change change, 666 AbstractFileSystem.List list 667 ) { 668 super(info, change, list); 669 this.lfs = lfs; 670 } 671 672 public Object readAttribute(String name, String attrName) { 673 if (attrName.equals("java.io.File")) { 675 return lfs.getFile(name); 676 } 677 678 return super.readAttribute(name, attrName); 679 } 680 } 681 } 682 | Popular Tags |