1 11 package org.eclipse.ui.internal; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.jface.util.SafeRunnable; 23 import org.eclipse.ui.IStartup; 24 import org.osgi.framework.Bundle; 25 26 33 public class EarlyStartupRunnable extends SafeRunnable { 34 35 private static final String EXTENSION_CLASS = "org.eclipse.core.runtime.IExtension"; 37 39 private static final String GET_PLUGIN_METHOD = "getPlugin"; 41 private static final String GET_DESC_METHOD = "getDeclaringPluginDescriptor"; 43 private static final String PI_RUNTIME_COMPATIBILITY = "org.eclipse.core.runtime.compatibility"; 45 private IExtension extension; 46 47 51 public EarlyStartupRunnable(IExtension extension) { 52 this.extension = extension; 53 } 54 55 public void run() throws Exception { 56 IConfigurationElement[] configElements = extension 57 .getConfigurationElements(); 58 59 boolean foundAtLeastOne = false; 61 for (int i = 0; i < configElements.length; ++i) { 62 IConfigurationElement element = configElements[i]; 63 if (element != null 64 && element.getName() 65 .equals(IWorkbenchConstants.TAG_STARTUP)) { 66 runEarlyStartup(getExecutableExtension(element)); 67 foundAtLeastOne = true; 68 } 69 } 70 71 if (!foundAtLeastOne) { 73 runEarlyStartup(getPluginForCompatibility()); 74 } 75 } 76 77 public void handleException(Throwable exception) { 78 IStatus status = new Status(IStatus.ERROR, extension.getNamespace(), 0, 79 "Unable to execute early startup code for an extension", exception); 81 WorkbenchPlugin.log("Unhandled Exception", status); } 83 84 private void runEarlyStartup(Object executableExtension) { 85 if (executableExtension != null 86 && executableExtension instanceof IStartup) { 87 ((IStartup) executableExtension).earlyStartup(); 88 } else { 89 IStatus status = new Status(IStatus.ERROR, 90 extension.getNamespace(), 0, 91 "startup class must implement org.eclipse.ui.IStartup", null); 93 WorkbenchPlugin.log("Bad extension specification", status); } 95 } 96 97 105 private Object getExecutableExtension(IConfigurationElement element) 106 throws CoreException { 107 108 String classname = element.getAttribute(IWorkbenchConstants.TAG_CLASS); 109 110 if (classname == null || classname.length() <= 0) { 113 return getPluginForCompatibility(); 114 } 115 116 return WorkbenchPlugin.createExtension(element, 118 IWorkbenchConstants.TAG_CLASS); 119 } 120 121 126 private Object getPluginForCompatibility() { 127 Bundle compatBundle = Platform.getBundle(PI_RUNTIME_COMPATIBILITY); 129 if (compatBundle == null) { 130 return null; 131 } 132 133 try { 135 Class extensionClass = compatBundle.loadClass(EXTENSION_CLASS); 138 Method getDescMethod = extensionClass.getDeclaredMethod( 139 GET_DESC_METHOD, new Class [0]); 140 Object pluginDesc = getDescMethod.invoke(extension, new Object [0]); 141 if (pluginDesc == null) { 142 return null; 143 } 144 145 Class pluginDescClass = pluginDesc.getClass(); 147 Method getPluginMethod = pluginDescClass.getDeclaredMethod( 148 GET_PLUGIN_METHOD, new Class [0]); 149 return getPluginMethod.invoke(pluginDesc, new Object [0]); 150 } catch (ClassNotFoundException e) { 151 handleException(e); 152 } catch (IllegalAccessException e) { 153 handleException(e); 154 } catch (InvocationTargetException e) { 155 handleException(e); 156 } catch (NoSuchMethodException e) { 157 handleException(e); 158 } 159 160 return null; 161 } 162 } 163 | Popular Tags |