Skip to main content
summaryrefslogtreecommitdiffstats
blob: af1451a5f4cca310a1dfe2f64b55417ce99fda2e (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
package org.eclipse.team.internal.ccvs.core.resources;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
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.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ISynchronizer;
import org.eclipse.core.resources.ResourcesPlugin;
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.CVSTag;
import org.eclipse.team.core.TeamPlugin;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;

/**
 * A synchronizer is responsible for managing synchronization information for local
 * CVS resources.
 * 
 * @see ResourceSyncInfo
 * @see FolderSyncInfo
 */
public class EclipseSynchronizer {
	private static final QualifiedName FOLDER_SYNC_KEY = new QualifiedName(CVSProviderPlugin.ID, "folder-sync");
	private static final QualifiedName RESOURCE_SYNC_KEY = new QualifiedName(CVSProviderPlugin.ID, "resource-sync");
	private static EclipseSynchronizer instance;
	private int nestingCount = 0;
	private Set changedResources = new HashSet();
	private Set changedFolders = new HashSet();
	
	private EclipseSynchronizer() {
		getSynchronizer().add(RESOURCE_SYNC_KEY);
		getSynchronizer().add(FOLDER_SYNC_KEY);
	}
	
	/**
	 * Associates the provided folder sync information with the given folder. The folder
	 * must exist on the file system.
	 * <p>
	 * The workbench and team plugins are notified that the state of this resources has 
	 * changed.</p>
	 * 
	 * @param file the file or folder for which to associate the sync info.
	 * @param info the folder sync to set.
	 * 
 	 * @throws CVSException if there was a problem adding sync info.
	 */
	public void setFolderSync(IContainer folder, FolderSyncInfo info) throws CVSException {
		beginOperation();
		setCachedFolderSync(folder, info);
		changedFolders.add(folder);
		endOperation();
	}
	
	/**
	 * Answers the folder sync information associated with this folder or <code>null</code>
	 * if none is available.
	 * 
	 * @param folder the folder for which to return folder sync info.
 	 * @throws CVSException if there was a problem adding folder sync info.
	 */
	public FolderSyncInfo getFolderSync(IContainer folder) throws CVSException {
		if(folder.getType()==IResource.ROOT) return null;
		beginOperation();		
		FolderSyncInfo info = getCachedFolderSync(folder);
		if (info == null) {
			info = readFolderConfig(folder);
			setCachedFolderSync(folder, info);
		}
		endOperation();
		return info;
	}	

	/**
	 * Associates the provided sync information with the given file or folder. The resource
	 * may or may not exist on the file system however the parent folder must be a cvs
	 * folder.
	 * <p>
	 * The workbench and team plugins are notified that the state of this resources has 
	 * changed.</p>
	 * 
	 * @param file the file or folder for which to associate the sync info.
	 * @param info to set. The name in the resource info must match the file or folder name.
	 * 
 	 * @throws CVSException if there was a problem adding sync info.
	 */
	public void setResourceSync(IResource resource, ResourceSyncInfo info) throws CVSException {
		beginOperation();
		setCachedResourceSync(resource, info);
		changedResources.add(resource);		
		endOperation();
	}
	
	/**
	 * Answers the sync information associated with this file of folder or <code>null</code>
	 * if none is available. A resource cannot have sync information if its parent folder
	 * does not exist.
	 * 
	 * @param file the file or folder for which to return sync info.
 	 * @throws CVSException if there was a problem adding sync info or broadcasting
	 * the changes.
	 */
	public ResourceSyncInfo getResourceSync(IResource resource) throws CVSException {
		if(resource.getType()==IResource.ROOT) return null;
		beginOperation();
		ResourceSyncInfo info = getCachedResourceSync(resource);
		if (info == null) {
			IContainer parent = resource.getParent();
			if (parent != null) {
				ResourceSyncInfo[] infos = readEntriesFile(resource.getParent());
				if (infos != null) {
					for (int i = 0; i < infos.length; i++) {
						ResourceSyncInfo syncInfo = infos[i];
						IResource peer;
						if (resource.getName().equals(syncInfo.getName())) {
							info = syncInfo;
							peer = resource;
						} else {
							IPath path = new Path(syncInfo.getName());
							if (syncInfo.isDirectory()) {
								peer = parent.getFolder(path);
							} else {
								peer = parent.getFile(path);
							}
						}
						// may create a phantom if the sibling resource does not exist.
						setCachedResourceSync(peer, syncInfo);
					}
				}
			}
		}
		endOperation();
		return info;
	}
	
	/**
	 * Removes the folder's and all children's folder sync information. This will essentially remove
	 * all CVS knowledge from these resources.
	 */		
	public void deleteFolderSync(IContainer folder, IProgressMonitor monitor) throws CVSException {
		beginOperation();
		setCachedFolderSync(folder, null);
		changedFolders.add(folder);
		endOperation();
	}
	
	/**
	 * Removes the resource's sync information.
	 */
	public void deleteResourceSync(IResource resource, IProgressMonitor monitor) throws CVSException {
		beginOperation();
		setCachedResourceSync(resource, null);
		changedResources.add(resource);
		endOperation();
	}

	/**
	 * Answers if the following resource is ignored
	 */
	public boolean isIgnored(IResource resource) {
		// FIX ME!
		return false;
	}
	
	/**
	 * Adds a pattern or file name to be ignored in the current files directory.
	 */
	public void setIgnored(IResource resource, String pattern) throws CVSException {
		// FIX ME!
	}
	
	/**
	 * Answers an array with the sync information for immediate child resources of this folder. Note 
	 * that the returned sync information may be for resources that no longer exist (e.g. in the
	 * case of a pending deletion).
	 * 
	 * @param folder the folder for which to return the children resource sync infos. The folder
	 * must exist.
	 * 
	 * @throws CVSException if an error occurs retrieving the sync info.
	 */
	public IResource[] members(IContainer folder) throws CVSException {
		try {
			IResource[] children = folder.members(true);
			List list = new ArrayList(children.length);
			for (int i = 0; i < children.length; ++i) {
				IResource child = children[i];
				if (! child.isPhantom() || getCachedResourceSync(child) != null) {
					list.add(child);
				}
			}
			return (IResource[]) list.toArray(new IResource[list.size()]);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	public void beginOperation() throws CVSException {
		if (nestingCount++ == 0) {
			// any work here?
		}
	}
	
	public void endOperation() throws CVSException {
		if (--nestingCount == 0) {
			if (! changedFolders.isEmpty() || ! changedResources.isEmpty()) {
				// write sync info to disk
				Iterator it = changedFolders.iterator();
				while (it.hasNext()) {
					IContainer folder = (IContainer) it.next();
					FolderSyncInfo info = getCachedFolderSync(folder);
					if (info != null) {
						writeFolderConfig(folder, info);
					} else {
						deleteFolderConfig(folder);
					}
				}
				it = changedResources.iterator();
				while (it.hasNext()) {
					IResource resource = (IResource) it.next();
					ResourceSyncInfo info = getCachedResourceSync(resource);
					if (info != null) {
						writeResourceSync(resource, info);
					} else {
						deleteResourceSync(resource);
					}
				}
				
				// broadcast events
				changedResources.addAll(changedFolders);
				changedFolders.clear();
				IResource[] resources = (IResource[]) changedResources.toArray(
					new IResource[changedResources.size()]);
				TeamPlugin.getManager().broadcastResourceStateChanges(resources);
				changedResources.clear();
			}
		}
	}
	
	private ISynchronizer getSynchronizer() {
		return ResourcesPlugin.getWorkspace().getSynchronizer();
	}
	
	private ResourceSyncInfo getCachedResourceSync(IResource resource) throws CVSException {
		try {
			byte[] bytes = getSynchronizer().getSyncInfo(RESOURCE_SYNC_KEY, resource);
			if(bytes==null) {
				return null;
			}
			return new ResourceSyncInfo(new String(bytes), null, null);
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	private void setCachedResourceSync(IResource resource, ResourceSyncInfo info) throws CVSException {
		try {
			if(info==null) {
				getSynchronizer().flushSyncInfo(RESOURCE_SYNC_KEY, resource, IResource.DEPTH_ZERO);
			} else {
				getSynchronizer().setSyncInfo(RESOURCE_SYNC_KEY, resource, info.getEntryLine(true).getBytes());
			}
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	private FolderSyncInfo getCachedFolderSync(IContainer folder) throws CVSException {
		try {
			byte[] bytes = getSynchronizer().getSyncInfo(FOLDER_SYNC_KEY, folder);
			if(bytes==null) {
				return null;
			}
			DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
			String repo = is.readUTF();
			String root = is.readUTF();
			String tag = is.readUTF();
			CVSTag cvsTag = null;
			boolean isStatic = is.readBoolean();
			if(!tag.equals("null")) {
				cvsTag = new CVSEntryLineTag(tag);
			}
			return new FolderSyncInfo(repo, root, cvsTag, isStatic);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		} catch(IOException e) {
			throw CVSException.wrapException(e);
		}	
	}
	
	private void setCachedFolderSync(IContainer folder, FolderSyncInfo info) throws CVSException {
		try {
			if(info==null) {
				getSynchronizer().flushSyncInfo(FOLDER_SYNC_KEY, folder, IResource.DEPTH_INFINITE);
			} else {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				DataOutputStream os = new DataOutputStream(bos);
				os.writeUTF(info.getRepository());
				os.writeUTF(info.getRoot());
				CVSEntryLineTag tag = info.getTag();
				if(tag==null) {
					os.writeUTF("null");
				} else {
					os.writeUTF(info.getTag().toEntryLineFormat(false));
				}
				os.writeBoolean(info.getIsStatic());				
				getSynchronizer().setSyncInfo(FOLDER_SYNC_KEY, folder, bos.toByteArray());
				os.close();
			}
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		} catch(IOException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	private FolderSyncInfo readFolderConfig(IContainer folder) throws CVSException {
		//return SyncFileUtil.readFolderConfig(folder.getLocation().toFile());
		return null;
	}
	
	private void writeFolderConfig(IContainer folder, FolderSyncInfo info) throws CVSException {
		/*
		SyncFileUtil.writeFolderConfig(folder.getLocation().toFile(), info);
		try { 
			folder.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
		*/
	}
	
	private void deleteFolderConfig(IContainer folder) throws CVSException {
		//SyncFileUtil.deleteSync(folder.getLocation().toFile());
	}
	
	private ResourceSyncInfo[] readEntriesFile(IContainer folder) throws CVSException {
		//return SyncFileUtil.readEntriesFile(folder.getLocation().toFile());
		return null;
	}
	
	private void writeResourceSync(IResource resource, ResourceSyncInfo info) throws CVSException {
		/*
		SyncFileUtil.writeResourceSync(resource.getLocation().toFile(), info);
		try { 
			resource.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}*/
	}
	
	private void deleteResourceSync(IResource resource) throws CVSException {
		/*
		SyncFileUtil.deleteSync(resource.getLocation().toFile());
		try { 
			resource.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
		*/
	}
	
	public static EclipseSynchronizer getInstance() {
		if (instance == null) {
			instance = new EclipseSynchronizer();			
		}
		return instance;
	}
}

Back to the top