Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 125524
Collapse All | Expand All

(-)sjavacore/org/eclipse/jst/server/core/PublishUtil.java (-24 / +116 lines)
Lines 158-164 Link Here
158
	 * @throws CoreException
158
	 * @throws CoreException
159
	 */
159
	 */
160
	public static void createZipFile(IModuleResource[] resources, IPath zipPath) throws CoreException {
160
	public static void createZipFile(IModuleResource[] resources, IPath zipPath) throws CoreException {
161
		IStatus[] status = PublishUtil.publishZip(resources, zipPath, null);
161
		createZipFile(resources, zipPath, false);
162
	}
163
	
164
	/**
165
	 * Creates a new zip file containing the given module resources. Deletes the existing file
166
	 * (and doesn't create a new one) if resources is null or empty.
167
	 * 
168
	 * @param resources
169
	 * @param zipPath
170
	 * @throws CoreException
171
	 */
172
	public static void createZipFile(IModuleResource[] resources, IPath zipPath, boolean allowRewrite) throws CoreException {
173
		IStatus[] status = PublishUtil.publishZip(resources, zipPath, allowRewrite, null);
162
		if (status != null && status.length > 0)
174
		if (status != null && status.length > 0)
163
			throw new CoreException(status[0]);
175
			throw new CoreException(status[0]);
164
	}
176
	}
Lines 171-177 Link Here
171
	 * @param ts timestamp
183
	 * @param ts timestamp
172
	 * @throws CoreException if anything goes wrong
184
	 * @throws CoreException if anything goes wrong
173
	 */
185
	 */
174
	private static void copyFile(InputStream in, IPath to, long ts, IModuleFile mf) throws CoreException {
186
	private static void copyFile(InputStream in, IPath to, long ts, boolean allowRewrite, IModuleFile mf) throws CoreException {
175
		OutputStream out = null;
187
		OutputStream out = null;
176
		
188
		
177
		File tempFile = null;
189
		File tempFile = null;
Lines 191-197 Link Here
191
			out.close();
203
			out.close();
192
			out = null;
204
			out = null;
193
			
205
			
194
			moveTempFile(tempFile, file);
206
			moveTempFile(tempFile, file, allowRewrite);
195
			
207
			
196
			if (ts != IResource.NULL_STAMP && ts != 0)
208
			if (ts != IResource.NULL_STAMP && ts != 0)
197
				file.setLastModified(ts);
209
				file.setLastModified(ts);
Lines 281-286 Link Here
281
	 * @return a possibly-empty array of error and warning status 
293
	 * @return a possibly-empty array of error and warning status 
282
	 */
294
	 */
283
	public static IStatus[] publishSmart(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
295
	public static IStatus[] publishSmart(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
296
		return publishSmart(resources, path, false, monitor);
297
	}
298
	
299
	/**
300
	 * Smart copy the given module resources to the given path.
301
	 * 
302
	 * @param resources an array of module resources
303
	 * @param path an external path to copy to
304
	 * @param allowRewrite if <code>true</code>, attempt to rewrite the
305
	 *    existing file if it can't be deleted
306
	 * @param monitor a progress monitor, or <code>null</code> if progress
307
	 *    reporting and cancellation are not desired
308
	 * @return a possibly-empty array of error and warning status 
309
	 */
310
	public static IStatus[] publishSmart(IModuleResource[] resources, IPath path, boolean allowRewrite, IProgressMonitor monitor) {
284
		if (resources == null)
311
		if (resources == null)
285
			return new IStatus[0];
312
			return new IStatus[0];
286
		
313
		
Lines 370-376 Link Here
370
				
397
				
371
				if (copy) {
398
				if (copy) {
372
					try {
399
					try {
373
						copyFile(mf, toPath);
400
						copyFile(mf, toPath, allowRewrite);
374
					} catch (CoreException ce) {
401
					} catch (CoreException ce) {
375
						status.add(ce.getStatus());
402
						status.add(ce.getStatus());
376
					}
403
					}
Lines 380-386 Link Here
380
				IModuleFolder folder = (IModuleFolder) current;
407
				IModuleFolder folder = (IModuleFolder) current;
381
				IModuleResource[] children = folder.members();
408
				IModuleResource[] children = folder.members();
382
				monitor.subTask(NLS.bind(Messages.copyingTask, new String[] {resources[i].getName(), current.getName()}));
409
				monitor.subTask(NLS.bind(Messages.copyingTask, new String[] {resources[i].getName(), current.getName()}));
383
				IStatus[] stat = publishSmart(children, toPath, ProgressUtil.getSubMonitorFor(monitor, dw));
410
				IStatus[] stat = publishSmart(children, toPath, allowRewrite, ProgressUtil.getSubMonitorFor(monitor, dw));
384
				addArrayToList(status, stat);
411
				addArrayToList(status, stat);
385
			}
412
			}
386
			if (monitor.isCanceled())
413
			if (monitor.isCanceled())
Lines 404-409 Link Here
404
	 * @return a possibly-empty array of error and warning status
431
	 * @return a possibly-empty array of error and warning status
405
	 */
432
	 */
406
	public static IStatus[] publishDelta(IModuleResourceDelta delta, IPath path, IProgressMonitor monitor) {
433
	public static IStatus[] publishDelta(IModuleResourceDelta delta, IPath path, IProgressMonitor monitor) {
434
		return publishDelta(delta, path, false, monitor);
435
	}
436
	/**
437
	 * Handle a delta publish.
438
	 * 
439
	 * @param delta a module resource delta
440
	 * @param path the path to publish to
441
	 * @param allowRewrite if <code>true</code>, attempt to rewrite the
442
	 *    existing resources if they can't be deleted
443
	 * @param monitor a progress monitor, or <code>null</code> if progress
444
	 *    reporting and cancellation are not desired
445
	 * @return a possibly-empty array of error and warning status
446
	 */
447
	public static IStatus[] publishDelta(IModuleResourceDelta delta, IPath path, boolean allowRewrite, IProgressMonitor monitor) {
407
		List status = new ArrayList();
448
		List status = new ArrayList();
408
		
449
		
409
		IModuleResource resource = delta.getModuleResource();
450
		IModuleResource resource = delta.getModuleResource();
Lines 420-426 Link Here
420
					if (!f.exists())
461
					if (!f.exists())
421
						f.mkdirs();
462
						f.mkdirs();
422
					
463
					
423
					copyFile(file, path2);
464
					copyFile(file, path2, allowRewrite);
424
				}
465
				}
425
			} catch (CoreException ce) {
466
			} catch (CoreException ce) {
426
				status.add(ce.getStatus());
467
				status.add(ce.getStatus());
Lines 453-459 Link Here
453
		IModuleResourceDelta[] childDeltas = delta.getAffectedChildren();
494
		IModuleResourceDelta[] childDeltas = delta.getAffectedChildren();
454
		int size = childDeltas.length;
495
		int size = childDeltas.length;
455
		for (int i = 0; i < size; i++) {
496
		for (int i = 0; i < size; i++) {
456
			IStatus[] stat = publishDelta(childDeltas[i], path, monitor);
497
			IStatus[] stat = publishDelta(childDeltas[i], path, allowRewrite, monitor);
457
			addArrayToList(status, stat);
498
			addArrayToList(status, stat);
458
		}
499
		}
459
		IStatus[] stat = new IStatus[status.size()];
500
		IStatus[] stat = new IStatus[status.size()];
Lines 468-479 Link Here
468
			throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDeleting, path2), null));
509
			throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDeleting, path2), null));
469
	}
510
	}
470
511
471
	private static void copyFile(IModuleFile mf, IPath path) throws CoreException {
512
	private static void copyFile(IModuleFile mf, IPath path, boolean allowRewrite) throws CoreException {
472
		Trace.trace(Trace.PUBLISHING, "Copying: " + mf.getName() + " to " + path.toString());
513
		Trace.trace(Trace.PUBLISHING, "Copying: " + mf.getName() + " to " + path.toString());
473
		
514
		
474
		IFile file = (IFile) mf.getAdapter(IFile.class);
515
		IFile file = (IFile) mf.getAdapter(IFile.class);
475
		if (file != null)
516
		if (file != null)
476
			copyFile(file.getContents(), path, file.getLocalTimeStamp(), mf);
517
			copyFile(file.getContents(), path, file.getLocalTimeStamp(), allowRewrite, mf);
477
		else {
518
		else {
478
			File file2 = (File) mf.getAdapter(File.class);
519
			File file2 = (File) mf.getAdapter(File.class);
479
			InputStream in = null;
520
			InputStream in = null;
Lines 482-488 Link Here
482
			} catch (IOException e) {
523
			} catch (IOException e) {
483
				throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorReading, file2.getAbsolutePath()), e));
524
				throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorReading, file2.getAbsolutePath()), e));
484
			}
525
			}
485
			copyFile(in, path, file2.lastModified(), mf);
526
			copyFile(in, path, file2.lastModified(), allowRewrite, mf);
486
		}
527
		}
487
	}
528
	}
488
529
Lines 496-508 Link Here
496
	 * @return a possibly-empty array of error and warning status
537
	 * @return a possibly-empty array of error and warning status
497
	 */
538
	 */
498
	public static IStatus[] publishFull(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
539
	public static IStatus[] publishFull(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
540
		return publishFull(resources, path, false, monitor);
541
	}
542
	/**
543
	 * Publish the given module resources to the given path.
544
	 * 
545
	 * @param resources an array of module resources
546
	 * @param path a path to publish to
547
	 * @param allowRewrite if <code>true</code>, attempt to rewrite the
548
	 *    existing resources if they can't be deleted
549
	 * @param monitor a progress monitor, or <code>null</code> if progress
550
	 *    reporting and cancellation are not desired
551
	 * @return a possibly-empty array of error and warning status
552
	 */
553
	public static IStatus[] publishFull(IModuleResource[] resources, IPath path, boolean allowRewrite, IProgressMonitor monitor) {
499
		if (resources == null)
554
		if (resources == null)
500
			return new IStatus[0];
555
			return new IStatus[0];
501
		
556
		
502
		List status = new ArrayList();
557
		List status = new ArrayList();
503
		int size = resources.length;
558
		int size = resources.length;
504
		for (int i = 0; i < size; i++) {
559
		for (int i = 0; i < size; i++) {
505
			IStatus[] stat = copy(resources[i], path, monitor);
560
			IStatus[] stat = copy(resources[i], path, allowRewrite, monitor);
506
			addArrayToList(status, stat);
561
			addArrayToList(status, stat);
507
		}
562
		}
508
		
563
		
Lines 511-522 Link Here
511
		return stat;
566
		return stat;
512
	}
567
	}
513
568
514
	private static IStatus[] copy(IModuleResource resource, IPath path, IProgressMonitor monitor) {
569
	private static IStatus[] copy(IModuleResource resource, IPath path, boolean allowRewrite, IProgressMonitor monitor) {
515
		Trace.trace(Trace.PUBLISHING, "Copying: " + resource.getName() + " to " + path.toString());
570
		Trace.trace(Trace.PUBLISHING, "Copying: " + resource.getName() + " to " + path.toString());
516
		List status = new ArrayList();
571
		List status = new ArrayList();
517
		if (resource instanceof IModuleFolder) {
572
		if (resource instanceof IModuleFolder) {
518
			IModuleFolder folder = (IModuleFolder) resource;
573
			IModuleFolder folder = (IModuleFolder) resource;
519
			IStatus[] stat = publishFull(folder.members(), path, monitor);
574
			IStatus[] stat = publishFull(folder.members(), path, allowRewrite, monitor);
520
			addArrayToList(status, stat);
575
			addArrayToList(status, stat);
521
		} else {
576
		} else {
522
			IModuleFile mf = (IModuleFile) resource;
577
			IModuleFile mf = (IModuleFile) resource;
Lines 525-531 Link Here
525
			if (!f.exists())
580
			if (!f.exists())
526
				f.mkdirs();
581
				f.mkdirs();
527
			try {
582
			try {
528
				copyFile(mf, path);
583
				copyFile(mf, path, allowRewrite);
529
			} catch (CoreException ce) {
584
			} catch (CoreException ce) {
530
				status.add(ce.getStatus());
585
				status.add(ce.getStatus());
531
			}
586
			}
Lines 546-551 Link Here
546
	 * @return a possibly-empty array of error and warning status
601
	 * @return a possibly-empty array of error and warning status
547
	 */
602
	 */
548
	public static IStatus[] publishZip(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
603
	public static IStatus[] publishZip(IModuleResource[] resources, IPath path, IProgressMonitor monitor) {
604
		return publishZip(resources, path, false, monitor);
605
	}
606
	
607
	/**
608
	 * Creates a new zip file containing the given module resources. Deletes the existing file
609
	 * (and doesn't create a new one) if resources is null or empty.
610
	 * 
611
	 * @param resources an array of module resources
612
	 * @param path the path where the zip file should be created 
613
	 * @param allowRewrite if <code>true</code>, attempt to rewrite the
614
	 *    existing zip file if it can't be deleted
615
	 * @param monitor a progress monitor, or <code>null</code> if progress
616
	 *    reporting and cancellation are not desired
617
	 * @return a possibly-empty array of error and warning status
618
	 */
619
	public static IStatus[] publishZip(IModuleResource[] resources, IPath path, boolean allowRewrite, IProgressMonitor monitor) {
549
		if (resources == null || resources.length == 0) {
620
		if (resources == null || resources.length == 0) {
550
			// should also check if resources consists of all empty directories
621
			// should also check if resources consists of all empty directories
551
			File file = path.toFile();
622
			File file = path.toFile();
Lines 565-571 Link Here
565
			addZipEntries(zout, resources);
636
			addZipEntries(zout, resources);
566
			zout.close();
637
			zout.close();
567
			
638
			
568
			moveTempFile(tempFile, file);
639
			moveTempFile(tempFile, file, allowRewrite);
569
		} catch (CoreException e) {
640
		} catch (CoreException e) {
570
			return new IStatus[] { e.getStatus() };
641
			return new IStatus[] { e.getStatus() };
571
		} catch (Exception e) {
642
		} catch (Exception e) {
Lines 650-660 Link Here
650
	 *  
721
	 *  
651
	 * @param tempFile
722
	 * @param tempFile
652
	 * @param file
723
	 * @param file
724
	 * @param allowRewrite if <code>true</code>, attempt to rewrite an existing
725
	 *    file with the tempFile contents if the existing file can't be deleted
726
	 *    to permit the move
653
	 * @throws CoreException
727
	 * @throws CoreException
654
	 */
728
	 */
655
	private static void moveTempFile(File tempFile, File file) throws CoreException {
729
	private static void moveTempFile(File tempFile, File file, boolean allowRewrite) throws CoreException {
656
		if (file.exists()) {
730
		if (file.exists()) {
657
			if (!safeDelete(file)) {
731
			if (!safeDelete(file, allowRewrite ? 1 : 10)) {
732
				if (allowRewrite) {
733
					try {
734
						InputStream in = new FileInputStream(tempFile);
735
						IStatus status = copyFile(in, file.getPath());
736
						if (!status.isOK()) {
737
							MultiStatus status2 = new MultiStatus(JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDelete, file.toString()), null);
738
							status2.add(status);
739
							throw new CoreException(status2);
740
						}
741
						tempFile.delete();
742
						return;
743
					} catch (FileNotFoundException e) {
744
						// Shouldn't occur.
745
					}
746
				}
658
				tempFile.delete();
747
				tempFile.delete();
659
				throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDelete, file.toString()), null));
748
				throw new CoreException(new Status(IStatus.ERROR, JavaServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDelete, file.toString()), null));
660
			}
749
			}
Lines 669-677 Link Here
669
	 * @param f
758
	 * @param f
670
	 * @return <code>true</code> if it succeeds, <code>false</code> otherwise
759
	 * @return <code>true</code> if it succeeds, <code>false</code> otherwise
671
	 */
760
	 */
672
	private static boolean safeDelete(File f) {
761
	private static boolean safeDelete(File f, int retrys) {
673
		int count = 0;
762
		int count = 0;
674
		while (count < 10) {
763
		while (count < retrys) {
675
			if (!f.exists())
764
			if (!f.exists())
676
				return true;
765
				return true;
677
			
766
			
Lines 680-691 Link Here
680
			if (!f.exists())
769
			if (!f.exists())
681
				return true;
770
				return true;
682
			
771
			
683
			try {
684
				Thread.sleep(100);
685
			} catch (Exception e) {
686
				// ignore
687
			}
688
			count++;
772
			count++;
773
			// delay if we are going to try again
774
			if (count < retrys) {
775
				try {
776
					Thread.sleep(100);
777
				} catch (Exception e) {
778
					// ignore
779
				}
780
			}
689
		}
781
		}
690
		return false;
782
		return false;
691
	}
783
	}
(-)tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatServerBehaviour.java (-2 / +2 lines)
Lines 286-292 Link Here
286
		} else {
286
		} else {
287
			IPath to = getServer().getRuntime().getLocation().append("webapps").append(module[0].getName());
287
			IPath to = getServer().getRuntime().getLocation().append("webapps").append(module[0].getName());
288
			IModuleResource[] mr = getResources(module);
288
			IModuleResource[] mr = getResources(module);
289
			IStatus[] stat = PublishUtil.publishSmart(mr, to, monitor);
289
			IStatus[] stat = PublishUtil.publishSmart(mr, to, true, monitor);
290
			PublishOperation2.addArrayToList(status, stat);
290
			PublishOperation2.addArrayToList(status, stat);
291
			p.put(module[0].getId(), to.toOSString());
291
			p.put(module[0].getId(), to.toOSString());
292
		}
292
		}
Lines 327-333 Link Here
327
			}
327
			}
328
			
328
			
329
			IModuleResource[] mr = getResources(module);
329
			IModuleResource[] mr = getResources(module);
330
			IStatus[] stat = PublishUtil.publishZip(mr, jarPath, monitor);
330
			IStatus[] stat = PublishUtil.publishZip(mr, jarPath, true, monitor);
331
			List status = new ArrayList();
331
			List status = new ArrayList();
332
			PublishOperation2.addArrayToList(status, stat);
332
			PublishOperation2.addArrayToList(status, stat);
333
			PublishOperation2.throwException(status);
333
			PublishOperation2.throwException(status);
(-)tomcatcore/org/eclipse/jst/server/tomcat/core/internal/PublishOperation2.java (-3 / +3 lines)
Lines 68-74 Link Here
68
		
68
		
69
		if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
69
		if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
70
			IModuleResource[] mr = server.getResources(module);
70
			IModuleResource[] mr = server.getResources(module);
71
			IStatus[] stat = PublishUtil.publishFull(mr, path, monitor);
71
			IStatus[] stat = PublishUtil.publishFull(mr, path, true, monitor);
72
			addArrayToList(status, stat);
72
			addArrayToList(status, stat);
73
			return;
73
			return;
74
		}
74
		}
Lines 77-83 Link Here
77
		
77
		
78
		int size = delta.length;
78
		int size = delta.length;
79
		for (int i = 0; i < size; i++) {
79
		for (int i = 0; i < size; i++) {
80
			IStatus[] stat = PublishUtil.publishDelta(delta[i], path, monitor);
80
			IStatus[] stat = PublishUtil.publishDelta(delta[i], path, true, monitor);
81
			addArrayToList(status, stat);
81
			addArrayToList(status, stat);
82
		}
82
		}
83
	}
83
	}
Lines 106-112 Link Here
106
			path.toFile().mkdirs();
106
			path.toFile().mkdirs();
107
		
107
		
108
		IModuleResource[] mr = server.getResources(module);
108
		IModuleResource[] mr = server.getResources(module);
109
		IStatus[] stat = PublishUtil.publishZip(mr, jarPath, monitor);
109
		IStatus[] stat = PublishUtil.publishZip(mr, jarPath, true, monitor);
110
		addArrayToList(status, stat);
110
		addArrayToList(status, stat);
111
	}
111
	}
112
112

Return to bug 125524