Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bfc23c63f3e7f7794fb0e706a802f773f3a81646 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.core.resource;

import java.io.IOException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewInvalidationEvent;
import org.eclipse.emf.cdo.view.CDOViewSet;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.papyrus.cdo.core.IPapyrusRepository;
import org.eclipse.papyrus.cdo.core.IPapyrusRepositoryManager;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.core.controlmode.CDOProxyManager;
import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
import org.eclipse.papyrus.infra.services.resourceloading.OnDemandLoadingModelSet;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;

/**
 * This is the CDOAwareModelSet type. Enjoy.
 */
public class CDOAwareModelSet extends OnDemandLoadingModelSet {

	private static final Set<CDOState> DIRTY_STATES = EnumSet.of(CDOState.NEW, CDOState.DIRTY, CDOState.CONFLICT, CDOState.INVALID_CONFLICT);

	private final ThreadLocal<Boolean> inGetResource = new ThreadLocal<Boolean>();

	private final CDOProxyManager proxyManager = new CDOProxyManager(this);

	private final IPapyrusRepositoryManager repositoryManager;

	private IPapyrusRepository repository;

	private IListener invalidationListener;

	public CDOAwareModelSet(IPapyrusRepositoryManager repositoryManager) {
		super();

		this.resources = new SafeResourceList();

		setTrackingModification(false);

		this.repositoryManager = repositoryManager;
	}

	@Override
	public EObject getEObject(URI uri, boolean loadOnDemand) {
		return CDOUtils.isCDOURI(uri) ? getCDOObject(uri, loadOnDemand) : super.getEObject(uri, loadOnDemand);
	}

	protected EObject getCDOObject(URI uri, boolean loadOnDemand) {
		EObject result = null;

		if(CDOProxyManager.isCDOProxyURI(uri)) {
			// get a real proxy object, out of thin air, to give CDO the non-null
			// instance that it needs (otherwise the 'non-null constraint' of
			// all kinds of reference lists will be violated)
			result = proxyManager.getProxy(uri);
		} else {
			result = super.getEObject(uri, loadOnDemand);
		}

		return result;
	}

	@Override
	public Resource getResource(URI uri, boolean loadOnDemand) {
		Boolean oldValue = inGetResource.get();
		inGetResource.set(Boolean.TRUE);

		try {
			return super.getResource(uri, loadOnDemand);
		} finally {
			inGetResource.set(oldValue);
		}
	}

	boolean isInGetResource() {
		return inGetResource.get() == Boolean.TRUE;
	}

	@Override
	public Resource createResource(URI uri, String contentType) {
		initTransaction(uri);
		return super.createResource(uri, contentType);
	}

	@Override
	protected void demandLoad(Resource resource) throws IOException {

		if(CDOUtils.isCDOURI(resource.getURI())) {
			// XML options not applicable to CDO resources
			resource.load(null);

			resourceLoadedHook(resource);
		} else {
			super.demandLoad(resource);
		}
	}

	protected void resourceLoadedHook(Resource resource) {
		for(Diagram next : Iterables.filter(resource.getContents(), Diagram.class)) {

			DawnDiagramUpdater.initializeElement(next);
		}
	}

	public CDOView getCDOView() {
		CDOViewSet viewSet = CDOUtil.getViewSet(this);
		CDOView[] views = (viewSet == null) ? null : viewSet.getViews();

		return ((views != null) && (views.length > 0)) ? views[0] : null;
	}

	@Override
	public void createModels(URI newURI) {
		initTransaction(newURI);
		super.createModels(newURI);
	}

	@Override
	public void loadModels(URI uri) throws ModelMultiException {

		initTransaction(uri);
		super.loadModels(uri);
	}

	protected void initTransaction(URI uri) {
		if(getCDOView() == null) {
			// get the repository and start a transaction on it

			if(repository == null) {
				repository = repositoryManager.getRepositoryForURI(uri);
			}

			if(repository != null) {
				repository.createTransaction(this);
				CDOView view = getCDOView();
				if(view != null) {
					view.addListener(getInvalidationListener());
				}
			}
		}
	}

	@Override
	public void unload() {
		try {
			super.unload();
		} finally {
		if((repository != null) && (getCDOView() != null)) {
			CDOView view = getCDOView();
			if(view != null) {
				view.removeListener(getInvalidationListener());
			}
			invalidationListener = null;

			// dispose the transaction
			repository.close(this);

				// now, we can remove the CDOViewSet adapter
				eAdapters().clear();
		}

		repository = null;
		}
	}

	protected final IListener getInvalidationListener() {
		if(invalidationListener == null) {
			invalidationListener = createInvalidationListener();
		}
		return invalidationListener;
	}

	protected IListener createInvalidationListener() {
		return new IListener() {

			public void notifyEvent(IEvent event) {
				if(event instanceof CDOViewInvalidationEvent) {
					TransactionalEditingDomain domain = getTransactionalEditingDomain();
					if(domain instanceof CDOAwareTransactionalEditingDomain) {
						((CDOAwareTransactionalEditingDomain)domain).fireResourceSetChanged((CDOViewInvalidationEvent)event);
					}
				}
			}
		};
	}

	@Override
	public boolean isUserModelResource(URI uri) {
		return super.isUserModelResource(uri) || CDOUtils.isCDOURI(uri);
	}

	@Override
	public EList<Adapter> eAdapters() {
		if(eAdapters == null) {
			eAdapters = new EAdapterList<Adapter>(this) {

				private static final long serialVersionUID = 1L;

				@Override
				public Adapter remove(int index) {
					Adapter toRemove = primitiveGet(index);
					if((toRemove instanceof CDOViewSet) && !canDisconnectCDOViewSet()) {
						// don't allow its removal if my view is still open!
						// (Papyrus attempts to clear the resource set's adapters when disposing a ModelSet)
						return null;
					}

					return super.remove(index);
				}

				@Override
				public void clear() {
					if(!canDisconnectCDOViewSet()) {
						// we can remove everything but the view-set adapter
						Adapter viewSetAdapter = getViewSetAdapter();
						if(viewSetAdapter != null) {
							retainAll(Collections.singleton(viewSetAdapter));
						} else {
							super.clear();
						}
					} else {
						super.clear();
					}
				}

				private Adapter getViewSetAdapter() {
					return Iterables.find(this, Predicates.instanceOf(CDOViewSet.class), null);
				}
			};
		}

		return eAdapters;
	}

	private boolean canDisconnectCDOViewSet() {
		CDOView view = getCDOView();
		return ((view == null) || view.isClosed()) && getResources().isEmpty();
	}

	boolean isDirty(Resource resource) {
		return (resource instanceof CDOResource) && DIRTY_STATES.contains(((CDOResource)resource).cdoState());
	}

	@Override
	protected void handleResourcesToDelete() {
		final int initialCount = getResourcesToDeleteOnSave().size();

		super.handleResourcesToDelete();

		// any changes made by resource deletions?
		CDOView view = getCDOView();
		if((view instanceof CDOTransaction) && (getResourcesToDeleteOnSave().size() < initialCount)) {
			try {
				((CDOTransaction)view).commit();
			} catch (CommitException e) {
				Activator.log.error("Failed to commit resource deletions.", e);
			}
		}
	}

	@Override
	protected boolean validateDeleteResource(URI uri) {
		boolean result = true;

		Resource resource = getResource(uri, false);

		// if it was meant to be deleted, an attempt would have been made to remove it.  That may
		// have been prevented if it was dirty.  If it isn't now, then delete it.  Otherwise,
		// block again
		if((resource != null) && isDirty(resource)) {
			result = false;
			Activator.log.warn("Attempt to delete a dirty CDO resource: " + uri);
		}

		return result;
	}

	@Override
	protected boolean deleteResource(URI uri) {
		Resource res = getResource(uri, false);
		boolean result = res instanceof CDOResource;

		if(!result) {
			// not a CDO resource.  Default behaviour
			result = super.deleteResource(uri);
		} else {
			try {
				res.delete(null);
				if(res.getResourceSet() != null) {
					res.unload();
					res.getResourceSet().getResources().remove(res);
				}
			} catch (IOException e) {
				Activator.log.error(e);
			}
		}

		return result;
	}

	//
	// Nested types
	//

	/**
	 * CDO doesn't permit resources to be removed from the resource set if they are {@linkplain CDOState#DIRTY dirty}, so this specialized list
	 * prevents that.
	 */
	private class SafeResourceList extends ResourcesEList<Resource> {

		private static final long serialVersionUID = 1L;

		@Override
		public boolean remove(Object object) {
			boolean result = !(object instanceof CDOResource) || !isDirty((CDOResource)object);

			if(result) {
				result = super.remove(object);
			}

			return result;
		}
	}
}

Back to the top