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

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.xerces.parsers.SAXParser;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This class handles the updating of the .vcm_meta file in projects managed by the CVSTeamProvider.
 * 
 * It does so by listening to deltas on the project description and the .vcm_meta file itself.
 * 
 */
public class ProjectDescriptionManager implements IResourceChangeListener {

	public final static IPath PROJECT_DESCRIPTION_PATH = new Path(".vcm_meta"); //$NON-NLS-1$
	public final static IPath CORE_PROJECT_DESCRIPTION_PATH = new Path(".project"); //$NON-NLS-1$
	public final static boolean UPDATE_PROJECT_DESCRIPTION_ON_LOAD = true;

	public static final String VCMMETA_MARKER = "org.eclipse.team.cvs.core.vcmmeta";  //$NON-NLS-1$
	
	/*
	 * Read the project meta file into the provider description
	 */
	public static void readProjectDescription(IProjectDescription desc, InputStream stream) throws IOException, CVSException {
		SAXParser parser = new SAXParser();
		parser.setContentHandler(new ProjectDescriptionContentHandler(desc));
		try {
			parser.parse(new InputSource(stream));
		} catch (SAXException ex) {
			throw new CVSException(IStatus.ERROR, IStatus.ERROR, Policy.bind("ProjectDescriptionManager.unableToReadDescription"), ex); //$NON-NLS-1$
		}
	}

	public static void writeProjectDescription(IProject project, IProgressMonitor progress) throws CVSException {
			
		// generate the new contents of the project meta file into a string
		ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
		String newContents = null;
		try {
			IProjectDescription desc = project.getDescription();
			ProjectDescriptionWriter.writeProjectDescription(desc, byteOutputStream);
			byteOutputStream.close();
			newContents = byteOutputStream.toString("UTF8"); //$NON-NLS-1$
		} catch (IOException ex) {
			throw CVSException.wrapException(project, Policy.bind("ProjectDescriptionManager.ioDescription"), ex); //$NON-NLS-1$
		} catch (CoreException ex) {
			throw CVSException.wrapException(project, Policy.bind("ProjectDescriptionManager.coreDescription"), ex); //$NON-NLS-1$
		}

		IFile descResource = project.getFile(PROJECT_DESCRIPTION_PATH);
		if (descResource.exists()) {
			// check if the existing contents are the same before rewriting
			String oldContents = null;
			try {
				StringBuffer stringBuffer = new StringBuffer();
				InputStream is = ((IFile) descResource).getContents();
				byte[] buf = new byte[512];
				int result = is.read(buf);
				while (result != -1) {
					stringBuffer.append(new String(buf, 0, result, "UTF8")); //$NON-NLS-1$
					result = is.read(buf);
				}
				oldContents = stringBuffer.toString();
				is.close();
			} catch (IOException ex) {
				throw CVSException.wrapException(project, Policy.bind("ProjectDescriptionManager.ioDescription"), ex); //$NON-NLS-1$
			} catch (CoreException ex) {
				throw CVSException.wrapException(project, Policy.bind("ProjectDescriptionManager.coreDescription"), ex); //$NON-NLS-1$
			}

			if (oldContents.equals(newContents)) {
				// the contents of the new file would be the same as the old
				return;
			}
			try {
				descResource.setContents(
					new ByteArrayInputStream(byteOutputStream.toByteArray()),
					false,
					false,
					progress);
			} catch (CoreException ex) {
				throw CVSException.wrapException(project, Policy.bind("ProjectDescriptionManager.coreDescription"), ex); //$NON-NLS-1$
			}
		} else {
			try {
				descResource.create(
					new ByteArrayInputStream(byteOutputStream.toByteArray()),
					false,
					progress);
			} catch (CoreException ex) {
				throw CVSException.wrapException(descResource, Policy.bind("ProjectDescriptionManager.coreDescription"), ex); //$NON-NLS-1$
			}

		}
	}

	/*
	 * To be called whenever the project description file is believed to have changed by
	 * a load/loadIfIncoming operation.
	 */
	public static void updateProjectIfNecessary(IProject project, IProgressMonitor progress) throws CoreException, CVSException {
		
		IFile descResource = project.getFile(PROJECT_DESCRIPTION_PATH);		
		if (descResource.exists() && UPDATE_PROJECT_DESCRIPTION_ON_LOAD) {
								
			// If a managed .project files exists, don't read the .vcm_meta
			ICVSFile coreDescResource = CVSWorkspaceRoot.getCVSFileFor(project.getFile(CORE_PROJECT_DESCRIPTION_PATH));
			if (coreDescResource.exists() && coreDescResource.isManaged()) {
				createVCMMetaMarker(descResource);
				Util.logError(Policy.bind("ProjectDescriptionManager.vcmmetaIgnored", project.getName()), null); //$NON-NLS-1$
				return;
			} else {
				ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(project);
				if (! folder.isCVSFolder()) {
					createVCMMetaMarker(descResource);
					Util.logError(Policy.bind("ProjectDescriptionManager.vcmmetaIgnored", project.getName()), null); //$NON-NLS-1$
					return;
				}
			}
		
			// update project description file (assuming it has changed)
			IProjectDescription desc = project.getDescription();
			DataInputStream is = null;
			try {
				is = new DataInputStream(((IFile) descResource).getContents());
				try {
					readProjectDescription(desc, is);
				} finally {
					is.close();
				}
				try {
					project.setDescription(desc, IResource.FORCE | IResource.KEEP_HISTORY, progress);
				} catch (CoreException ex) {
					// Failing to set the description is probably due to a missing nature
					// Other natures are still set
					Util.logError(Policy.bind("ProjectDescriptionManager.unableToSetDescription"), ex); //$NON-NLS-1$
				}
				// Make sure we have the cvs nature (the above read may have removed it)
				if (!project.getDescription().hasNature(CVSProviderPlugin.getTypeId())) {
					try {
						// TeamPlugin.addNatureToProject(project, CVSProviderPlugin.getTypeId(), progress);
						
						// Set the nature manually in order to override any .project file
						IProjectDescription description = project.getDescription();
						String[] prevNatures= description.getNatureIds();
						String[] newNatures= new String[prevNatures.length + 1];
						System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
						newNatures[prevNatures.length]= CVSProviderPlugin.getTypeId();
						description.setNatureIds(newNatures);
						project.setDescription(description, IResource.FORCE | IResource.KEEP_HISTORY, progress);
					}  catch (CoreException ex) {
						// Failing to set the provider is probably due to a missing nature.
						// Other natures are still set
						Util.logError(Policy.bind("ProjectDescriptionManager.unableToSetDescription"), ex); //$NON-NLS-1$
					}
				}
				// Mark the .vcm_meta file with a problem marker
				if (project.getFile(CORE_PROJECT_DESCRIPTION_PATH).exists()) {
					createVCMMetaMarker(descResource);
				}
			} catch(TeamException ex) {
				Util.logError(Policy.bind("ProjectDescriptionManager.unableToReadDescription"), ex); //$NON-NLS-1$
				// something went wrong, delete the project description file
				descResource.delete(true, progress);
			} catch (IOException ex) {
				Util.logError(Policy.bind("ProjectDescriptionManager.unableToReadDescription"), ex); //$NON-NLS-1$
				// something went wrong, delete the project description file
				descResource.delete(true, progress);
			}
		}
	}

	/* 
	 * Write out the project description file.
	 * 
	 * For now just do it. It would be nice to detect the proper conditions
	 * 
	 */
	public static void writeProjectDescriptionIfNecessary(CVSTeamProvider provider, IResource resource, IProgressMonitor monitor) throws CVSException {
		if (resource.getType() == IResource.PROJECT || isProjectDescription(resource)) {
			IProject project = resource.getProject();
			if (project.getFile(PROJECT_DESCRIPTION_PATH).exists() /* || ! project.getFile(CORE_PROJECT_DESCRIPTION_PATH).exists() */) {
				writeProjectDescription(project, monitor);
			}
		}
	}

	public static boolean isProjectDescription(IResource resource) {
		return resource.getProjectRelativePath().equals(PROJECT_DESCRIPTION_PATH);
	}

	public void resourceChanged(IResourceChangeEvent event) {
		try {
			IResourceDelta root = event.getDelta();
			IResourceDelta[] projectDeltas = root.getAffectedChildren(IResourceDelta.CHANGED | IResourceDelta.ADDED);
			for (int i = 0; i < projectDeltas.length; i++) {
				IResourceDelta delta = projectDeltas[i];
				IResource resource = delta.getResource();
				if (resource.getType() == IResource.PROJECT) {
					IProject project = (IProject)resource;
					RepositoryProvider provider = RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId());
					if (provider == null) continue;
					// First, check if the .vcm_meta file for the project is in the delta.
					IResourceDelta[] children = delta.getAffectedChildren(IResourceDelta.ADDED);
					boolean inSync = false;
					for (int j = 0; j < children.length; j++) {
						IResourceDelta childDelta = children[j];
						IResource childResource = childDelta.getResource();
						if (isProjectDescription(childResource))
							switch (childDelta.getKind()) {
								case IResourceDelta.REMOVED:
									writeProjectDescriptionIfNecessary((CVSTeamProvider)provider, project, Policy.monitorFor(null));
									inSync = true;
									break;
								case IResourceDelta.CHANGED:
								case IResourceDelta.ADDED:
									updateProjectIfNecessary(project, Policy.monitorFor(null));
									inSync = true;
									break;
							}
					}
					// Check if we didn't do anything above and the project description changed.
					if (! inSync && (delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
						writeProjectDescriptionIfNecessary((CVSTeamProvider)provider, project, Policy.monitorFor(null));
					}
				}
			}	
		} catch (CVSException ex) {
			Util.logError(Policy.bind("ProjectDescriptionManager.cannotUpdateDesc"), ex); //$NON-NLS-1$
		} catch (CoreException ex) {
			Util.logError(Policy.bind("ProjectDescriptionManager.cannotUpdateDesc"), ex); //$NON-NLS-1$
		} 
	}
	
	protected static IMarker createVCMMetaMarker(IResource resource) {
		try {
			IMarker[] markers = resource.findMarkers(VCMMETA_MARKER, false, IResource.DEPTH_ZERO);
   			if (markers.length == 1) {
   				return markers[0];
   			}
			IMarker marker = resource.createMarker(VCMMETA_MARKER);
			marker.setAttribute(IMarker.MESSAGE, Policy.bind("ProjectDescriptionManager.vcmmetaMarker" , resource.getName(), resource.getProject().getName()));  //$NON-NLS-1$
			return marker;
		} catch (CoreException e) {
			Util.logError(Policy.bind("ProjectDescriptionManager.markerError"), e); //$NON-NLS-1$
		}
		return null;
	}
}

Back to the top