1 11 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.osgi.framework.Bundle; 25 import org.osgi.framework.BundleException; 26 import org.osgi.framework.Constants; 27 28 import org.eclipse.core.runtime.Assert; 29 import org.eclipse.core.runtime.IConfigurationElement; 30 import org.eclipse.core.runtime.IExtension; 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.Platform; 33 import org.eclipse.core.runtime.Status; 34 35 import org.eclipse.osgi.util.ManifestElement; 36 37 38 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 39 40 49 public abstract class ConfigurationElementSorter { 50 51 57 public final void sort(Object [] elements) { 58 Arrays.sort(elements, new ConfigurationElementComparator(elements)); 59 } 60 61 67 public abstract IConfigurationElement getConfigurationElement(Object object); 68 69 73 private class ConfigurationElementComparator implements Comparator { 74 75 private Map fDescriptorMapping; 76 private Map fPrereqsMapping; 77 78 public ConfigurationElementComparator(Object [] elements) { 79 Assert.isNotNull(elements); 80 initialize(elements); 81 } 82 83 87 public int compare(Object object0, Object object1) { 88 89 if (dependsOn(object0, object1)) 90 return -1; 91 92 if (dependsOn(object1, object0)) 93 return +1; 94 95 return 0; 96 } 97 98 107 private boolean dependsOn(Object element0, Object element1) { 108 if (element0 == null || element1 == null) 109 return false; 110 111 String pluginDesc0= (String )fDescriptorMapping.get(element0); 112 String pluginDesc1= (String )fDescriptorMapping.get(element1); 113 114 if (pluginDesc0.equals(pluginDesc1)) 116 return false; 117 118 Set prereqUIds0= (Set )fPrereqsMapping.get(pluginDesc0); 119 120 return prereqUIds0.contains(pluginDesc1); 121 } 122 123 128 private void initialize(Object [] elements) { 129 int length= elements.length; 130 fDescriptorMapping= new HashMap (length); 131 fPrereqsMapping= new HashMap (length); 132 Set fBundleSet= new HashSet (length); 133 134 for (int i= 0; i < length; i++) { 135 IConfigurationElement configElement= getConfigurationElement(elements[i]); 136 Bundle bundle= Platform.getBundle(configElement.getContributor().getName()); 137 fDescriptorMapping.put(elements[i], bundle.getSymbolicName()); 138 fBundleSet.add(bundle); 139 } 140 141 Iterator iter= fBundleSet.iterator(); 142 while (iter.hasNext()) { 143 Bundle bundle= (Bundle)iter.next(); 144 List toTest= new ArrayList (fBundleSet); 145 toTest.remove(bundle); 146 Set prereqUIds= new HashSet (Math.max(0, toTest.size() - 1)); 147 fPrereqsMapping.put(bundle.getSymbolicName(), prereqUIds); 148 149 String requires = (String )bundle.getHeaders().get(Constants.REQUIRE_BUNDLE); 150 ManifestElement[] manifestElements; 151 try { 152 manifestElements = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, requires); 153 } catch (BundleException e) { 154 String uid= getExtensionPointUniqueIdentifier(bundle); 155 String message= "ConfigurationElementSorter for '" + uid + "': getting required plug-ins for '" + bundle.getSymbolicName() + "' failed"; Status status= new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, e); 157 TextEditorPlugin.getDefault().getLog().log(status); 158 continue; 159 } 160 161 if (manifestElements == null) 162 continue; 163 164 int i= 0; 165 while (i < manifestElements.length && !toTest.isEmpty()) { 166 String prereqUId= manifestElements[i].getValue(); 167 for (int j= 0; j < toTest.size();) { 168 Bundle toTest_j= (Bundle)toTest.get(j); 169 if (toTest_j.getSymbolicName().equals(prereqUId)) { 170 toTest.remove(toTest_j); 171 prereqUIds.add(toTest_j.getSymbolicName()); 172 } else 173 j++; 174 } 175 i++; 176 } 177 } 178 } 179 180 188 private String getExtensionPointUniqueIdentifier(Bundle bundle) { 189 if (bundle != null) { 190 String bundleName= bundle.getSymbolicName(); 191 if (bundleName != null) { 192 Set entries= fDescriptorMapping.entrySet(); 193 Iterator iter= entries.iterator(); 194 while (iter.hasNext()) { 195 Map.Entry entry= (Map.Entry )iter.next(); 196 if (bundleName.equals(entry.getValue())) { 197 IExtension extension = getConfigurationElement(entry.getKey()).getDeclaringExtension(); 198 return extension.getExtensionPointUniqueIdentifier(); 199 } 200 } 201 } 202 } 203 return "unknown"; } 205 206 } 207 } 208 | Popular Tags |