1 22 package org.netbeans.lib.cvsclient.admin; 23 24 import java.io.*; 25 import java.util.*; 26 27 import org.netbeans.lib.cvsclient.command.*; 28 import org.netbeans.lib.cvsclient.file.FileUtils; 29 30 40 public class StandardAdminHandler implements AdminHandler { 41 42 private static final Object ksEntries = new Object (); 43 44 private static Runnable t9yBeforeRename; 45 46 57 public void updateAdminData(String localDirectory, String repositoryPath, 58 Entry entry, GlobalOptions globalOptions) 59 throws IOException { 60 63 final File CVSdir = new File(localDirectory, "CVS"); 65 CVSdir.mkdirs(); 66 67 File rootFile = new File(CVSdir, "Root"); if (!rootFile.exists()) { 70 PrintWriter w = new PrintWriter(new FileWriter(rootFile)); 71 try { 72 w.println(globalOptions.getCVSRoot()); 73 } finally { 74 w.close(); 75 } 76 } 77 File repositoryFile = new File(CVSdir, "Repository"); if (!repositoryFile.exists()) { 79 PrintWriter w = new PrintWriter(new FileWriter(repositoryFile)); 80 try { 81 if (entry != null && !entry.isDirectory()) { 82 int length = entry.getName().length(); 84 repositoryPath = repositoryPath.substring(0, repositoryPath.length() - length); 85 } 86 if (repositoryPath.endsWith("/")) { repositoryPath = repositoryPath.substring(0, 88 repositoryPath.length() - 1); 89 } 90 if (repositoryPath.length() == 0) { 91 repositoryPath = "."; } 93 w.println(repositoryPath); 95 } finally { 96 w.close(); 97 } 98 } 99 100 final File entriesFile = new File(CVSdir, "Entries"); 102 if (entriesFile.createNewFile()) { 106 addDirectoryToParentEntriesFile(CVSdir); 109 110 final Writer w = new BufferedWriter(new FileWriter(entriesFile)); 115 try { 116 w.write("D"); } finally { 118 w.close(); 119 } 120 } 121 122 if (entry != null) { 124 updateEntriesFile(entriesFile, entry); 125 } 126 } 127 128 134 public boolean exists(File file) { 135 return file.exists(); 136 } 137 138 145 private void addDirectoryToParentEntriesFile(File CVSdir) 146 throws IOException { 147 synchronized (ksEntries) { 148 File parentCVSEntries = seekEntries(CVSdir.getParentFile().getParentFile()); 149 150 if (parentCVSEntries != null) { 153 final File directory = parentCVSEntries.getParentFile(); 154 final File tempFile = new File(directory, "Entries.Backup"); tempFile.createNewFile(); 156 BufferedReader reader = null; 157 BufferedWriter writer = null; 158 try { 159 reader = new BufferedReader(new FileReader(parentCVSEntries)); 160 writer = new BufferedWriter(new FileWriter(tempFile)); 161 162 String line; 163 164 boolean written = false; 167 168 Entry directoryEntry = new Entry(); 169 directoryEntry.setName(CVSdir.getParentFile().getName()); 170 directoryEntry.setDirectory(true); 171 172 while ((line = reader.readLine()) != null) { 173 if (line.trim().equals("D")) { continue; 176 } 177 178 final Entry currentEntry = new Entry(line); 179 if (currentEntry.getName() != null 180 && currentEntry.getName().equals(directoryEntry.getName())) { 181 writer.write(directoryEntry.toString()); 182 written = true; 183 } 184 else { 185 writer.write(line); 186 } 187 writer.newLine(); 188 } 189 190 if (!written) { 191 writer.write(directoryEntry.toString()); 192 writer.newLine(); 193 } 194 } finally { 195 try { 196 if (writer != null) { 197 writer.close(); 198 } 199 } finally { 200 if (reader != null) { 201 reader.close(); 202 } 203 } 204 } 205 206 if (t9yBeforeRename != null) t9yBeforeRename.run(); 207 FileUtils.renameFile(tempFile, parentCVSEntries); 208 } 209 } 210 } 211 212 231 private void updateEntriesFile(File originalFile, Entry entry) 232 throws IOException { 233 synchronized (ksEntries) { 234 final File directory = originalFile.getParentFile(); 235 final File tempFile = new File(directory, "Entries.Backup"); tempFile.createNewFile(); 237 238 BufferedReader reader = null; 239 BufferedWriter writer = null; 240 try { 241 reader = new BufferedReader(new FileReader(originalFile)); 242 writer = new BufferedWriter(new FileWriter(tempFile)); 243 String line; 244 boolean written = false; 248 while ((line = reader.readLine()) != null) { 249 final Entry currentEntry = new Entry(line); 250 if ((currentEntry.getName() != null) && 251 currentEntry.getName().equals(entry.getName())) { 252 writer.write(entry.toString()); 253 written = true; 254 } 255 else { 256 writer.write(line); 257 } 258 writer.newLine(); 259 } 260 if (!written) { 261 writer.write(entry.toString()); 262 writer.newLine(); 263 } 264 } finally { 265 try { 266 if (writer != null) { 267 writer.close(); 268 } 269 } finally { 270 if (reader != null) { 271 reader.close(); 272 } 273 } 274 } 275 276 if (t9yBeforeRename != null) t9yBeforeRename.run(); 277 FileUtils.renameFile(tempFile, originalFile); 278 } 279 } 280 281 286 public Entry getEntry(File file) throws IOException { 287 final File entriesFile = seekEntries(file.getParentFile()); if (entriesFile == null) { 291 return null; 292 } 293 294 processEntriesDotLog(new File(file.getParent(), "CVS")); 296 BufferedReader reader = null; 297 Entry entry = null; 298 boolean found = false; 299 try { 300 reader = new BufferedReader(new FileReader(entriesFile)); 301 String line; 302 while (!found && ((line = reader.readLine()) != null)) { 303 entry = new Entry(line); 304 if (entry.getName() != null) { 307 File entryFile = new File(file.getParentFile(), entry.getName()); 309 found = entryFile.equals(file); 310 } 311 } 312 } 313 finally { 314 if (reader != null) { 315 reader.close(); 316 } 317 } 318 319 if (!found) { 320 return null; 321 } 322 323 return entry; 324 } 325 326 331 public Entry[] getEntriesAsArray(File directory) 332 throws IOException { 333 List entries = new LinkedList(); 334 335 final File entriesFile = seekEntries(directory); 336 if (entriesFile == null) { 338 return new Entry[0]; 339 } 340 341 processEntriesDotLog(new File(directory, "CVS")); 343 BufferedReader reader = null; 344 Entry entry = null; 345 try { 346 reader = new BufferedReader(new FileReader(entriesFile)); 347 String line; 348 while ((line = reader.readLine()) != null) { 349 entry = new Entry(line); 350 if (entry.getName() != null) { 353 entries.add(entry); 354 } 355 } 356 } 357 finally { 358 if (reader != null) { 359 reader.close(); 360 } 361 } 362 Entry[] toReturnArray = new Entry[entries.size()]; 363 toReturnArray = (Entry[])entries.toArray(toReturnArray); 364 return toReturnArray; 365 } 366 367 372 public Iterator getEntries(File directory) 373 throws IOException { 374 List entries = new LinkedList(); 375 376 final File entriesFile = seekEntries(directory); 377 if (entriesFile == null) { 379 return entries.iterator(); 380 } 381 382 processEntriesDotLog(new File(directory, "CVS")); 384 BufferedReader reader = null; 385 Entry entry = null; 386 try { 387 reader = new BufferedReader(new FileReader(entriesFile)); 388 String line; 389 while ((line = reader.readLine()) != null) { 390 entry = new Entry(line); 391 if (entry.getName() != null) { 394 entries.add(entry); 395 } 396 } 397 } 398 finally { 399 if (reader != null) { 400 reader.close(); 401 } 402 } 403 return entries.iterator(); 404 } 405 406 411 public void setEntry(File file, Entry entry) 412 throws IOException { 413 String parent = file.getParent(); 414 File entriesFile = seekEntries(parent); 415 if (entriesFile == null) { 416 entriesFile = new File(parent, "CVS/Entries"); } 418 processEntriesDotLog(new File(parent, "CVS")); updateEntriesFile(entriesFile, entry); 420 } 421 422 427 public void removeEntry(File file) throws IOException { 428 synchronized (ksEntries) { 429 final File entriesFile = seekEntries(file.getParent()); 430 431 if (entriesFile == null) { 434 return; 435 } 436 437 processEntriesDotLog(new File(file.getParent(), "CVS")); 439 final File directory = file.getParentFile(); 440 final File tempFile = new File(directory, "Entries.Backup"); tempFile.createNewFile(); 442 BufferedReader reader = null; 443 BufferedWriter writer = null; 444 try { 445 reader = new BufferedReader(new FileReader(entriesFile)); 446 writer = new BufferedWriter(new FileWriter(tempFile)); 447 String line; 448 boolean directoriesExist = false; 451 while ((line = reader.readLine()) != null) { 452 final Entry currentEntry = new Entry(line); 453 if ((currentEntry.getName() != null) && 454 455 !currentEntry.getName().equals(file.getName())) { 456 writer.write(currentEntry.toString()); 457 writer.newLine(); 458 directoriesExist = directoriesExist || 459 currentEntry.isDirectory(); 460 } 461 } 462 if (!directoriesExist) { 463 writer.write("D"); writer.newLine(); 465 } 466 } finally { 467 try { 468 if (writer != null) { 469 writer.close(); 470 } 471 } finally { 472 if (reader != null) { 473 reader.close(); 474 } 475 } 476 } 477 478 if (t9yBeforeRename != null) t9yBeforeRename.run(); 479 FileUtils.renameFile(tempFile, entriesFile); 480 } 481 } 482 483 495 public String getRepositoryForDirectory(String directory, 496 String repository) 497 throws IOException { 498 File repositoryFile = null; 501 String repositoryDirs = ""; File dirFile = new File(directory); 503 while (true) { 504 if (dirFile == null 507 || dirFile.getName().length() == 0 508 || !dirFile.exists()) { 509 throw new FileNotFoundException("Repository file not found " + "for directory " + directory); } 512 513 repositoryFile = new File(dirFile, "CVS/Repository"); if (repositoryFile.exists()) { 515 break; 516 } 517 repositoryDirs = '/' + dirFile.getName() + repositoryDirs; 518 dirFile = dirFile.getParentFile(); 519 } 520 BufferedReader reader = null; 521 String fileRepository = null; 524 try { 525 reader = new BufferedReader(new FileReader(repositoryFile)); 526 fileRepository = reader.readLine(); 527 } 528 finally { 529 if (reader != null) { 530 reader.close(); 531 } 532 } 533 534 if (fileRepository == null) { 535 fileRepository = ""; } 537 538 fileRepository += repositoryDirs; 539 if (fileRepository.startsWith("/")) { return fileRepository; 542 } 543 544 if (fileRepository.startsWith("./")) { fileRepository = fileRepository.substring(2); 547 } 548 return repository + '/' + fileRepository; 551 } 552 553 560 private void processEntriesDotLog(File directory) throws IOException { 561 562 synchronized (ksEntries) { 563 final File entriesDotLogFile = new File(directory, "Entries.Log"); if (!entriesDotLogFile.exists()) { 565 return; 566 } 567 568 BufferedReader reader = new BufferedReader(new FileReader( 569 entriesDotLogFile)); 570 571 List additionsList = new LinkedList(); 574 HashSet removalSet = new HashSet(); 575 576 String line; 577 578 try { 579 while ((line = reader.readLine()) != null) { 580 if (line.startsWith("A ")) { final Entry entry = new Entry(line.substring(2)); 582 additionsList.add(entry); 583 } 584 else if (line.startsWith("R ")) { final Entry entry = new Entry(line.substring(2)); 586 removalSet.add(entry.getName()); 587 } 588 } 590 } finally { 591 reader.close(); 592 } 593 594 if ((additionsList.size() > 0) || (removalSet.size() > 0)) { 595 final File backup = new File(directory, "Entries.Backup"); final BufferedWriter writer = new BufferedWriter(new FileWriter( 597 backup)); 598 final File entriesFile = new File(directory, "Entries"); reader = new BufferedReader(new FileReader(entriesFile)); 600 601 try { 602 int directoryCount = 0; 605 606 while ((line = reader.readLine()) != null) { 607 if (line.trim().equals("D")) { continue; 611 } 612 613 final Entry entry = new Entry(line); 614 615 if (entry.isDirectory()) { 616 directoryCount++; 617 } 618 619 if (!removalSet.contains(entry.getName())) { 620 writer.write(entry.toString()); 621 writer.newLine(); 622 if (entry.isDirectory()) { 623 directoryCount--; 624 } 625 } 626 } 627 Iterator it = additionsList.iterator(); 628 while (it.hasNext()) { 629 final Entry entry = (Entry)it.next(); 630 if (entry.isDirectory()) { 631 directoryCount++; 632 } 633 writer.write(entry.toString()); 634 writer.newLine(); 635 } 636 if (directoryCount == 0) { 637 writer.write("D"); writer.newLine(); 639 } 640 } finally { 641 try { 642 reader.close(); 643 } finally { 644 writer.close(); 645 } 646 } 647 648 if (t9yBeforeRename != null) t9yBeforeRename.run(); 649 FileUtils.renameFile(backup, entriesFile); 650 } 651 entriesDotLogFile.delete(); 652 } 653 } 654 655 661 public Set getAllFiles(File directory) throws IOException { 662 TreeSet fileSet = new TreeSet(); 663 BufferedReader reader = null; 664 try { 665 final File entriesFile = seekEntries(directory); 667 if (entriesFile != null) { 670 return fileSet; } 672 673 reader = new BufferedReader(new FileReader(entriesFile)); 674 String line; 675 676 while ((line = reader.readLine()) != null) { 677 final Entry entry = new Entry(line); 678 679 if (entry.getName() != null) { 680 final File f = new File(directory, entry.getName()); 681 if (f.isFile()) { 682 fileSet.add(f); 683 } 684 } 685 } 686 } 687 finally { 688 if (reader != null) { 689 reader.close(); 690 } 691 } 692 693 return fileSet; 694 } 695 696 702 public String getStickyTagForDirectory(File directory) { 703 BufferedReader reader = null; 704 File tagFile = new File(directory, "CVS/Tag"); 706 try { 707 reader = new BufferedReader(new FileReader(tagFile)); 708 String tag = reader.readLine(); 709 return tag; 710 } 711 catch (IOException ex) { 712 } 714 finally { 715 if (reader != null) { 716 try { 717 reader.close(); 718 } 719 catch (IOException ex) { 720 } 722 } 723 } 724 return null; 725 } 726 727 733 private static File seekEntries(File folder) { 734 synchronized(ksEntries) { 735 File entries = new File(folder, "CVS/Entries"); if (entries.exists()) { 737 return entries; 738 } else { 739 File backup = new File(folder, "CVS/Entries.Backup"); if (backup.exists()) { 741 try { 742 if (t9yBeforeRename != null) t9yBeforeRename.run(); 743 FileUtils.renameFile(backup, entries); 744 return entries; 745 } catch (IOException e) { 746 } 748 } 749 } 750 return null; 751 } 752 } 753 754 private static File seekEntries(String folder) { 755 return seekEntries(new File(folder)); 756 } 757 758 static void t9yBeforeRenameSync(Runnable run) { 759 t9yBeforeRename = run; 760 } 761 } 762 | Popular Tags |