Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c417e0b8bedbfd3bf2ce898b3e020af80565d67f (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*******************************************************************************
 * Copyright (c) 2005, 2007 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.jst.jee.model.internal;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
import org.eclipse.jem.util.emf.workbench.FlexibleProjectResourceSet;
import org.eclipse.jem.util.emf.workbench.ProjectResourceSet;
import org.eclipse.jem.util.emf.workbench.WorkbenchResourceHelperBase;
import org.eclipse.jst.j2ee.componentcore.EnterpriseArtifactEdit;
import org.eclipse.jst.j2ee.model.IModelProvider;
import org.eclipse.jst.j2ee.model.IModelProviderEvent;
import org.eclipse.jst.j2ee.model.IModelProviderListener;
import org.eclipse.jst.j2ee.model.ModelProviderEvent;
import org.eclipse.jst.javaee.core.internal.util.JavaeeResourceImpl;
import org.eclipse.jst.jee.JEEPlugin;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
import org.eclipse.wst.common.componentcore.internal.impl.PlatformURLModuleConnection;
import org.eclipse.wst.common.componentcore.internal.impl.WTPResourceFactoryRegistry;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.internal.emfworkbench.WorkbenchResourceHelper;
import org.eclipse.wst.common.internal.emfworkbench.validateedit.ResourceStateInputProvider;
import org.eclipse.wst.common.internal.emfworkbench.validateedit.ResourceStateValidator;
import org.eclipse.wst.common.internal.emfworkbench.validateedit.ResourceStateValidatorImpl;
import org.eclipse.wst.common.internal.emfworkbench.validateedit.ResourceStateValidatorPresenter;

public class JEE5ModelProvider implements IModelProvider, ResourceStateInputProvider, ResourceStateValidator, IModelProviderListener{

	protected XMLResourceImpl writableResource;
	protected IProject proj;
	protected IPath defaultResourcePath;
	protected ResourceStateValidator stateValidator;
	protected ResourceAdapter resourceAdapter = new ResourceAdapter();
	protected final ListenerList listeners = new ListenerList();
	//private static boolean resourceChangeListenerEnabled = false;

	private List modelResources = new ArrayList();
	protected class ResourceAdapter extends AdapterImpl {
		@Override
		public void notifyChanged(Notification notification) {
			if (notification.getEventType() == Notification.SET
					&& notification.getFeatureID(null) == Resource.RESOURCE__IS_LOADED) {
				resourceIsLoadedChanged((Resource) notification.getNotifier(), notification.getOldBooleanValue(), notification.getNewBooleanValue());
			} else if (notification.getFeatureID(null) == Resource.RESOURCE__IS_MODIFIED)
				resourceChanged((Resource) notification.getNotifier());
		}
		@Override
		public boolean equals(Object arg0) {
			
			return this.getClass().equals(arg0.getClass());
		}

		@Override
		public int hashCode() {
			
			return getClass().hashCode();
		}
	}
	
	public JEE5ModelProvider() {
		super();
	}

	protected ProjectResourceSet getResourceSet(IProject proj2) {
		return (ProjectResourceSet)WorkbenchResourceHelperBase.getResourceSet(proj);
	}

	public XMLResourceImpl getWritableResource() {
		return writableResource;
	}

	public void setWritableResource(XMLResourceImpl writableResource) {
		this.writableResource = writableResource;
	}
	
	protected void resourceChanged(Resource aResource) {
		if (hasListeners()) {
			int eventCode = ModelProviderEvent.KNOWN_RESOURCES_CHANGED;
			ModelProviderEvent evt = new ModelProviderEvent(eventCode, this, proj);
			evt.addResource(aResource);
			notifyListeners(evt);
		}
	}
	
	protected void resourceIsLoadedChanged(Resource aResource, boolean oldValue, boolean newValue) {
		if (hasListeners()) {
			int eventCode = newValue ? ModelProviderEvent.LOADED_RESOURCE : ModelProviderEvent.UNLOADED_RESOURCE;
			ModelProviderEvent evt = new ModelProviderEvent(eventCode, this, proj);
			evt.addResource(aResource);
			notifyListeners(evt);
		}
	}
	private void addManagedResource(XMLResourceImpl res) {
		modelResources.add(res);
		if (!res.eAdapters().contains(resourceAdapter))
			res.eAdapters().add(resourceAdapter);
	}
	/**
	 * Returns true if there are any listeners
	 */
	public boolean hasListeners() {
		return !listeners.isEmpty();
	}
	
	private URI getModuleURI(URI uri) {
		URI moduleuri = ModuleURIUtil.fullyQualifyURI(proj,getContentTypeDescriber());
		IPath requestPath = new Path(moduleuri.path()).append(new Path(uri.path()));
		URI resourceURI = URI.createURI(PlatformURLModuleConnection.MODULE_PROTOCOL + requestPath.toString());
		return resourceURI;
	}

	protected XMLResourceImpl getModelResource(final IPath modelPath) {
		if(proj == null || !proj.isAccessible()){
			throw new IllegalStateException("The project <" + proj + "> is not accessible."); //$NON-NLS-1$//$NON-NLS-2$
		}
		if (writableResource != null) {
			addManagedResource(writableResource);
			return writableResource;
		}
		IPath innerModelPath = modelPath;
		if ((innerModelPath == null) || innerModelPath.equals(IModelProvider.FORCESAVE))
			innerModelPath = getDefaultResourcePath();
		ProjectResourceSet resSet = getResourceSet(proj);
		IVirtualFolder container = ComponentCore.createComponent(proj).getRootFolder();
		String modelPathURI = innerModelPath.toString();
		URI uri = URI.createURI(modelPathURI);
		
		IVirtualFile dd = container.getFile(innerModelPath);
		URI projURI = URI.createURI(dd.getProjectRelativePath().toString());
		
		XMLResourceImpl res = null;
		try {
			if (dd.exists()) {
				res = (XMLResourceImpl) resSet.getResource(getModuleURI(uri),true);
				addManagedResource(res);
			} else {//First find in resource set, then create if not found new Empty Resource.
				XMLResourceImpl newRes =  createModelResource(innerModelPath, resSet, projURI);
				addManagedResource(newRes);
				return newRes;
			}
		} catch (WrappedException ex) {
			if (ex.getCause() instanceof FileNotFoundException)
				return null;
			throw ex;
		}
		return res;
	}
	

	protected XMLResourceImpl createModelResource(IPath modelPath, ProjectResourceSet resourceSet, URI uri) {
		// First try to find existing cached resource.
		XMLResourceImpl res = (XMLResourceImpl)resourceSet.getResource(getModuleURI(uri), false);
		if (res == null || !res.isLoaded()) {
			// Create temp resource if no file exists
			res=  (XMLResourceImpl)((FlexibleProjectResourceSet)resourceSet).createResource(getModuleURI(uri),WTPResourceFactoryRegistry.INSTANCE.getFactory(uri, getContentType(getContentTypeDescriber())));
			res.setEncoding("UTF-8"); //$NON-NLS-1$
			populateRoot(res, resourceSet.getProject().getName());
		}
		return res;
	}

	public void populateRoot(XMLResourceImpl res, String string) {
	}

	private IContentDescription getContentType(String contentTypeDescriber) {
		if (contentTypeDescriber != null)
			return Platform.getContentTypeManager().getContentType(contentTypeDescriber).getDefaultDescription();
		return null;
	}

	public IPath getDefaultResourcePath() {
		return defaultResourcePath;
	}

	public void setDefaultResourcePath(IPath defaultResourcePath) {
		this.defaultResourcePath = defaultResourcePath;
	}

	public Object getModelObject() {
		return getModelObject(getDefaultResourcePath());
	}

	public Object getModelObject(IPath modelPath) {
		return null;
	}
	
	/**
	 * Used to optionally define an associated content type for XML file creation
	 * @return
	 */
	protected String getContentTypeDescriber() {
		
		return null;
	}
	


	public IStatus validateEdit(final IPath modelPath, final Object context) {
		IPath innaerModelPath = modelPath;
		Object innerContext = context;
		if (innaerModelPath == null)
			innaerModelPath = getDefaultResourcePath();
		IWorkspace work = ResourcesPlugin.getWorkspace();
		IFile file = WorkbenchResourceHelper.getFile(getModelResource(innaerModelPath));
		if (file != null && file.exists()) {
			IFile[] files = { file };
			if (innerContext == null)
				innerContext = IWorkspace.VALIDATE_PROMPT;
			return work.validateEdit(files, innerContext);
		}
		return Status.OK_STATUS;
	}

	public void modify(Runnable runnable, IPath modelPath) {
		//About to modify and save this model
		try {
			JavaeeResourceImpl res = (JavaeeResourceImpl)getModelResource(modelPath);
			if (res != null)
				setWritableResource(res);
			runnable.run();
			try {
				if (res != null) {
					if (modelPath != null && modelPath.equals(IModelProvider.FORCESAVE))
						res.save(Collections.EMPTY_MAP,true);
					else
						res.save(Collections.EMPTY_MAP);
				}
			} catch (IOException e) {
				JEEPlugin.logError(e);
			}
		} catch (Exception ex) {
			JEEPlugin.logError(ex);
		} finally {
			setWritableResource(null);
		}
		
	}

//	private class ResourceChangeListener implements IResourceChangeListener {
//		public void resourceChanged(IResourceChangeEvent event) {
//			IResourceDelta delta= event.getDelta();
//			// make sure that there is a delta (since some events don't have one)
//			if (delta != null)
//			{
//				IResourceDelta[] affectedChildren= delta.getAffectedChildren(IResourceDelta.CHANGED | IResourceDelta.REMOVED , IResource.FILE);
//				IResourceDelta projectDelta = null;
//				IResource changedResource = null; 
//				IProject changedProject = null;
//				IPath resourcePath = null;
//
//				for (int i= 0; i < affectedChildren.length; i++) {
//					projectDelta = affectedChildren[i];
//					changedResource = projectDelta.getResource(); 
//					changedProject = changedResource.getProject();
//					HashSet<IPath> currentResources = modelResources.get(changedProject);
//					// only deal with the projects that have resources that have been loaded 
//					if (currentResources != null)
//					{
//						// if this is a project deletion, remove the project from the HashMap.
//						if (changedResource == changedProject && projectDelta.getKind() == IResourceDelta.REMOVED)
//						{
//							modelResources.remove(changedProject);
//							// if modelResources is empty, we should self-destruct
//							if (modelResources.isEmpty())
//							{
//								resourceChangeListenerEnabled = false;
//								ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
//							}
//						}
//						else
//						{
//							Iterator<IPath> iter = currentResources.iterator();
//							ArrayList<IPath> toUnload = new ArrayList<IPath>();
//							// check each resource that was loaded from the project to see if it is part of the change
//							while (iter.hasNext())
//							{
//								resourcePath = iter.next();
//								if (projectDelta.findMember(resourcePath) != null)
//								{
//									// limit the list of resources that need to be unloaded to those that have changed
//									toUnload.add(resourcePath);
//								}
//							}
//							if (toUnload.size() > 0)
//							{
//								Resource current = null;
//								ProjectResourceSet resourceSet = getResourceSet(changedProject);
//								URIConverter uriConverter = resourceSet.getURIConverter();
//								HashSet<URI> resourceURIs = new HashSet<URI>();
//								iter = toUnload.iterator();
//								while (iter.hasNext())
//								{
//									// convert all of the resources to URIs - this is a faster match during the compare
//									resourceURIs.add(uriConverter.normalize(URI.createURI(iter.next().toString())));
//								}
//								Iterator<Resource> iter2 = resourceSet.getResources().iterator();
//								while (iter2.hasNext())
//								{
//									current = iter2.next();
//									if (resourceURIs.contains(current.getURI()))
//									{
//										current.unload();
//									}
//								}
//							}
//						}
//					}
//				}
//			}
//		}
//	}

	public void addListener(IModelProviderListener listener) {
		
		listeners.add(listener);
	}

	public void removeListener(IModelProviderListener listener)
	{
		listeners.remove(listener);
	}
	/**
	 * Save only resources that need to be saved (i.e., no other references).
	 */
	public void modelsChanged(IModelProviderEvent anEvent) {
		int code = anEvent.getEventCode();
		switch (code) {
			case IModelProviderEvent.REMOVED_RESOURCE : {
				if (hasResourceReference(anEvent.getChangedResources()))
					removeResources(anEvent.getChangedResources());
				else
					return;
				break;
			}
		}
		if (hasListeners()) {
			anEvent.setModel(this);
			notifyListeners(anEvent);
		}
	}
	protected void removeResources(List aList) {
		Resource res;
		for (int i = 0; i < aList.size(); i++) {
			res = (Resource) aList.get(i);
			removeResource(res) ;
		}
	}
	/**
	 * Remove reference to the aResource.
	 */
	protected boolean removeResource(Resource aResource) {
		if (aResource != null) {
			aResource.eAdapters().remove(resourceAdapter);
			return getResources().remove(aResource);
		}
		return false;
	}
	
	/**
	 * Return true if any Resource in the list of
	 * 
	 * @resources is referenced by me.
	 */
	protected boolean hasResourceReference(List tResources) {
		for (int i = 0; i < tResources.size(); i++) {
			if (hasResourceReference((Resource) tResources.get(i)))
				return true;
		}
		return false;
	}
	/**
	 * Return true if aResource is referenced by me.
	 */
	protected boolean hasResourceReference(Resource aResource) {
		if (aResource != null)
			return getResources().contains(aResource);
		return false;
	}
	
	/**
	 * Notify listeners of
	 * 
	 * @anEvent.
	 */
	protected void notifyListeners(IModelProviderEvent anEvent) {
		
		NotifyRunner notifier = new NotifyRunner(anEvent); 
		
		Object[] notifyList = listeners.getListeners(); 
		for (int i = 0; i < notifyList.length; i++) {
			notifier.setListener( (IModelProviderListener) notifyList[i] );
			SafeRunner.run(notifier);
		}
	}
	public class NotifyRunner implements ISafeRunnable { 
		
		private final IModelProviderEvent event;
		private IModelProviderListener listener;
		
		public NotifyRunner(IModelProviderEvent event) {
			Assert.isNotNull(event);
			this.event = event;
		}
		
		
		public void setListener(IModelProviderListener listener) {
			this.listener = listener;
		}

		public void handleException(Throwable exception) { 
			JEEPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JEEPlugin.PLUGIN_ID, 0, exception.getMessage(), exception));
			
		}

		public void run() throws Exception {
			if(listener != null)
				listener.modelsChanged(event); 
		}
		
	}
	public ResourceStateValidator getStateValidator() {
		if (stateValidator == null)
			stateValidator = createStateValidator();
		return stateValidator;
	}

	/**
	 * Method createStateValidator.
	 * 
	 * @return ResourceStateValidator
	 */
	private ResourceStateValidator createStateValidator() {
		return new ResourceStateValidatorImpl(this);
	}

	protected EnterpriseArtifactEdit createArtifactEdit() {
		return null;
	}

	public void checkActivation(ResourceStateValidatorPresenter presenter) throws CoreException {
		getStateValidator().checkActivation(presenter);
		
	}

	public boolean checkReadOnly() {
		return getStateValidator().checkReadOnly();
	}

	public boolean checkSave(ResourceStateValidatorPresenter presenter) throws CoreException {
		return getStateValidator().checkSave(presenter);
	}

	public void lostActivation(ResourceStateValidatorPresenter presenter) throws CoreException {
		getStateValidator().lostActivation(presenter);
		
	}

	public IStatus validateState(ResourceStateValidatorPresenter presenter) throws CoreException {
		if (presenter == null)
			return Status.OK_STATUS;
		return getStateValidator().validateState(presenter);
	}

	public void cacheNonResourceValidateState(List roNonResourceFiles) {
		// do nothing
	}

	public List getNonResourceFiles() {
		return null;
	}

	public List getNonResourceInconsistentFiles() {
		return null;
	}

	public List getResources() {
		return modelResources;
	}


	public boolean isDirty() {
		
			List list = getResources();
			for (int i = 0; i < list.size(); i++) {
				if (((Resource) list.get(i)).isModified())
					return true;
			}
			return false;
	}
}

Back to the top