1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.util.Arrays ; 14 import java.util.Comparator ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtensionRegistry; 21 import org.eclipse.core.runtime.IRegistryChangeEvent; 22 import org.eclipse.core.runtime.IRegistryChangeListener; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.debug.core.model.LaunchConfigurationDelegate; 25 import org.eclipse.jface.preference.IPreferenceStore; 26 import org.eclipse.pde.internal.ui.IPreferenceConstants; 27 import org.eclipse.pde.internal.ui.PDEPlugin; 28 import org.eclipse.pde.ui.launcher.OSGiLaunchConfigurationInitializer; 29 30 public class OSGiFrameworkManager implements IRegistryChangeListener { 31 32 public static final String POINT_ID = "org.eclipse.pde.ui.osgiFrameworks"; public static final String DEFAULT_FRAMEWORK = "org.eclipse.pde.ui.EquinoxFramework"; 35 public static final String ATT_ID = "id"; public static final String ATT_NAME = "name"; public static final String ATT_DELEGATE = "launcherDelegate"; public static final String ATT_INITIALIZER = "initializer"; 40 public static final String ELEMENT_FRAMEWORK = "framework"; 42 private Map fFrameworks; 43 44 public IConfigurationElement[] getFrameworks() { 45 if (fFrameworks == null) 46 loadElements(); 47 return (IConfigurationElement[])fFrameworks.values().toArray(new IConfigurationElement[fFrameworks.size()]); 48 } 49 50 public IConfigurationElement[] getSortedFrameworks() { 51 IConfigurationElement[] elements = getFrameworks(); 52 return orderElements(elements); 53 } 54 55 private void loadElements() { 56 fFrameworks = new HashMap (); 57 IExtensionRegistry registry = Platform.getExtensionRegistry(); 58 IConfigurationElement[] elements = registry.getConfigurationElementsFor(POINT_ID); 59 for (int i = 0; i < elements.length; i++) { 60 String id = elements[i].getAttribute(ATT_ID); 61 if (id == null 62 || elements[i].getAttribute(ATT_NAME) == null 63 || elements[i].getAttribute(ATT_DELEGATE) == null) 64 continue; 65 fFrameworks.put(id, elements[i]); 66 } 67 } 68 69 private IConfigurationElement[] orderElements(IConfigurationElement[] elems) { 70 Arrays.sort(elems, new Comparator () { 71 public int compare(Object o1, Object o2) { 72 String name1 = ((IConfigurationElement)o1).getAttribute(ATT_NAME); 73 String name2 = ((IConfigurationElement)o2).getAttribute(ATT_NAME); 74 if (name1 != null) 75 return name1.compareToIgnoreCase(name2); 76 return 1; 77 } 78 }); 79 return elems; 80 } 81 82 83 public void registryChanged(IRegistryChangeEvent event) { 84 } 86 87 public String getDefaultFramework() { 88 IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore(); 89 return store.getString(IPreferenceConstants.DEFAULT_OSGI_FRAMEOWRK); 90 } 91 92 public OSGiLaunchConfigurationInitializer getDefaultInitializer() { 93 return getInitializer(getDefaultFramework()); 94 } 95 96 public OSGiLaunchConfigurationInitializer getInitializer(String frameworkID) { 97 if (fFrameworks == null) 98 loadElements(); 99 if (fFrameworks.containsKey(frameworkID)) { 100 try { 101 IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID); 102 if (element.getAttribute(ATT_INITIALIZER) != null) { 103 Object result = element.createExecutableExtension(ATT_INITIALIZER); 104 if (result instanceof OSGiLaunchConfigurationInitializer) 105 return (OSGiLaunchConfigurationInitializer)result; 106 } 107 } catch (CoreException e) { 108 } 109 } 110 return new OSGiLaunchConfigurationInitializer(); 111 } 112 113 public LaunchConfigurationDelegate getFrameworkLauncher(String frameworkID) { 114 if (fFrameworks == null) 115 loadElements(); 116 if (fFrameworks.containsKey(frameworkID)) { 117 try { 118 IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID); 119 Object result = element.createExecutableExtension(ATT_DELEGATE); 120 if (result instanceof LaunchConfigurationDelegate) 121 return (LaunchConfigurationDelegate)result; 122 } catch (CoreException e) { 123 } 124 } 125 return null; 126 } 127 128 public String getFrameworkName(String frameworkID) { 129 if (fFrameworks == null) 130 loadElements(); 131 if (fFrameworks.containsKey(frameworkID)) { 132 IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID); 133 return element.getAttribute(ATT_NAME); 134 } 135 return null; 136 } 137 138 139 } 140 | Popular Tags |