1 23 package org.netbeans.lib.cvsclient.command; 24 25 import java.io.*; 26 import java.util.*; 27 28 import org.netbeans.lib.cvsclient.*; 29 import org.netbeans.lib.cvsclient.admin.*; 30 import org.netbeans.lib.cvsclient.connection.*; 31 import org.netbeans.lib.cvsclient.event.*; 32 import org.netbeans.lib.cvsclient.request.*; 33 34 39 public abstract class BasicCommand extends BuildableCommand { 40 43 protected List requests = new LinkedList(); 44 45 48 protected ClientServices clientServices; 49 50 53 private boolean recursive = true; 54 55 58 protected File[] files; 59 60 65 public boolean getRecursive() { 66 return recursive; 67 } 68 69 73 public boolean isRecursive() { 74 return recursive; 75 } 76 77 81 public void setRecursive(boolean recursive) { 82 this.recursive = recursive; 83 } 84 85 97 public void setFiles(File[] theFiles) { 98 if (theFiles == null) { 100 files = theFiles; 101 return; 102 } 103 104 106 files = new File[theFiles.length]; 107 int fileCount = 0; 108 int dirCount = 0; 109 int totalCount = theFiles.length; 110 for (int index = 0; index < totalCount; index++) { 111 File currentFile = theFiles[index]; 112 if (currentFile.isDirectory()) { 113 files[totalCount - (1 + dirCount)] = currentFile; 114 dirCount = dirCount + 1; 115 } 116 else { 117 files[fileCount] = currentFile; 118 fileCount = fileCount + 1; 119 } 120 } 121 } 122 123 128 public File[] getFiles() { 129 return files; 130 } 131 132 141 public File getXthFile(int index) { 142 if (index < 0 || index >= files.length) { 143 return null; 144 } 145 File file = files[index]; 146 if (!file.isFile()) { 147 return null; 148 } 149 return file; 150 } 151 152 155 public File getFileEndingWith(String ending) { 156 String locEnding = ending.replace('\\', '/'); 157 String localDir = getLocalDirectory().replace('\\','/'); 158 int index = 0; 159 for (index = 0; index < files.length; index++) { 160 String path = files[index].getAbsolutePath(); 161 String parentPath = files[index].getParentFile().getAbsolutePath().replace('\\', '/'); 162 path = path.replace('\\', '/'); 163 if ((path.endsWith(locEnding) && locEnding.indexOf('/') >= 0) || 164 (files[index].getName().equals(locEnding) && parentPath.equals(localDir))) { 165 return files[index]; 166 } 167 } 168 return null; 169 } 170 171 172 180 private void addRequests(File path) 181 throws FileNotFoundException, IOException, CommandAbortedException { 182 if (path == null) { 183 throw new IllegalArgumentException ("Cannot add requests for a " + 184 "null path."); 185 } 186 187 if (!path.exists() || path.isFile()) { 188 addRequestsForFile(path); 189 } 190 else { 191 addRequestsForDirectory(path); 192 } 193 } 194 195 200 protected boolean doesCheckFileTime() { 201 return true; 202 } 203 204 211 protected void sendEntryAndModifiedRequests(Entry entry, 212 File file) { 213 if (entry == null) { 214 return; 215 } 216 217 if (file != null && !file.exists() && entry.isNewUserFile()) { 219 return; 220 } 221 222 Date entryLastModified = entry.getLastModified(); 223 boolean hadConflicts = entry.hadConflicts(); 224 if (!hadConflicts) { 225 entry.setConflict(null); 231 } else if (fileRequiresConflictResolution(file, entry)) { 232 Entry clone = new Entry(entry.toString()); 234 clone.setConflict(Entry.HAD_CONFLICTS_AND_TIMESTAMP_MATCHES_FILE); 235 entry = clone; 236 } 237 addRequest(new EntryRequest(entry)); 238 239 if (file == null || !file.exists() || entry.isUserFileToBeRemoved()) { 240 return; 241 } 242 243 if (doesCheckFileTime() && !hadConflicts && entryLastModified != null) { 244 if (DateComparator.getInstance().equals(file.lastModified(), 245 entryLastModified.getTime())) { 246 addRequest(new UnchangedRequest(file.getName())); 247 return; 248 } 249 } 250 251 addRequest(new ModifiedRequest(file, entry.isBinary())); 252 } 253 254 266 private final boolean fileRequiresConflictResolution(File file, Entry entry) { 267 268 if (file == null) return false; 270 boolean ret = false; 271 272 if (entry.hadConflicts()) { 273 long mergedTime = entry.getLastModified().getTime() / 1000; 277 long timeStampOfFile = file.lastModified() / 1000; 278 ret = timeStampOfFile <= mergedTime; 279 } 280 281 return ret; 282 } 283 284 285 292 protected void addRequestsForDirectory(File directory) 293 throws IOException, CommandAbortedException { 294 if (!clientServices.exists(directory)) { 295 return; 296 } 297 if (clientServices.isAborted()) { 298 throw new CommandAbortedException("Command aborted during request generation", "Command aborted during request generation"); 299 } 300 301 addDirectoryRequest(directory); 302 303 File [] dirFiles = directory.listFiles(); 304 List localFiles; 305 if (dirFiles == null) { 306 localFiles = new ArrayList(0); 307 } else { 308 localFiles = new ArrayList(Arrays.asList(dirFiles)); 309 localFiles.remove(new File(directory, "CVS")); 310 } 311 312 List subDirectories = null; 313 if (isRecursive()) { 314 subDirectories = new LinkedList(); 315 } 316 317 for (Iterator it = clientServices.getEntries(directory); it.hasNext();) { 319 final Entry entry = (Entry)it.next(); 320 final File file = new File(directory, entry.getName()); 321 if (entry.isDirectory()) { 322 if (isRecursive()) { 323 subDirectories.add(new File(directory, entry.getName())); 324 } 325 } 326 else { 327 addRequestForFile(file, entry); 328 } 329 localFiles.remove(file); 330 } 331 332 if (isRecursive() && !new File(directory, "CVS").exists()) { 335 File[] subFiles = directory.listFiles(); 336 if (subFiles != null) { 337 for (int i = 0; i < subFiles.length; i++) { 338 if (subFiles[i].isDirectory() && new File(subFiles[i], "CVS").exists()) { 339 subDirectories.add(subFiles[i]); 340 } 341 } 342 } 343 } 344 345 for (Iterator it = localFiles.iterator(); it.hasNext();) { 346 String localFileName = ((File)it.next()).getName(); 347 if (!clientServices.shouldBeIgnored(directory, localFileName)) { 348 addRequest(new QuestionableRequest(localFileName)); 349 } 350 } 351 352 if (isRecursive()) { 353 for (Iterator it = subDirectories.iterator(); it.hasNext();) { 354 File subdirectory = (File)it.next(); 355 File cvsSubDir = new File(subdirectory, "CVS"); if (clientServices.exists(cvsSubDir)) { 357 addRequestsForDirectory(subdirectory); 358 } 359 } 360 } 361 } 362 363 367 protected void addRequestForFile(File file, Entry entry) { 368 sendEntryAndModifiedRequests(entry, file); 369 } 370 371 377 protected void addRequestsForFile(File file) throws IOException { 378 addDirectoryRequest(file.getParentFile()); 379 380 try { 381 final Entry entry = clientServices.getEntry(file); 382 if (entry != null) { 385 addRequestForFile(file, entry); 386 } else if (file.exists()) { 387 boolean unusedBinaryFlag = false; 390 addRequest(new ModifiedRequest(file, unusedBinaryFlag)); 391 } 392 } 393 catch (IOException ex) { 394 System.err.println("An error occurred getting the Entry " + 395 "for file " + file + ": " + ex); 396 ex.printStackTrace(); 397 } 398 } 399 400 403 protected final void addDirectoryRequest(File directory) { 404 String dir = getRelativeToLocalPathInUnixStyle(directory); 407 408 try { 409 String repository = clientServices.getRepositoryForDirectory( 410 directory.getAbsolutePath()); 411 addRequest(new DirectoryRequest(dir, repository)); 412 String tag = clientServices.getStickyTagForDirectory(directory); 413 if (tag != null) { 414 addRequest(new StickyRequest(tag)); 415 } 416 } 417 catch (FileNotFoundException ex) { 418 } 422 catch (IOException ex) { 423 System.err.println("An error occurred reading the respository " + 424 "for the directory " + dir + ": " + ex); 425 ex.printStackTrace(); 426 } 427 } 428 429 435 protected void addArgumentRequests() { 436 if (files == null) { 437 return; 438 } 439 440 for (int i = 0; i < files.length; i++) { 441 final File file = files[i]; 442 String relativePath = getRelativeToLocalPathInUnixStyle(file); 443 addRequest(new ArgumentRequest(relativePath)); 444 } 445 } 446 447 459 public void execute(ClientServices client, EventManager em) 460 throws CommandException, AuthenticationException { 461 requests.clear(); 462 clientServices = client; 463 super.execute(client, em); 464 465 if (client.isFirstCommand()) { 466 addRequest(new RootRequest(client.getRepository())); 467 } 468 469 addFileRequests(); 470 } 471 472 private void addFileRequests() throws CommandException { 473 try { 474 if (files != null && files.length > 0) { 475 for (int i = 0; i < files.length; i++) { 476 addRequests(files[i]); 477 } 478 } 479 else { 480 if (assumeLocalPathWhenUnspecified()) { 483 addRequests(new File(getLocalDirectory())); 484 } 485 } 486 } 487 catch (Exception ex) { 488 throw new CommandException(ex, ex.getLocalizedMessage()); 489 } 490 } 491 492 498 protected boolean assumeLocalPathWhenUnspecified() { 499 return true; 500 } 501 502 505 protected final void addRequest(Request request) { 506 requests.add(request); 507 } 508 509 512 protected final void addRequestForWorkingDirectory(ClientServices clientServices) 513 throws IOException { 514 addRequest(new DirectoryRequest(".", clientServices.getRepositoryForDirectory(getLocalDirectory()))); 516 } 517 518 522 protected final void addArgumentRequest(boolean value, String argument) { 523 if (!value) { 524 return; 525 } 526 527 addRequest(new ArgumentRequest(argument)); 528 } 529 530 533 protected final void appendFileArguments(StringBuffer buffer) { 534 File[] files = getFiles(); 535 if (files == null) { 536 return; 537 } 538 539 for (int index = 0; index < files.length; index++) { 540 if (index > 0) { 541 buffer.append(' '); 542 } 543 buffer.append(files[index].getName()); 544 } 545 } 546 } 547 548 | Popular Tags |