Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66c6b355e3c7a940c3d3b531239f34bc6f00aa9b (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
 * Copyright (c) 2003, 2006 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
 *******************************************************************************/
/*
 * Created on Mar 3, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.wst.common.internal.emfworkbench;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jem.util.emf.workbench.EMFWorkbenchContextBase;
import org.eclipse.jem.util.emf.workbench.ISynchronizerExtender;
import org.eclipse.jem.util.emf.workbench.ProjectResourceSet;
import org.eclipse.jem.util.emf.workbench.WorkbenchURIConverter;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.wst.common.internal.emf.resource.CompatibilityXMIResource;
import org.eclipse.wst.common.internal.emf.resource.ReferencedXMIFactoryImpl;
import org.eclipse.wst.common.internal.emf.utilities.DefaultOverridableResourceFactoryRegistry;
import org.eclipse.wst.common.internal.emfworkbench.edit.EditModelRegistry;
import org.eclipse.wst.common.internal.emfworkbench.integration.EditModel;
import org.eclipse.wst.common.internal.emfworkbench.integration.EditModelEvent;
import org.eclipse.wst.common.internal.emfworkbench.integration.ProjectResourceSetEditImpl;

/**
 * @author schacher
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class EMFWorkbenchContext extends EMFWorkbenchContextBase implements ISynchronizerExtender {

	private Map readOnlyModels = new HashMap();
	private Map editableModels = new HashMap();

	protected Adapter resourceSetListener;

	protected boolean defaultToMOF5Compatibility = false;


	/**
	 * @param aProject
	 */
	public EMFWorkbenchContext(IProject aProject) {
		super(aProject);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.EMFWorkbenchContext#initializeResourceSet(org.eclipse.wst.common.internal.emfworkbench.ProjectResourceSet)
	 */
	protected void initializeResourceSet(ProjectResourceSet aResourceSet) {
		super.initializeResourceSet(aResourceSet);
		Resource.Factory.Registry reg = new DefaultOverridableResourceFactoryRegistry();
		Resource.Factory factory = new ReferencedXMIFactoryImpl();
		reg.getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, factory);
		// add xmi because other plugins are registering it globally
		reg.getExtensionToFactoryMap().put("xmi", factory); //$NON-NLS-1$
		aResourceSet.setResourceFactoryRegistry(reg);
		aResourceSet.getSynchronizer().addExtender(this); // added so we can be informed of closes
		// to the project.
		startListeningToResourceSet();
	}

	public static String getCacheID(String editModelID, Map params) {
		return EditModelRegistry.getInstance().getCacheID(editModelID, params);
	}

	/**
	 * This is the API that clients should use when they have an intent to modify a particular
	 * resource. You should only access the resources through the J2EEEditModel that is returned by
	 * this method if you have the intent to modify.
	 * 
	 * @see J2EEEditModel
	 */
	public final EditModel getEditModelForWrite(String editModelID, Object accessorKey, Map params) {
		EditModel editModel = getExistingEditModel(editModelID, params, false);
		if (null == editModel) {
			editModel = createEditModelForWrite(editModelID, params);
			synchronized (editModel) {
				cacheEditModel(editModel, params);
				editModel.access(accessorKey);
			}
		} else {
			if (editModel.isDisposed() || editModel.isDisposing()) {
				editModel = createEditModelForWrite(editModelID, params);
				cacheEditModel(editModel, params);
			}
			synchronized (editModel) {
				editModel.access(accessorKey);
			}
		}
		return editModel;
	}

	/**
	 * This is the API that clients should use when they want to read a group of resources that are
	 * normally managed by the edit model with
	 * 
	 * @aKey. You should only access the resources through the J2EEEditModel that is returned by
	 *        this method. You must call releaseEditModel(...) when you are finished with the edit
	 *        model.
	 * @see J2EEEditModel
	 */
	public final EditModel getEditModelForRead(String editModelID, Object accessorKey, Map params) {
		try {
			EditModel editModel = getExistingEditModel(editModelID, params, true);
			if (null == editModel) {
				editModel = createEditModelForRead(editModelID, params);
				synchronized (editModel) {
					cacheEditModel(editModel, params);
					EditModelLeastUsedCache.getInstance().access(editModel);
					editModel.access(accessorKey);
				}
			} else {
				if (editModel.isDisposed() || editModel.isDisposing()) {
					editModel = createEditModelForRead(editModelID, params);
					cacheEditModel(editModel, params);
				}
				synchronized (editModel) {
					EditModelLeastUsedCache.getInstance().access(editModel);
					editModel.access(accessorKey);
				}
			}
			return editModel;
		} finally {
			EditModelLeastUsedCache.getInstance().optimizeLRUSizeIfNecessary();
		}
	}

	/**
	 * This is the API that clients should use when they have an intent to modify a particular
	 * resource. You should only access the resources through the J2EEEditModel that is returned by
	 * this method if you have the intent to modify.
	 * 
	 * @see J2EEEditModel
	 */
	public final EditModel getEditModelForWrite(String editModelID, Object accessorKey) {
		return getEditModelForWrite(editModelID, accessorKey, null);
	}

	/**
	 * This is the API that clients should use when they want to read a group of resources that are
	 * normally managed by the edit model with
	 * 
	 * @aKey. You should only access the resources through the J2EEEditModel that is returned by
	 *        this method. You must call releaseEditModel(...) when you are finished with the edit
	 *        model.
	 * @see J2EEEditModel
	 */
	public final EditModel getEditModelForRead(String editModelID, Object accessorKey) {
		return getEditModelForRead(editModelID, accessorKey, null);
	}



	public EditModel getExistingEditModel(String editModelID, Map params, boolean isReadOnly) {
		EditModel editModel = null;
		synchronized (readOnlyModels) {
			if (isReadOnly) {
				editModel = (EditModel) readOnlyModels.get(getCacheID(editModelID, params));
			} else {
				synchronized (editableModels) {
					editModel = (EditModel) editableModels.get(getCacheID(editModelID, params));
				}
			}
		}
		return editModel;
	}

	/**
	 * Subclasses may override to return the appropriate read-only J2EEEditModel.
	 */
	protected EditModel createEditModelForRead(String editModelID, Map params) {
		return EditModelRegistry.getInstance().createEditModelForRead(editModelID, this, params);
	}

	/**
	 * Subclasses may override to return the appropriate J2EEEditModel.
	 */
	protected EditModel createEditModelForWrite(String editModelID, Map params) {
		return EditModelRegistry.getInstance().createEditModelForWrite(editModelID, this, params);
	}

	/**
	 * Insert the method's description here. Creation date: (4/16/2001 12:25:39 PM)
	 * 
	 * @return java.util.List
	 */
	public void cacheEditModel(EditModel editModel, Map params) {
		editModel.setParams(params);
		synchronized (readOnlyModels) {
			if (editModel.isReadOnly())
				readOnlyModels.put(getCacheID(editModel.getEditModelID(), params), editModel);
			else
				synchronized (editableModels) {
					editableModels.put(getCacheID(editModel.getEditModelID(), params), editModel);
				}
		}
	}

	protected void discardAllEditModels() {
		synchronized (readOnlyModels) {
			synchronized (editableModels) {
				Collection readOnly = readOnlyModels.values();
				EditModelLeastUsedCache.getInstance().removeAllCached(readOnly);
				discardModels(readOnly);
				discardModels(editableModels.values());
			}
		}

	}

	private void discardModels(Collection editModels) {
		if (editModels != null && !editModels.isEmpty()) {
			// Make a copy for safety against concurrent modification
			Iterator it = new ArrayList(editModels).iterator();
			while (it.hasNext()) {
				((EditModel) it.next()).dispose();
			}
		}
	}

	public void removeEditModel(EditModel editModel, boolean readOnly) {
		// The best way would be to recompute the cache id, but we don't care
		// because the edit model should only be cached once anyway
		synchronized (readOnlyModels) {
			if (readOnly)
				readOnlyModels.values().remove(editModel);
			else
				synchronized (editableModels) {
					editableModels.values().remove(editModel);
				}
		}
	}

	/**
	 * Notify all editModels of the change.
	 */
	protected void notifyEditModels(EditModelEvent anEvent) {
		if (anEvent == null)
			return;
		List aList = new ArrayList();
		synchronized (readOnlyModels) {
			synchronized (editableModels) {
				aList.addAll(readOnlyModels.values());
				aList.addAll(editableModels.values());
			}
		}
		EditModel editModel;
		for (int i = 0; i < aList.size(); i++) {
			editModel = (EditModel) aList.get(i);
			try {
				editModel.resourceChanged(anEvent);
			} catch (Exception e) {
				Logger.getLogger().logError(e);
			}
		}
	}

	protected boolean shouldNotifyEditModels() {
		synchronized (readOnlyModels) {
			synchronized (editableModels) {
				return !this.readOnlyModels.isEmpty() || !this.editableModels.isEmpty();
			}
		}
	}

	protected Adapter getResourceSetListener() {
		if (resourceSetListener == null)
			resourceSetListener = new ResourceSetListener();
		return resourceSetListener;
	}


	protected class ResourceSetListener extends AdapterImpl {
		/*
		 * @see Adapter#notifyChanged(new ENotificationImpl((InternalEObject)Notifier,
		 *      int,(EStructuralFeature) EObject, Object, Object, int))
		 */
		public void notifyChanged(Notification notification) {
			switch (notification.getEventType()) {
				case Notification.ADD :
					addedResource((Resource) notification.getNewValue());
					break;
				case Notification.REMOVE :
					removedResource((Resource) notification.getOldValue());
					break;
				case Notification.REMOVE_MANY :
					removedResources((List) notification.getOldValue());
					break;
			}
		}
	}

	/**
	 * Notify all editModels of the change.
	 */
	public void addedResource(Resource addedResource) {
		if (defaultToMOF5Compatibility && (addedResource != null) && (addedResource instanceof CompatibilityXMIResource))
			((CompatibilityXMIResource) addedResource).setFormat(CompatibilityXMIResource.FORMAT_MOF5);
		if (shouldNotifyEditModels()) {
			EditModelEvent event = new EditModelEvent(EditModelEvent.ADDED_RESOURCE, null);
			event.addResource(addedResource);
			notifyEditModels(event);
		}
	}

	/**
	 * Notify all editModels of the change.
	 */
	public void removedResource(Resource removedResource) {
		if (shouldNotifyEditModels()) {
			EditModelEvent event = new EditModelEvent(EditModelEvent.REMOVED_RESOURCE, null);
			event.addResource(removedResource);
			notifyEditModels(event);
		}
	}

	/**
	 * Notify all editModels of the change.
	 */
	public void removedResources(List removedResources) {
		if (shouldNotifyEditModels()) {
			EditModelEvent event = new EditModelEvent(EditModelEvent.REMOVED_RESOURCE, null);
			event.addResources(removedResources);
			notifyEditModels(event);
		}
	}

	protected void startListeningToResourceSet() {
		ResourceSet set = getResourceSet();
		if (set != null)
			set.eAdapters().add(getResourceSetListener());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.ISynchronizerExtender#projectChanged(org.eclipse.core.resources.IResourceDelta)
	 */
	public void projectChanged(IResourceDelta delta) {
		// default nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.ISynchronizerExtender#projectClosed()
	 */
	public void projectClosed() {
		discardAllEditModels();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.EMFWorkbenchContextBase#createURIConverter(org.eclipse.wst.common.internal.emfworkbench.ProjectResourceSet)
	 */
	protected WorkbenchURIConverter createURIConverter(ProjectResourceSet aResourceSet) {
		return new CompatibilityWorkbenchURIConverterImpl(getProject(), aResourceSet.getSynchronizer());
	}

	protected ProjectResourceSet createResourceSet() {
		if (project == null)
			throw new IllegalStateException("Attempt to create resource set with null project"); //$NON-NLS-1$
		return new ProjectResourceSetEditImpl(project);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.EMFWorkbenchContextBase#deleteFile(org.eclipse.emf.ecore.resource.Resource)
	 */
	public void deleteFile(Resource resource) {
		try {
			WorkbenchResourceHelper.deleteResource(resource);
		} catch (CoreException ex) {
			Logger.getLogger().logError(ex);
		}

	}

	/**
	 * @return Returns the defaultToMOF5Compatibility.
	 */
	public boolean isDefaultToMOF5Compatibility() {
		return defaultToMOF5Compatibility;
	}

	/**
	 * @param defaultToMOF5Compatibility
	 *            The defaultToMOF5Compatibility to set.
	 */
	public void setDefaultToMOF5Compatibility(boolean defaultToMOF5Compatibility) {
		this.defaultToMOF5Compatibility = defaultToMOF5Compatibility;
	}

}

Back to the top