Skip to main content
summaryrefslogtreecommitdiffstats
blob: 691d1e1dc2b99333cabfdda19831bef8ed4138b6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.bugs;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IBundleGroup;
import org.eclipse.core.runtime.IBundleGroupProvider;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.tasks.bugs.IProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.branding.IBundleGroupConstants;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * @author Steffen Pingel
 */
public class SupportProviderManager {

	private static final String ATTRIBUTE_CATEGORY_ID = "categoryId"; //$NON-NLS-1$

	private static final String ATTRIBUTE_DESCRIPTION = "description"; //$NON-NLS-1$

	private static final String ATTRIBUTE_FEATURE_ID = "featureId"; //$NON-NLS-1$

	private static final String ATTRIBUTE_ICON = "icon"; //$NON-NLS-1$

	private static final String ATTRIBUTE_ID = "id"; //$NON-NLS-1$

	private static final String ATTRIBUTE_NAME = "name"; //$NON-NLS-1$

	private static final String ATTRIBUTE_NAMESPACE = "namespace"; //$NON-NLS-1$

	private static final String ATTRIBUTE_PLUGIN_ID = "pluginId"; //$NON-NLS-1$

	private static final String ATTRIBUTE_PRODUCT_ID = "productId"; //$NON-NLS-1$

	private static final String ATTRIBUTE_PROVIDER_ID = "providerId"; //$NON-NLS-1$

	private static final String ATTRIBUTE_REPOSITORY_KIND = "kind"; //$NON-NLS-1$

	private static final String ATTRIBUTE_REPOSITORY_URL = "url"; //$NON-NLS-1$

	private static final String ATTRIBUTE_URL = "url"; //$NON-NLS-1$

	private static final String ATTRIBUTE_VALUE = "value"; //$NON-NLS-1$

	private static final String ATTRIBUTE_WEIGHT = "weight"; //$NON-NLS-1$

	private static final String ELEMENT_CATEGORY = "category"; //$NON-NLS-1$

	private static final String ELEMENT_MAPPING = "mapping"; //$NON-NLS-1$

	private static final String ELEMENT_PRODUCT = "product"; //$NON-NLS-1$

	private static final String ELEMENT_PROPERTY = "property"; //$NON-NLS-1$

	private static final String ELEMENT_PROVIDER = "provider"; //$NON-NLS-1$

	private static final String ELEMENT_REPOSITORY = "repository"; //$NON-NLS-1$

	private static final String EXTENSION_ID_PLUGIN_SUPPORT = "org.eclipse.mylyn.tasks.bugs.support"; //$NON-NLS-1$

	private HashMap<String, IBundleGroup> bundleGroupById;

	private List<SupportCategory> categories;

	private SupportProduct defaultProduct;

	private Map<String, SupportProduct> productById;

	private Map<String, SupportProvider> providerById;

	public SupportProviderManager() {
		readExtensions();
	}

	public void addCategory(SupportCategory category) {
		categories.add(category);
	}

	public boolean addProduct(SupportProduct product) {
		if (providerById.containsKey(product.getId())) {
			return false;
		}
		productById.put(product.getId(), product);
		return true;
	}

	public boolean addProvider(SupportProvider provider) {
		if (providerById.containsKey(provider.getId())) {
			return false;
		}
		providerById.put(provider.getId(), provider);
		return true;
	}

	private IBundleGroup getBundleGroup(String featureId) {
		if (bundleGroupById == null) {
			bundleGroupById = new HashMap<String, IBundleGroup>();
			IBundleGroupProvider[] providers = Platform.getBundleGroupProviders();
			if (providers != null) {
				for (IBundleGroupProvider provider : providers) {
					for (IBundleGroup bundleGroup : provider.getBundleGroups()) {
						bundleGroupById.put(bundleGroup.getIdentifier(), bundleGroup);
					}
				}
			}
		}
		return bundleGroupById.get(featureId);
	}

	public Collection<SupportCategory> getCategories() {
		return Collections.unmodifiableList(categories);
	}

	public SupportCategory getCategory(String categoryId) {
		for (SupportCategory category : categories) {
			if (category.getId().equals(categoryId)) {
				return category;
			}
		}
		return null;
	}

	public SupportProduct getDefaultProduct() {
		return defaultProduct;
	}

	public SupportProduct getProduct(String productId) {
		return productById.get(productId);
	}

	public Collection<SupportProduct> getProducts() {
		return Collections.unmodifiableCollection(productById.values());
	}

	public SupportProvider getProvider(String providerId) {
		return providerById.get(providerId);
	}

	public Collection<SupportProvider> getProviders() {
		return Collections.unmodifiableCollection(providerById.values());
	}

	private boolean readAttributes(IConfigurationElement element, AbstractSupportElement item) {
		item.setId(element.getAttribute(ATTRIBUTE_ID));
		item.setName(element.getAttribute(ATTRIBUTE_NAME));
		item.setDescription(element.getAttribute(ATTRIBUTE_DESCRIPTION));
		item.setUrl(element.getAttribute(ATTRIBUTE_URL));
		String iconPath = element.getAttribute(ATTRIBUTE_ICON);
		if (iconPath != null) {
			ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(element.getContributor().getName(),
					iconPath);
			if (descriptor != null) {
				item.setIcon(descriptor);
			}
		}

		// optionally complement data from referenced feature
		boolean available = true;
		String featureId = element.getAttribute(ATTRIBUTE_FEATURE_ID);
		if (featureId != null) {
			IBundleGroup bundleGroup = getBundleGroup(featureId);
			if (bundleGroup == null) {
				// indicate that the specified feature was not found
				available = false;
			} else {
				if (item.getName() == null) {
					item.setName(bundleGroup.getName());
				}
				if (item.getDescription() == null) {
					item.setDescription(bundleGroup.getDescription());
				}
				if (item.getIcon() == null) {
					String imageUrl = bundleGroup.getProperty(IBundleGroupConstants.FEATURE_IMAGE);
					if (imageUrl != null) {
						try {
							item.setIcon(ImageDescriptor.createFromURL(new URL(imageUrl)));
						} catch (MalformedURLException e) {
							// ignore 
						}
					}
				}
				if (item instanceof SupportProduct) {
					((SupportProduct) item).setBundleGroup(bundleGroup);
				}
			}
		}

		if (item.getName() == null) {
			item.setName(Messages.SupportProviderManager_Product_Unknown);
		}

		return available;
	}

	private void readCategory(IConfigurationElement element) {
		SupportCategory category = new SupportCategory();
		readAttributes(element, category);
		String weight = element.getAttribute(ATTRIBUTE_WEIGHT);
		if (weight != null) {
			try {
				category.setWeight(Integer.parseInt(weight));
			} catch (NumberFormatException e) {
				// ignore
			}
		}
		categories.add(category);
	}

	private void readExtensions() {
		categories = new ArrayList<SupportCategory>();
		productById = new HashMap<String, SupportProduct>();
		providerById = new HashMap<String, SupportProvider>();
		defaultProduct = new SupportProduct();

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(EXTENSION_ID_PLUGIN_SUPPORT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (element.getName().equals(ELEMENT_CATEGORY)) {
					readCategory(element);
				}
			}
		}
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (element.getName().equals(ELEMENT_PROVIDER)) {
					readProvider(element);
				}
			}
		}
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (element.getName().equals(ELEMENT_PRODUCT)) {
					readProduct(element);
				}
			}
		}
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (element.getName().equals(ELEMENT_MAPPING)) {
					readMapping(element);
				}
			}
		}

		// clear cache
		bundleGroupById = null;
	}

	private ProductRepositoryMapping readMapping(IConfigurationElement element) {
		String namespace = element.getAttribute(ATTRIBUTE_NAMESPACE);
		String productId = element.getAttribute(ATTRIBUTE_PRODUCT_ID);
		Map<String, String> attributes = new HashMap<String, String>();
		// repository
		for (IConfigurationElement attributeElement : element.getChildren(ELEMENT_REPOSITORY)) {
			String repositoryUrl = attributeElement.getAttribute(ATTRIBUTE_REPOSITORY_URL);
			attributes.put(IRepositoryConstants.REPOSITORY_URL, repositoryUrl);
			String connectorKind = attributeElement.getAttribute(ATTRIBUTE_REPOSITORY_KIND);
			attributes.put(IRepositoryConstants.CONNECTOR_KIND, connectorKind);
		}
		// attributes
		for (IConfigurationElement attributeElement : element.getChildren(ELEMENT_PROPERTY)) {
			String name = attributeElement.getAttribute(ATTRIBUTE_NAME);
			String value = attributeElement.getAttribute(ATTRIBUTE_VALUE);
			attributes.put(name, value);
		}

		if (!attributes.isEmpty()) {
			ProductRepositoryMapping mapping = new ProductRepositoryMapping(namespace);
			mapping.addAttributes(attributes);

			final SupportProduct product;
			if (productId == null) {
				product = defaultProduct;
			} else {
				product = getProduct(productId);
				if (product == null) {
					StatusHandler.log(new Status(
							IStatus.WARNING,
							TasksBugsPlugin.ID_PLUGIN,
							NLS.bind(
									"Mapping contributed by {0} with namespace ''{1}'' ignored, unkown product id ''{1}'' specified", //$NON-NLS-1$
									new String[] { element.getNamespaceIdentifier(), namespace, productId })));
					return null;
				}
			}
			product.addRepositoryMapping(mapping);
			return mapping;
		} else {
			StatusHandler.log(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN, NLS.bind(
					"Mapping contributed by {0} with namespace ''{1}'' ignored, no attributes specified", //$NON-NLS-1$
					new String[] { element.getNamespaceIdentifier(), namespace })));
			return null;
		}
	}

	private SupportProduct readProduct(IConfigurationElement element) {
		String id = element.getAttribute(ATTRIBUTE_ID);
		String providerId = element.getAttribute(ATTRIBUTE_PROVIDER_ID);
		IProvider provider = getProvider(providerId);
		if (provider == null) {
			StatusHandler.log(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN, NLS.bind(
					"Product contributed by {0} with id ''{1}'' ignored, unknown provider id ''{2}'' specified", //$NON-NLS-1$
					new String[] { element.getNamespaceIdentifier(), id, providerId })));
			return null;
		}
		boolean enabled = true;
		String pluginId = element.getAttribute(ATTRIBUTE_PLUGIN_ID);
		if (pluginId != null) {
			enabled &= Platform.getBundle(pluginId) != null;
		}
		SupportProduct product = new SupportProduct();
		enabled &= readAttributes(element, product);
		// disable products that do not have a corresponding plug-in or feature installed
		product.setInstalled(enabled);
		product.setProvider(provider);
		if (!addProduct(product)) {
			StatusHandler.log(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN, NLS.bind(
					"Product contributed by {0} ignored, id ''{1}'' already present", //$NON-NLS-1$
					element.getNamespaceIdentifier(), id)));
			return null;
		}
		((SupportProvider) provider).add(product);
		productById.put(id, product);
		return product;
	}

	private SupportProvider readProvider(IConfigurationElement element) {
		String id = element.getAttribute(ATTRIBUTE_ID);
		SupportProvider provider = new SupportProvider();
		readAttributes(element, provider);
		if (!addProvider(provider)) {
			StatusHandler.log(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN, NLS.bind(
					"Provider contributed by {0} ignored, id ''{1}'' already present", //$NON-NLS-1$
					element.getNamespaceIdentifier(), id)));
			return null;
		}
		String categoryId = element.getAttribute(ATTRIBUTE_CATEGORY_ID);
		SupportCategory category = getCategory(categoryId);
		if (category == null) {
			StatusHandler.log(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN, NLS.bind(
					"Provider contributed by {0} ignored with id ''{1}'' ignored, category id ''{2}'' is invalid", //$NON-NLS-1$
					new String[] { element.getNamespaceIdentifier(), id, categoryId })));
			return null;
		}
		providerById.put(id, provider);
		category.add(provider);
		provider.setCategory(category);
		return provider;
	}

}

Back to the top