Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0ac94516494228b82b30944e76501c01f0595718 (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
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.core.subscribers;

import java.util.*;

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.RemoteResourceMappingContext;
import org.eclipse.core.resources.mapping.ResourceTraversal;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.core.*;

/**
 * A resource mapping context that provides the client access to the remote state
 * of local resources using a subscriber. It uses a <code>SyncInfoFilter</code>
 * to determine whether the local contents differ from the remote contents.
 * This allows the context to be used for different operations (check-in,
 * update and replace).
 * @since 3.2
 */
public class SubscriberResourceMappingContext extends RemoteResourceMappingContext {

    private final Subscriber subscriber;

    // Lists used to keep track of resources that have been refreshed
    private Set<IResource> shallowRefresh = new HashSet<>();
    private Set<IResource> deepRefresh = new HashSet<>();
    private boolean autoRefresh;

    /**
     * Return a resource mapping context suitable for comparison operations.
     * Comparisons require that any out-of-sync resources have contents
     * that differ.
     * @param subscriber the subscriber
     * @return a resource mapping context suitable for compare operations
     */
    public static RemoteResourceMappingContext createContext(Subscriber subscriber) {
        return new SubscriberResourceMappingContext(subscriber, true);
    }

    /**
     * Create a resource mapping context for the given subscriber
     * @param subscriber the subscriber
     * from the local contents
     * @param autoRefresh whether the context should auto-refresh when queried
     */
    public SubscriberResourceMappingContext(Subscriber subscriber, boolean autoRefresh) {
        this.subscriber = subscriber;
        this.autoRefresh = autoRefresh;
    }

    @Override
	public final boolean hasRemoteChange(IResource resource, IProgressMonitor monitor) throws CoreException {
    	try {
			monitor.beginTask(null, 100);
			ensureRefreshed(resource, IResource.DEPTH_ONE, NONE, monitor);
			SyncInfo syncInfo = subscriber.getSyncInfo(resource);
			validateRemote(resource, syncInfo);
	    	if (syncInfo == null) return false;
	    	int direction = SyncInfo.getDirection(syncInfo.getKind());
			return direction == SyncInfo.INCOMING || direction == SyncInfo.CONFLICTING;
		} finally {
			monitor.done();
		}
    }

    @Override
	public boolean hasLocalChange(IResource resource, IProgressMonitor monitor) throws CoreException {
    	SyncInfo syncInfo = subscriber.getSyncInfo(resource);
    	if (syncInfo == null) return false;
    	int direction = SyncInfo.getDirection(syncInfo.getKind());
		return direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING;
    }

    @Override
	public final IStorage fetchRemoteContents(IFile file, IProgressMonitor monitor) throws CoreException {
    	try {
			monitor.beginTask(null, 100);
	    	ensureRefreshed(file, IResource.DEPTH_ZERO, FILE_CONTENTS_REQUIRED, Policy.subMonitorFor(monitor, 10));
	        SyncInfo syncInfo = subscriber.getSyncInfo(file);
	        IResourceVariant remote = validateRemote(file, syncInfo);
	        if (remote == null) {
	            return null;
	        }
	        return remote.getStorage(Policy.subMonitorFor(monitor, 90));
		} finally {
			monitor.done();
		}
    }

    @Override
	public final IStorage fetchBaseContents(IFile file, IProgressMonitor monitor) throws CoreException {
    	try {
			monitor.beginTask(null, 100);
	    	ensureRefreshed(file, IResource.DEPTH_ZERO, FILE_CONTENTS_REQUIRED, Policy.subMonitorFor(monitor, 10));
	        SyncInfo syncInfo = subscriber.getSyncInfo(file);
	        IResourceVariant base = validateBase(file, syncInfo);
	        if (base == null) {
	            return null;
	        }
	        return base.getStorage(Policy.subMonitorFor(monitor, 90));
		} finally {
			monitor.done();
		}
    }

    @Override
	public final IResource[] fetchMembers(IContainer container, IProgressMonitor monitor) throws CoreException {
    	try {
			monitor.beginTask(null, 100);
	    	ensureRefreshed(container, IResource.DEPTH_ONE, NONE, Policy.subMonitorFor(monitor, 100));
	        SyncInfo syncInfo = subscriber.getSyncInfo(container);
	        if (validateRemote(container, syncInfo) == null) {
	            // There is no remote so return an empty array
	            return new IResource[0];
	        }
	        return subscriber.members(container);
		} finally {
			monitor.done();
		}
    }

    @Override
	public final void refresh(ResourceTraversal[] traversals, int flags, IProgressMonitor monitor) throws CoreException {
    	subscriber.refresh(traversals, monitor);
        for (int i = 0; i < traversals.length; i++) {
			ResourceTraversal traversal = traversals[i];
			refreshed(traversal.getResources(), traversal.getDepth());
		}
    }

    /**
     * Refresh the subscriber and cache the fact that the resources were refreshed by
     * calling the <code>refreshed</code> method. The default implementation only refreshes
     * the state and does not fetch contents in the <code>FILE_CONTENTS_REQUIRED</code>
     * flag is passed. It is up to subclass to handle this.
     * @param resources the resources to be refreshed
     * @param depth the depth of the refresh
     * @param flags the flags that indicate extra state that should be fetched
     * @param monitor a progress monitor
     * @throws TeamException
     */
	protected void refresh(IResource[] resources, int depth, int flags, IProgressMonitor monitor) throws TeamException {
		subscriber.refresh(resources, depth, monitor);
		refreshed(resources, depth);
	}

	/**
	 * Record the fact that the resources have been refreshed to the given depth.
	 * This is done so that accesses to refreshed resources will not need to perform
	 * another refresh.
	 * @param resources the resources that were refreshed
	 * @param depth the depth to which the resources were refreshed
	 */
	protected final void refreshed(IResource[] resources, int depth) {
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			// Include files and depth-one folders as shallow
			if (depth == IResource.DEPTH_ONE || resource.getType() == IResource.FILE) {
				shallowRefresh.add(resource);
			} else if (depth == IResource.DEPTH_INFINITE) {
				deepRefresh.add(resource);
			}
		}
	}

    /*
     * Ensure that the given resource has been refreshed to the specified depth
     * since the context has been created.
     */
    private void ensureRefreshed(IResource resource, int depth, int flags, IProgressMonitor monitor) throws TeamException {
        if (autoRefresh) {
    		if (depth == IResource.DEPTH_INFINITE) {
    			// If the resource or a parent was refreshed deeply, no need to do it again
    			if (wasRefreshedDeeply(resource))
    				return;
    			// if the resource is a file, a shallow refresh is enough
    			if (resource.getType() == IResource.FILE && wasRefreshedShallow(resource))
    				return;
    		} else {
    			if (wasRefreshedShallow(resource))
    				return;
    		}
    		refresh(new IResource[] { resource }, depth, flags, monitor);
        }
	}

    /*
     * Look for a shallow refresh of the resource. If not there,
     * look fir a deep refresh of a parent or a shallow refresh of the
     * direct parent if the resource is a file.
     */
	private boolean wasRefreshedShallow(IResource resource) {
		if  (shallowRefresh.contains(resource))
			return true;
		if (resource.getType() == IResource.FILE && shallowRefresh.contains(resource.getParent()))
			return true;
		if (wasRefreshedDeeply(resource))
			return true;
		return false;
	}

	/*
	 * Look for a deep refresh of the resource or any of it's parents
	 */
	private boolean wasRefreshedDeeply(IResource resource) {
		if (resource.getType() == IResource.ROOT)
			return false;
		if (deepRefresh.contains(resource))
			return true;
		return wasRefreshedDeeply(resource.getParent());
	}

	/*
	 * Validate that the remote resource is of the proper type and return the
	 * remote resource if it is OK. A return of null indicates that there is no remote.
	 */
    private IResourceVariant validateRemote(IResource resource, SyncInfo syncInfo) throws CoreException {
        if (syncInfo == null) return null;
        IResourceVariant remote = syncInfo.getRemote();
        if (remote == null) return null;
        return validateRemote(resource, remote);
    }

	private IResourceVariant validateRemote(IResource resource, IResourceVariant remote) throws CoreException {
		boolean containerExpected = resource.getType() != IResource.FILE;
        if (remote.isContainer() && !containerExpected) {
            throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_0 + resource.getFullPath().toString(), null));
        } else if (!remote.isContainer() && containerExpected) {
            throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_1 + resource.getFullPath().toString(), null));
        }
        return remote;
	}

	/*
	 * Validate that the base resource is of the proper type and return the
	 * base resource if it is OK. A return of null indicates that there is no base.
	 */
    private IResourceVariant validateBase(IResource resource, SyncInfo syncInfo) throws CoreException {
        if (syncInfo == null) return null;
        IResourceVariant base = syncInfo.getBase();
        if (base == null) return null;
        return validateRemote(resource, base);
    }

    /**
     * Set whether the context should refresh the state of resources
     * when their state is requested. The context keeps track of what
     * resources were refreshed and only auto-refreshes a resource
     * once.
     * @param autoRefresh whether the context should refresh the state of resources
     * when their state is requested
     */
    public void setAutoRefresh(boolean autoRefresh) {
        this.autoRefresh = autoRefresh;
    }

	@Override
	public boolean isThreeWay() {
		return subscriber.getResourceComparator().isThreeWay();
	}

	public boolean contentDiffers(IFile file, IProgressMonitor monitor) throws CoreException {
		return hasRemoteChange(file, monitor) || hasLocalChange(file, monitor);
	}

	@Override
	public IProject[] getProjects() {
		Set<IProject> projects = new HashSet<>();
		IResource[] roots = subscriber.roots();
		for (int i = 0; i < roots.length; i++) {
			IResource resource = roots[i];
			projects.add(resource.getProject());
		}
		return projects.toArray(new IProject[projects.size()]);
	}
}

Back to the top