Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3f3d3b8c919dfc76a921165ee432ae19e2abf6c (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
/*******************************************************************************
 * Copyright (c) 2009 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eduard Bartsch (SAP AG) - initial API and implementation
 *    Mathias Kinzler (SAP AG) - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.resources.semantic;

import java.net.URI;
import java.util.Map;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.internal.resources.semantic.team.DelegatingResourceRuleFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.semantic.ISemanticFileSystem;
import org.eclipse.core.resources.semantic.ISemanticResource;
import org.eclipse.core.resources.semantic.ISemanticResourceInfo;
import org.eclipse.core.resources.semantic.SemanticResourceException;
import org.eclipse.core.resources.semantic.SemanticResourceStatusCode;
import org.eclipse.core.resources.semantic.SyncDirection;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.RepositoryProvider;

/**
 * The {@link ISemanticResource} base implementation.
 * 
 */
public abstract class SemanticResourceAdapterImpl implements ISemanticResource {

	/**
	 * Used for rule checking
	 * 
	 */
	protected enum RuleType {
		CREATE, DELETE, MODIFY, VALIDATE_EDIT, REFRESH
	}

	private final IResource resource;
	private final ISemanticFileSystem fs;

	SemanticResourceAdapterImpl(IResource resource, ISemanticFileSystem fileSystem) {
		this.resource = resource;
		this.fs = fileSystem;
	}

	public IResource getAdaptedResource() {
		return this.resource;
	}

	/**
	 * 
	 * @return store instance
	 * @throws CoreException
	 *             if store can not be determined
	 */
	protected ISemanticFileStoreInternal getOwnStore() throws CoreException {
		// TODO trace
		URI uri = this.resource.getLocationURI();

		try {
			IFileStore store = EFS.getStore(uri);

			if (store instanceof ISemanticFileStoreInternal) {
				return (ISemanticFileStoreInternal) store;
			} else if (store.getFileSystem() == EFS.getNullFileSystem()) {
				throw new SemanticResourceException(SemanticResourceStatusCode.STORE_NOT_FOUND, this.resource.getFullPath(),
						Messages.SemanticResourceAdapterImpl_NullFile_XMSG);
			} else {
				// quite unlikely (the URI was changed on the resource, e.g. on
				// a project or symbolic link)
				throw new SemanticResourceException(SemanticResourceStatusCode.STORE_NOT_FOUND, this.resource.getFullPath(), NLS.bind(
						Messages.SemanticResourceAdapterImpl_NoSemanticStore_XMSG, uri.toString()));
			}
		} catch (CoreException e) {
			throw new SemanticResourceException(SemanticResourceStatusCode.STORE_NOT_FOUND, this.resource.getFullPath(), null, e);
		}
	}

	protected ISchedulingRule checkCurrentRule(RuleType ruleType) throws CoreException {
		Job job = Job.getJobManager().currentJob();

		if (job != null) {

			ISchedulingRule checkRule = getRuleForType(ruleType, this.resource);

			ISchedulingRule rule = job.getRule();
			if (rule != null) {

				if (!rule.contains(checkRule)) {
					throw new SemanticResourceException(SemanticResourceStatusCode.LOCK_CONFLICT, this.resource.getFullPath(),
							Messages.SemanticResourceAdapterImpl_OperationNotCoveredByRule_XMSG);
				}
			} else {
				// TODO 0.1: we need to investigate again what to do when rule
				// is null
				if (SfsTraceLocation.CORE.isActive()) {
					SfsTraceLocation.getTrace().trace(SfsTraceLocation.CORE.getLocation(),
							Messages.SemanticResourceAdapterImpl_JobNoRule_XMSG);
					SfsTraceLocation.getTrace().traceDumpStack(SfsTraceLocation.CORE.getLocation());
				}

			}
			return checkRule;

		}

		throw new SemanticResourceException(SemanticResourceStatusCode.CALLED_OUTSIDE_OF_SCHEDULING_RULE, this.resource.getFullPath(),
				Messages.SemanticResourceAdapterImpl_CalledOutsideRule_XMSG);

	}

	ISchedulingRule getRuleForType(RuleType ruleType, IResource actResource) throws SemanticResourceException {

		if (actResource instanceof IWorkspaceRoot) {
			return actResource;
		}
		// TODO 0.1: check life cycle issues with project close/delete
		IProject project = actResource.getProject();
		if (!project.isAccessible()) {
			// project closed or deleted
			throw new SemanticResourceException(SemanticResourceStatusCode.PROJECT_NOT_ACCESSIBLE, actResource.getFullPath(), NLS.bind(
					Messages.SemanticResourceAdapterImpl_ProjectNotAccessible_XMSG, project.getName()));
		}

		IResourceRuleFactory rf;

		if (actResource.isLinked()) {
			// fallback to default factory
			rf = actResource.getWorkspace().getRuleFactory();
		} else {
			// we obtain the "expected" rule from the content provider
			RepositoryProvider provider = RepositoryProvider.getProvider(project, ISemanticFileSystem.SFS_REPOSITORY_PROVIDER);
			if (provider != null) {
				rf = provider.getRuleFactory();
			} else {
				if (actResource.getType() == IResource.PROJECT) {
					rf = actResource.getWorkspace().getRuleFactory();
				} else {
					rf = new DelegatingResourceRuleFactory(this.fs);
				}
			}
		}

		ISchedulingRule checkRule;

		switch (ruleType) {
			case DELETE :
				checkRule = rf.deleteRule(actResource);
				break;
			case CREATE :
				checkRule = rf.createRule(actResource);
				break;
			case MODIFY :
				checkRule = rf.modifyRule(actResource);
				break;
			case REFRESH :
				checkRule = rf.refreshRule(actResource);
				break;
			case VALIDATE_EDIT :
				checkRule = rf.validateEditRule(new IResource[] {actResource});
				break;
			default :
				throw new RuntimeException(Messages.SemanticResourceAdapterImpl_UnknownRuleType_XMSG);
		}
		return checkRule;
	}

	protected void refreshLocalIfNeeded(RuleType ruleType, ISchedulingRule rule, int options, IProgressMonitor monitor)
			throws CoreException {
		if ((options & ISemanticFileSystem.SUPPRESS_REFRESH) == 0) {

			if (rule instanceof IResource) {
				if (SfsTraceLocation.CORE.isActive()) {
					SfsTraceLocation.getTrace().traceEntry(SfsTraceLocation.CORE.getLocation());
				}
				((IResource) rule).refreshLocal(IResource.DEPTH_INFINITE, monitor);
				return;
			}

			throw new SemanticResourceException(SemanticResourceStatusCode.AUTO_REFRESH, new Path(""), NLS.bind( //$NON-NLS-1$
					Messages.SemanticResourceAdapterImpl_RuleNoResource_XMSG, ruleType.name()));
		}
	}

	public void deleteRemotely(int options, IProgressMonitor monitor) throws CoreException {

		checkCurrentRule(RuleType.DELETE);

		ISemanticFileStoreInternal store = getOwnStore();

		store.deleteRemotely(monitor);

		refreshLocalIfNeeded(RuleType.DELETE, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);

	}

	public void remove(int options, IProgressMonitor monitor) throws CoreException {

		checkCurrentRule(RuleType.DELETE);

		ISemanticFileStoreInternal store = getOwnStore();

		if ((options & ISemanticFileSystem.FORCE_REMOVE) != 0) {
			store.forceRemoveFromWorkspace(options, monitor);
		} else {
			store.removeFromWorkspace(monitor);
		}
		refreshLocalIfNeeded(RuleType.DELETE, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);

	}

	public IStatus validateRemoteDelete(Object shell) {
		try {
			ISemanticFileStoreInternal store = getOwnStore();

			return store.validateRemoteDelete(shell);
		} catch (CoreException e) {
			if (SfsTraceLocation.CORE.isActive()) {
				SfsTraceLocation.getTrace().trace(SfsTraceLocation.CORE.getLocation(), e.getMessage(), e);
			}
			return e.getStatus();
		}
	}

	public IStatus validateRemove(int options, IProgressMonitor monitor) {
		try {
			ISemanticFileStoreInternal store = getOwnStore();

			return store.validateRemove(options, monitor);
		} catch (CoreException e) {
			if (SfsTraceLocation.CORE.isActive()) {
				SfsTraceLocation.getTrace().trace(SfsTraceLocation.CORE.getLocation(), e.getMessage(), e);
			}
			return e.getStatus();
		}
	}

	public void synchronizeContentWithRemote(SyncDirection direction, int options, IProgressMonitor monitor) throws CoreException {
		checkCurrentRule(RuleType.REFRESH);

		ISemanticFileStoreInternal store = getOwnStore();

		store.synchronizeContentWithRemote(direction, monitor);

		refreshLocalIfNeeded(RuleType.MODIFY, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);
	}

	public ISemanticResourceInfo fetchResourceInfo(int options, IProgressMonitor monitor) throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.fetchResourceInfo(options, monitor);
	}

	public String getContentProviderID() throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.getContentProviderID();
	}

	public IStatus lockResource(int options, IProgressMonitor monitor) throws CoreException {
		checkCurrentRule(RuleType.REFRESH);
		ISemanticFileStoreInternal store = getOwnStore();

		IStatus result = store.lockResource(monitor);

		if (result.isOK()) {
			refreshLocalIfNeeded(RuleType.MODIFY, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);
		}

		return result;
	}

	public IStatus unlockResource(int options, IProgressMonitor monitor) throws CoreException {
		checkCurrentRule(RuleType.REFRESH);
		ISemanticFileStoreInternal store = getOwnStore();

		IStatus result = store.unlockResource(monitor);

		if (result.isOK()) {
			refreshLocalIfNeeded(RuleType.MODIFY, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);
		}

		return result;
	}

	public Map<QualifiedName, String> getPersistentProperties() throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.getPersistentProperties();
	}

	public String getPersistentProperty(QualifiedName key) throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.getPersistentProperty(key);
	}

	public Map<QualifiedName, Object> getSessionProperties() throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.getSessionProperties();
	}

	public Object getSessionProperty(QualifiedName key) throws CoreException {
		ISemanticFileStoreInternal store = getOwnStore();

		return store.getSessionProperty(key);
	}

	public void setPersistentProperty(QualifiedName key, String value) throws CoreException {
		checkCurrentRule(RuleType.MODIFY);
		ISemanticFileStoreInternal store = getOwnStore();

		store.setPersistentProperty(key, value);

	}

	public void setSessionProperty(QualifiedName key, Object value) throws CoreException {
		checkCurrentRule(RuleType.MODIFY);
		ISemanticFileStoreInternal store = getOwnStore();

		store.setSessionProperty(key, value);

	}

	public void setRemoteURI(URI uri, int options, IProgressMonitor monitor) throws CoreException {
		checkCurrentRule(RuleType.REFRESH);

		ISemanticFileStoreInternal store = getOwnStore();

		store.setRemoteURI(uri, monitor);

		refreshLocalIfNeeded(RuleType.MODIFY, getRuleForType(RuleType.MODIFY, this.resource), options, monitor);
	}

}

Back to the top