1 11 package org.eclipse.ui.internal.components; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.ui.PlatformUI; 20 import org.eclipse.ui.internal.components.framework.ClassIdentifier; 21 import org.eclipse.ui.internal.components.framework.ComponentException; 22 import org.eclipse.ui.internal.components.framework.ServiceFactory; 23 import org.eclipse.ui.internal.components.registry.ComponentRegistry; 24 import org.eclipse.ui.internal.components.registry.ComponentScope; 25 import org.eclipse.ui.internal.components.registry.ExtensionPointManager; 26 import org.eclipse.ui.internal.components.registry.IComponentScope; 27 import org.eclipse.ui.internal.components.registry.ServiceExtensionPoint; 28 import org.eclipse.ui.internal.misc.StatusUtil; 29 import org.osgi.framework.Bundle; 30 31 32 37 public class ComponentUtil { 38 39 public static final Object [] EMPTY_ARRAY = new Object [0]; 40 41 private static ComponentRegistry registry; 42 private static ExtensionPointManager uiExtensionPoints = null; 43 private static ServiceExtensionPoint partServices = null; 44 45 46 private ComponentUtil() { 47 } 48 49 public static ComponentRegistry getRegistry() { 50 if (registry != null) { 51 return registry; 52 } 53 registry = new ComponentRegistry(); 54 Bundle uiBundle = Platform.getBundle(PlatformUI.PLUGIN_ID); 55 uiExtensionPoints = new ExtensionPointManager(uiBundle); 56 partServices = new ServiceExtensionPoint(uiExtensionPoints, ComponentUtil.registry); 57 58 return registry; 59 } 60 61 public static void disposeRegistry() { 62 if (partServices != null) { 63 partServices.dispose(); 64 partServices = null; 65 } 66 67 if (uiExtensionPoints != null) { 68 uiExtensionPoints.dispose(); 69 uiExtensionPoints = null; 70 } 71 72 registry = null; 73 } 74 75 public static String getSimpleClassName(String fullyQualifiedClassName) { 76 int idx = fullyQualifiedClassName.lastIndexOf('.') + 1; 77 78 return fullyQualifiedClassName.substring(idx, fullyQualifiedClassName.length()); 79 } 80 81 public static Throwable getCause(Throwable toQuery) { 82 return StatusUtil.getCause(toQuery); 83 } 84 85 public static Class loadClass(ClassIdentifier type) throws ComponentException { 86 try { 87 Bundle pluginBundle = Platform.getBundle(type.getNamespace()); 88 Class result = pluginBundle.loadClass(type.getTypeName()); 89 return result; 90 } catch (ClassNotFoundException e) { 91 throw new ComponentException(type.getTypeName(), 92 NLS.bind(ComponentMessages.ComponentUtil_class_not_found, 93 type.getNamespace(), type.getTypeName()), null); 94 } 95 } 96 97 public static final ClassIdentifier getClassFromInitializationData(IConfigurationElement config, Object data) throws CoreException { 98 99 if (!(data instanceof String )) { 100 String msg = NLS.bind( 101 ComponentMessages.ReflectionFactory_missing_data, config.toString()); 102 103 throw new CoreException(new Status(IStatus.ERROR, config.getNamespace(), Status.OK, 104 msg, null)); 105 } 106 107 return new ClassIdentifier(config.getNamespace(), (String )data); 108 } 109 110 public static String getMessage(Throwable toQuery) { 111 String msg = toQuery.getMessage(); 112 113 if (msg == null) { 114 msg = toQuery.toString(); 115 } 116 117 return msg; 118 } 119 120 149 public static String getAttribute(IConfigurationElement element, String attributeId) throws CoreException { 150 String result = element.getAttribute(attributeId); 151 if (result == null) { 152 throw new CoreException(new Status(IStatus.ERROR, element.getNamespace(), 153 IStatus.OK, NLS.bind(ComponentMessages.ComponentUtil_missing_attribute, 154 new Object [] {element.getName(), attributeId, 155 element.getDeclaringExtension().getExtensionPointUniqueIdentifier()}), null 156 )); 157 } 158 159 return result; 160 } 161 162 public static Throwable getMostSpecificCause(Throwable exception) { 163 return getMostSpecificException(getCause(exception)); 164 } 165 166 172 private static Throwable getMostSpecificException(Throwable exception) { 173 Throwable cause = getCause(exception); 174 if (cause == null || cause == exception) { 175 return exception; 176 } 177 178 return getMostSpecificException(cause); 179 } 180 181 185 public static Object createInstance(Class implementation) throws ComponentException { 186 try { 187 return implementation.newInstance(); 188 } catch (InstantiationException e) { 189 throw new ComponentException(implementation, e); 190 } catch (IllegalAccessException e) { 191 throw new ComponentException(implementation, e); 192 } 193 } 194 195 209 public static IComponentScope getScope(String scope) { 210 return getRegistry().getScope(scope); 211 } 212 213 239 public static ServiceFactory getContext(String scope) { 240 IComponentScope s = getScope(scope); 241 242 if (s == null) { 243 return null; 244 } 245 246 return ((ComponentScope)s).getContext(); 247 } 248 249 250 251 } 252 | Popular Tags |