| 1 | package de.uka.ipd.sdq.codegen.simucontroller.runconfig; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.eclipse.core.runtime.CoreException; |
| 7 | import org.eclipse.core.runtime.IConfigurationElement; |
| 8 | import org.eclipse.core.runtime.IExtension; |
| 9 | import org.eclipse.core.runtime.Platform; |
| 10 | |
| 11 | /** |
| 12 | * Helper class for the extension point de.uka.ipd.sdq.codegen.simucontroller.simulator |
| 13 | * ("Palladio Simulator"). |
| 14 | * |
| 15 | * @author Philipp Merkle |
| 16 | * |
| 17 | */ |
| 18 | public class SimulatorExtensionHelper { |
| 19 | |
| 20 | /** the id for the "Palladio Simulator" extension point */ |
| 21 | public static final String EXTENSION_POINT_ID = "de.uka.ipd.sdq.codegen.simucontroller.simulator"; |
| 22 | |
| 23 | public static String[] getSimulatorNames() throws CoreException { |
| 24 | List<String> names = new ArrayList<String>(); |
| 25 | for (IExtension extension : loadSimulatorExtensions()) { |
| 26 | IConfigurationElement e = obtainConfigurationElement("simulator", extension); |
| 27 | if (e != null) { |
| 28 | names.add(e.getAttribute("name")); |
| 29 | } |
| 30 | } |
| 31 | return names.toArray(new String[names.size()]); |
| 32 | } |
| 33 | |
| 34 | public static String getSimulatorNameForId(String simulatorId) throws CoreException { |
| 35 | for (IExtension extension : loadSimulatorExtensions()) { |
| 36 | IConfigurationElement e = obtainConfigurationElement("simulator", extension); |
| 37 | if (e != null && e.getAttribute("id").equals(simulatorId)) { |
| 38 | return e.getAttribute("name"); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // could not find a simulator extension for the specified id |
| 43 | return ""; |
| 44 | } |
| 45 | |
| 46 | public static String getSimulatorIdForName(String simulatorName) throws CoreException { |
| 47 | for (IExtension extension : loadSimulatorExtensions()) { |
| 48 | IConfigurationElement e = obtainConfigurationElement("simulator", extension); |
| 49 | if (e != null && e.getAttribute("name").equals(simulatorName)) { |
| 50 | return e.getAttribute("id"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // could not find a simulator extension for the specified name |
| 55 | return ""; |
| 56 | } |
| 57 | |
| 58 | private static IConfigurationElement obtainConfigurationElement(String elementName, IExtension extension) |
| 59 | throws CoreException { |
| 60 | IConfigurationElement[] elements = extension.getConfigurationElements(); |
| 61 | for (IConfigurationElement element : elements) { |
| 62 | if (element.getName().equals(elementName)) { |
| 63 | return element; |
| 64 | } |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | private static List<IExtension> loadSimulatorExtensions() { |
| 70 | IExtension[] exts = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID).getExtensions(); |
| 71 | List<IExtension> results = new ArrayList<IExtension>(); |
| 72 | for (IExtension extension : exts) { |
| 73 | results.add(extension); |
| 74 | } |
| 75 | return results; |
| 76 | } |
| 77 | |
| 78 | } |