Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 565f8704b33c95474b0347cdf9f659220939b9cc (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
/*******************************************************************************
 * 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.variants;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;

/**
 * A resource variant tree that caches and obtains its bytes from the remote slot
 * in a three-way synchronizer. Clients must subclass to provide remote resource
 * variant refresh functionality.
 *
 * @see ThreeWaySubscriber
 *
 * @since 3.0
 */
public abstract class ThreeWayRemoteTree extends ResourceVariantTree {

	private ThreeWaySubscriber subscriber;

	/*
	 * A resource variant byte store that accesses the remote bytes
	 * from a three-way synchronizer. Both access and modification
	 * are supported.
	 */
	static class RemoteResourceVariantByteStore extends ResourceVariantByteStore {
		private ThreeWaySynchronizer synchronizer;
		public RemoteResourceVariantByteStore(ThreeWaySynchronizer synchronizer) {
			this.synchronizer = synchronizer;
		}
		@Override
		public void dispose() {
			// Nothing to do as contents are owned by the TargetSynchronizer
		}
		@Override
		public byte[] getBytes(IResource resource) throws TeamException {
			return getSynchronizer().getRemoteBytes(resource);
		}
		@Override
		public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
			return getSynchronizer().setRemoteBytes(resource, bytes);
		}
		@Override
		public boolean flushBytes(IResource resource, int depth) throws TeamException {
			// This method is invoked when the remote bytes are stale and should be removed
			// This is handled by the ThreeWaySynchronizer so nothing needs to be done here.
			return false;
		}
		public boolean isVariantKnown(IResource resource) throws TeamException {
			return getSynchronizer().hasSyncBytes(resource);
		}
		@Override
		public boolean deleteBytes(IResource resource) throws TeamException {
			return getSynchronizer().removeRemoteBytes(resource);
		}
		@Override
		public IResource[] members(IResource resource) throws TeamException {
			return synchronizer.members(resource);
		}
		private ThreeWaySynchronizer getSynchronizer() {
			return synchronizer;
		}
	}

	/**
	 * Create a remote resource variant tree that stores and obtains
	 * it's bytes from the remote slot of the synchronizer of the
	 * given subscriber
	 * @param subscriber a three-way subscriber
	 */
	public ThreeWayRemoteTree(ThreeWaySubscriber subscriber) {
		super(new RemoteResourceVariantByteStore(subscriber.getSynchronizer()));
		this.subscriber = subscriber;
	}

	@Override
	public IResource[] roots() {
		return getSubscriber().roots();
	}

	@Override
	public IResourceVariant getResourceVariant(IResource resource) throws TeamException {
		return getSubscriber().getResourceVariant(resource, getByteStore().getBytes(resource));
	}

	/**
	 * Return the subscriber associated with this resource variant tree.
	 * @return the subscriber associated with this resource variant tree
	 */
	protected ThreeWaySubscriber getSubscriber() {
		return subscriber;
	}

	@Override
	protected IResource[] collectChanges(final IResource local,
			final IResourceVariant remote, final int depth, IProgressMonitor monitor)
			throws TeamException {
		final IResource[][] resources = new IResource[][] { null };
		getSubscriber().getSynchronizer().run(local, new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				resources[0] = ThreeWayRemoteTree.super.collectChanges(local, remote, depth, monitor);
			}
		}, monitor);
		return resources[0];
	}
}

Back to the top