Proper loading of the profiles and packages from the SRS
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.uml2.ide / src / org / eclipse / emf / compare / uml2 / ide / ResourceSetProfileUnloader.java
blob4df8490b6e0aeace72730d6304c2ea4477ebe61d
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Obeo and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 * Stefan Dirix - bug 460780
11 *******************************************************************************/
12 package org.eclipse.emf.compare.uml2.ide;
14 import java.util.Collection;
15 import java.util.LinkedHashSet;
16 import java.util.Set;
18 import org.eclipse.emf.common.util.URI;
19 import org.eclipse.emf.compare.ide.hook.IResourceSetHook;
20 import org.eclipse.emf.compare.uml2.rcp.internal.policy.UMLLoadOnDemandPolicy;
21 import org.eclipse.emf.ecore.EObject;
22 import org.eclipse.emf.ecore.EStructuralFeature;
23 import org.eclipse.emf.ecore.resource.Resource;
24 import org.eclipse.emf.ecore.resource.ResourceSet;
25 import org.eclipse.emf.ecore.resource.URIConverter;
26 import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
27 import org.eclipse.uml2.common.util.CacheAdapter;
28 import org.eclipse.uml2.uml.Profile;
29 import org.eclipse.uml2.uml.ProfileApplication;
31 /**
32 * The {@link UMLLoadOnDemandPolicy} will load profiles in the resource set used by EMF Compare. These will be
33 * referenced in package registries and extended meta-data for their resource set. We want them to be properly
34 * unloaded and will do so from here.
36 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
38 public class ResourceSetProfileUnloader extends UMLLoadOnDemandPolicy implements IResourceSetHook {
39 public boolean isHookFor(Collection<? extends URI> uris) {
40 for (URI uri : uris) {
41 if (UML_EXTENSION.equals(uri.fileExtension())) {
42 return true;
45 return false;
48 public void preLoadingHook(ResourceSet resourceSet, Collection<? extends URI> uris) {
49 // We're not interested in this event
52 public void postLoadingHook(ResourceSet resourceSet, Collection<? extends URI> uris) {
53 // We're not interested in this event
56 public void onDispose(Iterable<Resource> resources) {
57 URIConverter uriConverter = new ExtensibleURIConverterImpl();
58 for (Resource resource : resources) {
59 if (resource.isLoaded()) {
60 URI uri = resource.getURI();
61 URI normalizedURI = uriConverter.normalize(uri);
62 if (isConventionalURIForUMLProfile(normalizedURI) || isUMLMetaModel(normalizedURI)
63 || isRegisteredUMLProfile(normalizedURI, uriConverter)) {
64 for (EObject child : resource.getContents()) {
65 // TODO Are profiles always at the root?
66 if (child instanceof Profile) {
67 disposeProfileApplications((Profile)child);
68 break;
71 resource.unload();
77 /**
78 * UML will hold onto the profiles in memory even if we unload them completely unless we do the same for
79 * the resources they're referenced from.
81 * @param profile
82 * The profile which applications we need to dispose of.
84 private void disposeProfileApplications(Profile profile) {
85 final Set<Resource> unloadMe = new LinkedHashSet<Resource>();
86 Collection<EStructuralFeature.Setting> settings = CacheAdapter.getInstance().getInverseReferences(
87 profile, false);
88 for (EStructuralFeature.Setting setting : settings) {
89 if (setting.getEObject() instanceof ProfileApplication) {
90 unloadMe.add(setting.getEObject().eResource());
93 for (Resource unload : unloadMe) {
94 if (unload.isLoaded()) {
95 unload.unload();