Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2011-11-12 09:54:24 -0500
committerUwe Stieber2011-11-12 09:54:24 -0500
commite71c5240685f128ce1fe3d03a72f52908e6a9d52 (patch)
tree7b92c03c042d3a7ad13adc4b12552da0cb9e7696
parent56002d55f8ded2f14bf1b30629e332aefaf747a6 (diff)
downloadorg.eclipse.tcf-e71c5240685f128ce1fe3d03a72f52908e6a9d52.tar.gz
org.eclipse.tcf-e71c5240685f128ce1fe3d03a72f52908e6a9d52.tar.xz
org.eclipse.tcf-e71c5240685f128ce1fe3d03a72f52908e6a9d52.zip
Target Explorer: Add default comparator to read extensions in a predictable order (sorted)
-rw-r--r--target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/AbstractExtensionPointManager.java49
-rw-r--r--target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/ExtensionPointComparator.java68
2 files changed, 116 insertions, 1 deletions
diff --git a/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/AbstractExtensionPointManager.java b/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/AbstractExtensionPointManager.java
index d90270ad1..20b181269 100644
--- a/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/AbstractExtensionPointManager.java
+++ b/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/AbstractExtensionPointManager.java
@@ -9,7 +9,11 @@
*******************************************************************************/
package org.eclipse.tm.te.runtime.extensions;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
@@ -33,6 +37,8 @@ public abstract class AbstractExtensionPointManager<V> {
private boolean initialized = false;
// The map of loaded extension listed by their unique id's
private Map<String, ExecutableExtensionProxy<V>> extensionsMap = new LinkedHashMap<String, ExecutableExtensionProxy<V>>();
+ // The extension point comparator
+ private ExtensionPointComparator comparator = null;
/**
* Constructor.
@@ -78,6 +84,47 @@ public abstract class AbstractExtensionPointManager<V> {
}
/**
+ * Returns the extensions of the specified extension point sorted.
+ * <p>
+ * For the order of the extensions, see {@link ExtensionPointComparator}.
+ *
+ * @param point The extension point. Must be not <code>null</code>.
+ * @return The extensions in sorted order or an empty array if the extension point has no extensions.
+ */
+ protected IExtension[] getExtensionsSorted(IExtensionPoint point) {
+ assert point != null;
+
+ List<IExtension> extensions = new ArrayList<IExtension>(Arrays.asList(point.getExtensions()));
+ if (extensions.size() > 0) {
+ Collections.sort(extensions, getExtensionPointComparator());
+ }
+
+ return extensions.toArray(new IExtension[extensions.size()]);
+ }
+
+ /**
+ * Returns the extension point comparator instance. If not available,
+ * {@link #doCreateExtensionPointComparator()} is called to create a new instance.
+ *
+ * @return The extension point comparator or <code>null</code> if the instance creation fails.
+ */
+ protected final ExtensionPointComparator getExtensionPointComparator() {
+ if (comparator == null) {
+ comparator = doCreateExtensionPointComparator();
+ }
+ return comparator;
+ }
+
+ /**
+ * Creates a new extension point comparator instance.
+ *
+ * @return The extension point comparator instance.
+ */
+ protected ExtensionPointComparator doCreateExtensionPointComparator() {
+ return new ExtensionPointComparator();
+ }
+
+ /**
* Returns the extension point id to read. The method
* must return never <code>null</code>.
*
@@ -144,7 +191,7 @@ public abstract class AbstractExtensionPointManager<V> {
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry.getExtensionPoint(getExtensionPointId());
if (point != null) {
- IExtension[] extensions = point.getExtensions();
+ IExtension[] extensions = getExtensionsSorted(point);
for (IExtension extension : extensions) {
IConfigurationElement[] elements = extension.getConfigurationElements();
for (IConfigurationElement element : elements) {
diff --git a/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/ExtensionPointComparator.java b/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/ExtensionPointComparator.java
new file mode 100644
index 000000000..2af3730ed
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tm.te.runtime/src/org/eclipse/tm/te/runtime/extensions/ExtensionPointComparator.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Wind River Systems, Inc. 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:
+ * Wind River Systems - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tm.te.runtime.extensions;
+
+import java.util.Comparator;
+
+import org.eclipse.core.runtime.IExtension;
+
+/**
+ * Extension point comparator implementation.
+ * <p>
+ * The comparator assure that extension are read in a predictable order.
+ * <p>
+ * The order of the extensions is defined as following:<br>
+ * <ul><li>Extensions contributed by Target Explorer plug-ins (<code>org.eclipse.tm.te.*</code>)
+ * in ascending alphabetic order and</li>
+ * <li>Extensions contributed by any other plug-in in ascending alphabetic order.</li>
+ * <li>Extensions contributed by the same plug-in in ascending alphabetic order by the
+ * extensions unique id</li>
+ */
+public class ExtensionPointComparator implements Comparator<IExtension> {
+ private final static String TARGET_EXPLORER_PLUGINS_PATTERN = "org.eclipse.tm.te."; //$NON-NLS-1$
+
+ /* (non-Javadoc)
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ @Override
+ public int compare(IExtension o1, IExtension o2) {
+ // We ignore any comparisation with null and
+ if (o1 == null || o2 == null) return 0;
+ // Check if it is the exact same element
+ if (o1 == o2) return 0;
+
+ // The extensions are compared by the unique id of the contributing plugin first
+ String contributor1 = o1.getContributor().getName();
+ String contributor2 = o2.getContributor().getName();
+
+ // Contributions from Target Explorer plug-ins comes before 3rdParty plug-ins
+ if (contributor1.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN) && !contributor2.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN))
+ return -1;
+ if (!contributor1.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN) && contributor2.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN))
+ return 1;
+ if (contributor1.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN) && contributor2.startsWith(TARGET_EXPLORER_PLUGINS_PATTERN)) {
+ int value = contributor1.compareTo(contributor2);
+ // Within the same plug-in, the extension are sorted by their unique id (if available)
+ if (value == 0 && o1.getUniqueIdentifier() != null && o2.getUniqueIdentifier() != null)
+ return o1.getUniqueIdentifier().compareTo(o2.getUniqueIdentifier());
+ // Otherwise, just return the comparisation result from the contributors
+ return value;
+ }
+
+ // Contributions from all other plug-ins are sorted alphabetical
+ int value = contributor1.compareTo(contributor2);
+ // Within the same plug-in, the extension are sorted by their unique id (if available)
+ if (value == 0 && o1.getUniqueIdentifier() != null && o2.getUniqueIdentifier() != null)
+ return o1.getUniqueIdentifier().compareTo(o2.getUniqueIdentifier());
+ // Otherwise, just return the comparisation result from the contributors
+ return value;
+ }
+
+}

Back to the top