Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c3c7ff9c0f21607c5d895b8ccce8d2777e4a225 (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 Atos Origin, CEA, 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:
 *  Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 437217
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.services.resourceloading.Activator;
import org.eclipse.papyrus.infra.services.resourceloading.HistoryRoutingManager;
import org.eclipse.papyrus.infra.services.resourceloading.ILoadingStrategy;
import org.eclipse.papyrus.infra.services.resourceloading.ILoadingStrategyExtension;
import org.eclipse.papyrus.infra.services.resourceloading.IProxyManager;
import org.eclipse.papyrus.infra.services.resourceloading.IStrategyChooser;

/**
 * The Class ProxyManager that manages the proxy resolving according to a specific strategy.
 */
public class ProxyManager implements IProxyManager {

	// === Manage strategies

	/** extension point ID for loading strategy */
	private static final String LOADING_STRATEGY_EXTENSION_POINT_ID = "org.eclipse.papyrus.infra.services.resourceloading.loadingStrategy";

	/** element ID for the loading strategy element */
	private static final String LOADING_STRATEGY_ELEMENT_ID = "loadingStrategy";

	/** attribute ID for identification of the strategy */
	private static final String LOADING_STRATEGY_ID = "id";

	/** attribute ID for the description of the strategy */
	private static final String LOADING_STRATEGY_DESCRIPTION_ID = "description";

	/** attribute ID for the implementation of the strategy */
	private static final String STRATEGY_ID = "strategy";

	// ==== Manage strategy extensions (for UML profile)

	/** extension point ID for strategy extensions */
	private static final String STRATEGY_EXTENDER_EXTENSION_POINT_ID = "org.eclipse.papyrus.infra.services.resourceloading.loadingStrategyExtender";

	/** element ID for the loading strategy extension element */
	private static final String STRATEGY_EXTENDER_ELEMENT_ID = "strategyExtender";

	/** attribute ID for the implementation of the strategy extension */
	private static final String STRATEGY_EXTENSION_ID = "strategyExtension";

	// === Manage strategy chooser extension

	/** extension point ID for strategy chooser extension */
	private static final String STRATEGY_CHOOSER_EXTENSION_POINT_ID = "org.eclipse.papyrus.infra.services.resourceloading.currentStrategyChooser";

	/** attribute ID for the implementation of the strategy chooser extension used in preferences */
	private static final String STRATEGY_CHOOSER_CHOOSER_ATTRIBUTE = "chooser";

	private static IStrategyChooser strategyChooser = getStrategyChooser();

	// ===

	/** The strategies id and descriptions for preferences */
	private static Map<Integer, String> strategiesAndDescriptions = new HashMap<Integer, String>();

	/** custom commands from extensions */
	private static Map<Integer, ILoadingStrategy> availableStrategies = getLoadingStrategies();

	/** custom commands from strategy extensions */
	private static Set<ILoadingStrategyExtension> strategyExtensions = getLoadingStrategyExtensions();

	private ModelSet modelSet;

	private HistoryRoutingManager routeManager = new HistoryRoutingManager(this);

	public ProxyManager(ModelSet modelSet) {
		super();
		this.modelSet = modelSet;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.infra.services.resourceloading.IProxyManager#loadResource(URI)
	 */
	public boolean loadResource(URI uri) {
		boolean result = availableStrategies.get(getCurrentStrategy()).loadResource(modelSet, uri);
		Iterator<ILoadingStrategyExtension> iterator = strategyExtensions.iterator();
		while (!result && iterator.hasNext()) {
			ILoadingStrategyExtension extension = iterator.next();
			result = extension.loadResource(modelSet, uri.trimFragment());
		}
		return result;
	}

	/**
	 * Gets the eobject according to the strategy.
	 *
	 * @param uri
	 *            the specified uri to load
	 * @return the resolved eobject or the proxy, depending on the loading strategy
	 * @throws MissingResourceException
	 *             the missing resource exception
	 */
	public EObject getEObjectFromStrategy(URI uri) throws MissingResourceException {
		// ask the strategy if the resource of the uri must be loaded
		URI trimFragment = uri.trimFragment();
		boolean loadOnDemand = loadResource(trimFragment);
		// accept to recover object, either if strategy provides it, or if it has already been loaded anyway
		Resource resource = modelSet.getResource(trimFragment, loadOnDemand);
		if (resource != null) {
			String fragment = uri.fragment();
			EObject object = resource.getEObject(fragment);
			if (object != null) {
				// object find in the resource
				return object;
			}
			// use HistoryRoutingManager to explore routes in di resource history
			else {
				String fileExtension = uri.fileExtension();
				Resource diResource = null;
				String resourceName = "";
				if (DiModel.MODEL_FILE_EXTENSION.equals(fileExtension)) {
					// proxy is in DI resource
					diResource = modelSet.getResource(trimFragment, loadOnDemand);
					resourceName = trimFragment.toString();
				} else {
					// retrieve the DI resource from the uri to get the history
					URI newURI = trimFragment.trimFileExtension().appendFileExtension(DiModel.MODEL_FILE_EXTENSION);
					try {
						diResource = modelSet.getResource(newURI.trimFragment(), loadOnDemand);
					} catch (WrappedException e) {
						// capture wrapped exception here, see Bug 405047 - [core] FileNotFoundException during MARTE profile load
						throw new MissingResourceException(CommonPlugin.INSTANCE.getString("_UI_StringResourceNotFound_exception", new Object[] { resourceName }), getClass().getName(), resourceName);
					}
					resourceName = newURI.trimFragment().toString();
				}
				if (diResource != null) {
					// call the HistoryRoutingManager to get the EObject
					// we assume di/notation are at the same level in folder hierarchy
					EObject eobject = routeManager.getEObject(modelSet, uri.lastSegment().toString(), fragment);
					return eobject;
				} else {
					// resource di not found
					// warn the user, ask him to select the resource
					throw new MissingResourceException(CommonPlugin.INSTANCE.getString("_UI_StringResourceNotFound_exception", new Object[] { resourceName }), getClass().getName(), resourceName);
				}
			}
		} else if (loadOnDemand) {
			// resource not found
			// warn the user, ask him to select a resource to search in
			// or ask to search in the entire resource set
			// or use a proxy
			// strategy used for the specified resource only
			throw new MissingResourceException(CommonPlugin.INSTANCE.getString("_UI_StringResourceNotFound_exception", new Object[] { trimFragment.toString() }), getClass().getName(), trimFragment.toString());
		} else {
			// we just want to manage a proxy for this object
			return null;
		}
	}

	/**
	 * Gets the current strategy.
	 *
	 * @return the current strategy, strategy 0 (load all resources) is loaded by default if null
	 */
	private static int getCurrentStrategy() {
		if (strategyChooser == null) {
			return 0;
		}
		return strategyChooser.getCurrentStrategy();
	}

	/**
	 * Gets the all strategies.
	 *
	 * @return the all strategies
	 */
	public static Map<Integer, String> getAllStrategies() {
		return strategiesAndDescriptions;
	}

	/**
	 * Gets the available strategies from extensions
	 *
	 * @return the strategies
	 */
	private static Map<Integer, ILoadingStrategy> getLoadingStrategies() {
		Map<Integer, ILoadingStrategy> strategies = new HashMap<Integer, ILoadingStrategy>();
		IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor(LOADING_STRATEGY_EXTENSION_POINT_ID);
		for (IConfigurationElement element : extensions) {
			if (LOADING_STRATEGY_ELEMENT_ID.equals(element.getName())) {
				try {
					// use description in extension to define preferences from the extensions
					int id = Integer.valueOf(element.getAttribute(LOADING_STRATEGY_ID));
					String description = element.getAttribute(LOADING_STRATEGY_DESCRIPTION_ID);
					ILoadingStrategy strategy = (ILoadingStrategy) element.createExecutableExtension(STRATEGY_ID);
					strategies.put(id, strategy);
					strategiesAndDescriptions.put(id, description);
				} catch (CoreException e1) {
					Activator.log.error(e1.getMessage(), e1);
					e1.printStackTrace();
				} catch (NumberFormatException e2) {
					Activator.log.error(e2.getMessage(), e2);
					e2.printStackTrace();
				}
			}
		}
		return strategies;
	}

	/**
	 * Gets the strategy extensions
	 *
	 * @return the strategy extensions
	 */
	private static Set<ILoadingStrategyExtension> getLoadingStrategyExtensions() {
		Set<ILoadingStrategyExtension> strategies = new HashSet<ILoadingStrategyExtension>();
		IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor(STRATEGY_EXTENDER_EXTENSION_POINT_ID);
		for (IConfigurationElement element : extensions) {
			if (STRATEGY_EXTENDER_ELEMENT_ID.equals(element.getName())) {
				try {
					ILoadingStrategyExtension strategyExtension = (ILoadingStrategyExtension) element.createExecutableExtension(STRATEGY_EXTENSION_ID);
					strategies.add(strategyExtension);
				} catch (CoreException e) {
					Activator.log.error(e.getMessage(), e);
					e.printStackTrace();
				}
			}
		}
		return strategies;
	}

	/**
	 * There is only one Strategy chooser chosen in extension registry.
	 *
	 * @return
	 */
	public static IStrategyChooser getStrategyChooser() {
		IStrategyChooser result = null;
		IConfigurationElement[] element = Platform.getExtensionRegistry().getConfigurationElementsFor(STRATEGY_CHOOSER_EXTENSION_POINT_ID);
		if (element.length > 0) {
			IConfigurationElement e = element[0];
			try {
				result = (IStrategyChooser) e.createExecutableExtension(STRATEGY_CHOOSER_CHOOSER_ATTRIBUTE);
			} catch (CoreException e1) {
				Activator.log.error(e1.getMessage(), e1);
				e1.printStackTrace();
			}
		}
		return result;
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.infra.services.resourceloading.IProxyManager#dispose()
	 */
	public void dispose() {
		routeManager.unload();
	}

}

Back to the top