Skip to main content
summaryrefslogtreecommitdiffstats
blob: 871b102518a4dc969591350312b5eb272d4f9e89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package org.eclipse.team.internal.ccvs.core.resources;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.team.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.ccvs.core.ICVSFolder;
import org.eclipse.team.core.TeamPlugin;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.Assert;
import org.eclipse.team.internal.ccvs.core.util.SyncFileWriter;

/**
 * A synchronizer is responsible for managing synchronization information for local
 * CVS resources.
 * 
 * @see ResourceSyncInfo
 * @see FolderSyncInfo
 */
public class EclipseSynchronizer {
	// the resources plugin synchronizer is used to cache and possibly persist. These 
	// are keys for storing the sync info.
	private static final QualifiedName FOLDER_SYNC_KEY = new QualifiedName(CVSProviderPlugin.ID, "folder-sync"); //$NON-NLS-1$
	private static final QualifiedName RESOURCE_SYNC_KEY = new QualifiedName(CVSProviderPlugin.ID, "resource-sync"); //$NON-NLS-1$
	private static final QualifiedName IGNORE_SYNC_KEY = new QualifiedName(CVSProviderPlugin.ID, "folder-ignore"); //$NON-NLS-1$
	
	private static final FolderSyncInfo EMPTY_FOLDER_SYNC_INFO = new FolderSyncInfo("", "", null, false); //$NON-NLS-1$ //$NON-NLS-2$
	
	// the cvs eclipse synchronizer is a singleton
	private static EclipseSynchronizer instance;
	
	// track resources that have changed in a given operation
	private int nestingCount = 0;
	private Set changedResources = new HashSet();
	private Set changedFolders = new HashSet();
	
	private EclipseSynchronizer() {		
	}
	
	public static EclipseSynchronizer getInstance() {		
		return instance;
	}

	public void setFolderSync(IContainer folder, FolderSyncInfo info) throws CVSException {
		Assert.isNotNull(info);
		try {
			beginOperation(null);
			setCachedFolderSync(folder, info);
			changedFolders.add(folder);
		} finally {
			endOperation(null);
		}
	}
	
	public FolderSyncInfo getFolderSync(IContainer folder) throws CVSException {
		if (folder.getType() == IResource.ROOT) return null;					
		FolderSyncInfo info = getCachedFolderSync(folder);
		if (info == null && folder.exists()) {
			// read folder sync info and remember it
			// -- if none found then remember that fact for later
			info = SyncFileWriter.readFolderSync(CVSWorkspaceRoot.getCVSFolderFor(folder));
			if (info == null) info = EMPTY_FOLDER_SYNC_INFO;
			setCachedFolderSync(folder, info);
		}
		if (info == EMPTY_FOLDER_SYNC_INFO) info = null;
		return info;
	}	

	public void setResourceSync(IResource resource, ResourceSyncInfo info) throws CVSException {
		Assert.isNotNull(info);
		try {
			beginOperation(null);
			ensureChildResourceSyncLoaded(resource.getParent());
			setCachedResourceSync(resource, info);
			changedResources.add(resource);		
		} finally {
			endOperation(null);
		}
	}
	
	public ResourceSyncInfo getResourceSync(IResource resource) throws CVSException {
		if (resource.getType() == IResource.ROOT) return null;
		ensureChildResourceSyncLoaded(resource.getParent());
		return getCachedResourceSync(resource);
	}

	public void deleteFolderSync(IContainer folder, IProgressMonitor monitor) throws CVSException {
		try {
			beginOperation(null);
			// if the folder doesn't exist anymore the folder sync will be gone
			if(folder.exists()) {
				FolderSyncInfo info = getCachedFolderSync(folder);
				if (info != null && info != EMPTY_FOLDER_SYNC_INFO) {
					// remember that we deleted the folder sync info
					setCachedFolderSync(folder, EMPTY_FOLDER_SYNC_INFO);
					changedFolders.add(folder);
				}
			}
		} finally {
			endOperation(null);
		}
	}
	
	public void deleteResourceSync(IResource resource, IProgressMonitor monitor) throws CVSException {
		try {
			beginOperation(null);
			IContainer parent = resource.getParent();
			if(parent.exists()) {
				ensureChildResourceSyncLoaded(resource.getParent());
				setCachedResourceSync(resource, null);
				changedResources.add(resource);
			}
		} finally {
			endOperation(null);
		}
	}

	public String[] getIgnored(IContainer resource) throws CVSException {
		if(resource.getType()==IResource.ROOT) return null;
		String[] ignores = getCachedFolderIgnores(resource);
		if(ignores==null) {
			ignores = SyncFileWriter.readCVSIgnoreEntries(CVSWorkspaceRoot.getCVSFolderFor(resource));
			setCachedFolderIgnores(resource, ignores);
		}
		return ignores;
	}
	
	public void setIgnored(IContainer resource, String pattern) throws CVSException {
		String[] ignores = getIgnored(resource);
		String[] newIgnores = new String[ignores.length+1];
		System.arraycopy(ignores, 0, newIgnores, 0, ignores.length);
		newIgnores[ignores.length] = pattern;
		setCachedFolderIgnores(resource, newIgnores);
		SyncFileWriter.addCVSIgnoreEntries(CVSWorkspaceRoot.getCVSFolderFor(resource), newIgnores);
		// broadcast changes to unmanaged children - they are the only candidates for being ignored
		List possibleIgnores = new ArrayList();
		accumulateNonManagedChildren(resource, possibleIgnores);
		TeamPlugin.getManager().broadcastResourceStateChanges((IResource[])possibleIgnores.toArray(new IResource[possibleIgnores.size()]));
	}
	
	private void accumulateNonManagedChildren(IContainer folder, List possibleIgnores) throws CVSException {
		try {
			IResource[] children = folder.members();
			for (int i = 0; i < children.length; i++) {
				IResource child = children[i];
				if(getCachedResourceSync(child)==null) {
					possibleIgnores.add(child);
				}
				if(child.getType()!=IResource.FILE) {
					accumulateNonManagedChildren((IContainer)child, possibleIgnores);
				}
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
		
	public IResource[] members(IContainer folder) throws CVSException {
		try {
			ensureChildResourceSyncLoaded(folder);
			HashMap children = (HashMap)folder.getSessionProperty(RESOURCE_SYNC_KEY);			
			Set childResources = new HashSet(children.keySet() /*IResource*/);
			childResources.addAll(Arrays.asList(folder.members()));
			return (IResource[])childResources.toArray(new IResource[childResources.size()]);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	static public void startup() {
		Assert.isTrue(instance==null);
		instance = new EclipseSynchronizer();	
	}
	
	static public void shutdown() {
	}
	
	public void beginOperation(IProgressMonitor monitor) throws CVSException {
		nestingCount += 1;
		if (nestingCount == 1) {
			// any work here?
		}		
	}
	
	public void endOperation(IProgressMonitor monitor) throws CVSException {
		if (nestingCount == 1) {	
			if (! changedFolders.isEmpty() || ! changedResources.isEmpty()) {
				try {
					/*** prepare operation ***/
					// find parents of changed resources
					Set dirtyParents = new HashSet();
					for(Iterator it = changedResources.iterator(); it.hasNext();) {
						IResource resource = (IResource) it.next();
						IContainer folder = resource.getParent();
						dirtyParents.add(folder);
					}
					
					monitor = Policy.monitorFor(monitor);
					int numDirty = dirtyParents.size();
					int numResources = changedFolders.size() + numDirty;
					monitor.beginTask(null, numResources);
					monitor.subTask(Policy.bind("EclipseSynchronizer_updatingSyncEndOperation")); //$NON-NLS-1$
					
					/*** write sync info to disk ***/
					// folder sync info changes
					for(Iterator it = changedFolders.iterator(); it.hasNext();) {
						IContainer folder = (IContainer) it.next();
						if(folder.exists()) {
							FolderSyncInfo info = getCachedFolderSync(folder);
							if (info == EMPTY_FOLDER_SYNC_INFO) {
								// deleted folder sync info since we loaded it
								deleteSync(folder, dirtyParents, true);
							} else if (info == null) {
								// attempted to delete folder sync info for a previously unmanaged folder
								// no-op
							} else {
								// modified or created new folder sync info since we loaded it
								ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor(folder);
								SyncFileWriter.writeFolderSync(cvsFolder, info);
							}
						}
						monitor.worked(1);
					}

					// update progress for parents we will skip because they were deleted
					monitor.worked(dirtyParents.size() - numDirty);

					// resource sync info changes
					for (Iterator it = dirtyParents.iterator(); it.hasNext();) {
						IContainer folder = (IContainer) it.next();
						if (folder.exists()) {
							// write sync info for all children in one go
							Assert.isTrue(isChildResourceSyncLoaded(folder));
							ResourceSyncInfo[] infos = getCachedResourceSyncForChildren(folder);
							ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor(folder);
							SyncFileWriter.writeAllResourceSync(cvsFolder, infos);
						}
						monitor.worked(1);
					}
					
					/*** broadcast events ***/
					changedResources.addAll(changedFolders);				
					IResource[] resources = (IResource[]) changedResources.toArray(
						new IResource[changedResources.size()]);
					TeamPlugin.getManager().broadcastResourceStateChanges(resources);
					changedResources.clear();
					changedFolders.clear();									
				} finally {
					monitor.done();
				}
			}
		}
		nestingCount -= 1;
		Assert.isTrue(nestingCount>= 0);
	}
	
	
	/*
	 * Returns the cached resource sync info, or null if none found.
	 */
	private static ResourceSyncInfo getCachedResourceSync(IResource resource) throws CVSException {
		try {
			IContainer parent = resource.getParent();
			if(parent.exists()) {
				HashMap children = (HashMap)resource.getParent().getSessionProperty(RESOURCE_SYNC_KEY);
				if(children!=null) {
					return (ResourceSyncInfo)children.get(resource);
				}
			}
			return null;
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Returns the cached resource sync info for all children.
	 */
	private static ResourceSyncInfo[] getCachedResourceSyncForChildren(IContainer container) throws CVSException {
		try {
			if(container.exists()) {
				HashMap children = (HashMap)container.getSessionProperty(RESOURCE_SYNC_KEY);
				if(children!=null) {
					Collection syncInfo = children.values();
					return (ResourceSyncInfo[])syncInfo.toArray(new ResourceSyncInfo[syncInfo.size()]);			
				}
			}
			return new ResourceSyncInfo[0];
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Sets the cached resource sync info, use null to delete it.
	 */
	private static void setCachedResourceSync(IResource resource, ResourceSyncInfo info) throws CVSException {
		try {
			IContainer parent = resource.getParent();
			HashMap children = (HashMap)parent.getSessionProperty(RESOURCE_SYNC_KEY);
			if(children!=null) {
				if(info==null) {
					children.remove(resource);
				} else {
					// replace or add new resource sync
					children.put(resource, info);
				}
				parent.setSessionProperty(RESOURCE_SYNC_KEY, children);
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Returns the cached sync info for a folder, null if none found, or
	 * special placeholder EMPTY_FOLDER_SYNC_INFO for deleted sync info.
	 */
	private static FolderSyncInfo getCachedFolderSync(IContainer folder) throws CVSException {
		try {
			if(!folder.exists()) return null;
			return (FolderSyncInfo)folder.getSessionProperty(FOLDER_SYNC_KEY);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Sets the cached sync info for a folder, use null to flush, or special
	 * EMPTY_FOLDER_SYNC_INFO placeholder for deleted sync info.
	 */
	private static void setCachedFolderSync(IContainer folder, FolderSyncInfo info) throws CVSException {
		try {
			folder.setSessionProperty(FOLDER_SYNC_KEY, info);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}	
	
	private static String[] getCachedFolderIgnores(IContainer folder) throws CVSException {
		try {
			if(!folder.exists()) return null;
			return (String[])folder.getSessionProperty(IGNORE_SYNC_KEY);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	private static void setCachedFolderIgnores(IContainer folder, String[] ignores) throws CVSException {
		try {
			folder.setSessionProperty(IGNORE_SYNC_KEY, ignores);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Reads and caches the ResourceSyncInfos for this folder if not already cached.
	 */
	private static void ensureChildResourceSyncLoaded(IContainer folder) throws CVSException {
		try {
			// don't try to load if the information is already cached
			if (isChildResourceSyncLoaded(folder)) return;
			ResourceSyncInfo[] infos = SyncFileWriter.readAllResourceSync(CVSWorkspaceRoot.getCVSFolderFor(folder));
			HashMap children;
			if (infos != null) {
				children = new HashMap(infos.length);
				for (int i = 0; i < infos.length; i++) {
					ResourceSyncInfo syncInfo = infos[i];
					IResource peer;
					IPath path = new Path(syncInfo.getName());
					if (syncInfo.isDirectory()) {
						peer = folder.getFolder(path);
					} else {
						peer = folder.getFile(path);
					}
					children.put(peer, syncInfo);
				}
			} else {
				children = new HashMap(0);
			}
			folder.setSessionProperty(RESOURCE_SYNC_KEY, children);
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}		
	}

	private static boolean isChildResourceSyncLoaded(IContainer folder) throws CVSException {
		try {
			// root folder has no entries therefore info is always loaded
			if (folder.getType() == IResource.ROOT || !folder.exists()) return true;
			HashMap children = (HashMap)folder.getSessionProperty(RESOURCE_SYNC_KEY);
			return children!=null;
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Recursively deletes sync info from disk and cache.
	 * Also removes containers from set.
	 * 
	 * @param root the container to start at
	 * @param set the set from which to remove
	 * @param flushResourceSync must be 'true'
	 */
	private static void deleteSync(IContainer root, Set set, boolean flushResourceSync) throws CVSException {
		try {
			// delete sync info from set, disk and cache
			set.remove(root);
			if (root.exists()) {
				SyncFileWriter.getCVSSubdirectory(CVSWorkspaceRoot.getCVSFolderFor(root)).delete();
			}
			flushContainerSync(root, flushResourceSync);
			
			// recurse
			IResource[] children = root.members();
			for (int i = 0; i < children.length; i++) {
				IResource resource = children[i];
				if (resource.getType() != IResource.FILE) {
					deleteSync((IContainer) resource, set, false);
				}
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Recursively flushes sync info from cache.
	 * NOTE: made public to support test cases until refreshLocal added
	 * 
	 * @param root the container to start at
	 * @param flushResourceSync must be 'true'
	 */
	public static void flushSync(IContainer root, boolean flushResourceSync) throws CVSException {
		try {
			// delete sync info from cache
			flushContainerSync(root, flushResourceSync);
			
			// recurse
			IResource[] children = root.members();
			for (int i = 0; i < children.length; i++) {
				if(children[i].getType()!=IResource.FILE) {;
					flushSync((IContainer) children[i], false);					
				}
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	private static void flushContainerSync(IContainer container, boolean flushResourceSync) throws CVSException {
		try {
			if(container.exists()) {
				if (flushResourceSync) {
					setCachedResourceSync(container, null);
				}
				container.setSessionProperty(RESOURCE_SYNC_KEY, null);
				container.setSessionProperty(IGNORE_SYNC_KEY, null);
				container.setSessionProperty(FOLDER_SYNC_KEY, null);
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}	
}

Back to the top