Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb1166c375dca43363c252416f0d39b27febb533 (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
/*******************************************************************************
 * Copyright (c) 2009 Mia-Software.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Nicolas Bros (Mia-Software) - initial API and implementation
 *    Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *    Gregoire Dupe (Mia-Software) - Bug 373078 - API Cleaning
 *******************************************************************************/

package org.eclipse.papyrus.emf.facet.util.core.internal.exported;

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.Platform;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.emf.facet.util.core.Logger;
import org.eclipse.papyrus.emf.facet.util.core.internal.Activator;
import org.eclipse.papyrus.emf.facet.util.core.internal.Messages;

/**
 * Provides common functionality for extension registries. Call {@link #initialize()} in a sub-class to read the extension points. Implement {@link #handleRootElement(IConfigurationElement)} to read the root
 * configuration elements of each extension.
 *
 * @since 0.2
 */
public abstract class AbstractRegistry {
	// copy of org.eclipse.papyrus.emf.facet.infra.common.core.internal.extensions.AbstractRegistry
	/**
	 * Initialize the registry by reading the extension point to discover
	 * extensions. This method calls {@link #handleRootElement(IConfigurationElement)} on each root
	 * configuration element.
	 */
	protected void initialize() {
		final IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
		final IExtensionPoint extensionPoint = extensionRegistry.getExtensionPoint(
				getExtensionPointNamespace(), getExtensionPointName());

		if (extensionPoint == null) {
			Logger.logError("Extension point not found:" + getExtensionPointNamespace() //$NON-NLS-1$
					+ "." + getExtensionPointName(), Activator.getDefault()); //$NON-NLS-1$
			return;
		}

		final IExtension[] extensions = extensionPoint.getExtensions();
		for (final IExtension extension : extensions) {
			final IConfigurationElement[] configurationElements = extension
					.getConfigurationElements();
			for (final IConfigurationElement configurationElement : configurationElements) {
				try {
					handleRootElement(configurationElement);
				} catch (Exception e) {
					final String errogMsg = NLS
							.bind("An exception occurred while loading registry of the extension point {0}. The problem occurred with the an extension contributed by the plug-in {1}", //$NON-NLS-1$
									getExtensionPointNamespace() + '.'
											+ getExtensionPointName(),
									extension.getContributor().getName());
					Logger.logError(e, errogMsg, Activator.getDefault());
				}
			}
		}
	}

	protected abstract String getExtensionPointNamespace();

	protected abstract String getExtensionPointName();

	/** Called for each root {@link IConfigurationElement} in the extension */
	protected abstract void handleRootElement(IConfigurationElement configurationElement);

	/**
	 * Logs the error in the log using the provided text and the information in
	 * the configuration element.
	 */
	protected static void logError(final IConfigurationElement element, final String text) {
		final IExtension extension = element.getDeclaringExtension();
		final StringBuffer buf = new StringBuffer();
		buf.append(NLS.bind(Messages.AbstractRegistry_pluginExtension, extension
				.getNamespaceIdentifier(), extension.getExtensionPointUniqueIdentifier()));
		// look for an ID if available - this should help debugging
		final String id = element.getAttribute("id"); //$NON-NLS-1$
		if (id != null) {
			buf.append(Messages.AbstractRegistry_id);
			buf.append(id);
		}
		buf.append(Messages.AbstractRegistry_colon + text);
		Logger.logError(buf.toString(), Activator.getDefault());
	}

	/**
	 * Logs a very common registry error when a required attribute is missing.
	 */
	protected static void logMissingAttribute(final IConfigurationElement element,
			final String attributeName) {
		logError(element, NLS.bind(Messages.AbstractRegistry_requiredAttributeNotDefined,
				attributeName));
	}

	/**
	 * Logs a registry error when the configuration element is unknown.
	 */
	protected static void logUnknownElement(final IConfigurationElement element) {
		logError(element, Messages.AbstractRegistry_unknownExtensionTag + element.getName());
	}
}

Back to the top