1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.debug.core.ILaunchConfiguration; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.viewers.IContentProvider; 21 import org.eclipse.jface.viewers.ILabelProvider; 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.osgi.service.resolver.BundleDescription; 24 import org.eclipse.osgi.service.resolver.BundleSpecification; 25 import org.eclipse.osgi.service.resolver.HostSpecification; 26 import org.eclipse.osgi.service.resolver.ImportPackageSpecification; 27 import org.eclipse.osgi.service.resolver.ResolverError; 28 import org.eclipse.osgi.service.resolver.State; 29 import org.eclipse.osgi.service.resolver.VersionConstraint; 30 import org.eclipse.osgi.service.resolver.VersionRange; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.pde.core.plugin.IPluginModelBase; 33 import org.eclipse.pde.internal.core.MinimalState; 34 import org.eclipse.pde.internal.core.TargetPlatform; 35 import org.eclipse.pde.internal.ui.PDELabelProvider; 36 import org.eclipse.pde.internal.ui.PDEPlugin; 37 import org.eclipse.pde.internal.ui.PDEPluginImages; 38 import org.eclipse.pde.internal.ui.PDEUIMessages; 39 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 40 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 41 import org.eclipse.swt.graphics.Image; 42 43 44 public class PluginValidationOperation implements IRunnableWithProgress { 45 46 private static Object [] NO_CHILDREN = new Object [0]; 47 48 private MinimalState fState; 49 private ArrayList fInvalidModels = new ArrayList (); 50 private String fProductID; 51 private String fApplicationID; 52 53 private IPluginModelBase[] fModels; 54 55 class InvalidNode { 56 public String toString() { 57 if (fInvalidModels.size() > 1) 58 return PDEUIMessages.PluginValidationOperation_invalidPlural; 59 return PDEUIMessages.PluginValidationOperation_invalidSingular; 60 } 61 } 62 63 class MissingCore { 64 public String toString() { 65 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingCore, "org.eclipse.osgi"); } 67 } 68 69 class MissingApplication { 70 public String toString() { 71 String pluginID = getApplicationPlugin(); 72 if (getState().getBundles(pluginID).length == 0) 73 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingApp, (new String [] {fApplicationID, pluginID})); 74 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingApp2, (new String [] {fApplicationID, pluginID})); 75 } 76 } 77 78 class MissingProduct { 79 public String toString() { 80 String pluginID = getProductPlugin(); 81 if (getState().getBundles(pluginID).length == 0) 82 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingProduct, (new String [] {fProductID, pluginID})); 83 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingProduct2, (new String [] {fProductID, pluginID})); 84 } 85 } 86 87 class ConstraintLabelProvider extends PDELabelProvider { 88 89 private Image fImage; 90 91 public ConstraintLabelProvider() { 92 PDEPlugin.getDefault().getLabelProvider().connect(this); 93 fImage = PDEPluginImages.DESC_ERROR_ST_OBJ.createImage(); 94 } 95 98 public String getText(Object element) { 99 if (element instanceof BundleDescription) { 100 String id = ((BundleDescription)element).getSymbolicName(); 101 if (((BundleDescription)element).getHost() != null) 102 return NLS.bind(PDEUIMessages.PluginValidationOperation_disableFragment, id); 103 return NLS.bind(PDEUIMessages.PluginValidationOperation_disablePlugin, id); 104 } 105 106 if (element instanceof ResolverError) 107 return toString((ResolverError)element); 108 109 if (element instanceof IPluginModelBase) { 110 IPluginModelBase model = (IPluginModelBase)element; 111 return model.getPluginBase().getId(); 112 } 113 return element.toString(); 114 } 115 116 private String toString(ResolverError error) { 117 int type = error.getType(); 118 VersionConstraint constraint = error.getUnsatisfiedConstraint(); 119 switch (type) { 120 case ResolverError.PLATFORM_FILTER: 121 String filter = error.getBundle().getPlatformFilter(); 122 return NLS.bind(PDEUIMessages.PluginValidationOperation_platformFilter, filter); 123 case ResolverError.MISSING_EXECUTION_ENVIRONMENT: 124 String [] ee = error.getBundle().getExecutionEnvironments(); 125 return NLS.bind(PDEUIMessages.PluginValidationOperation_ee, ee[0]); 126 case ResolverError.SINGLETON_SELECTION: 127 return PDEUIMessages.PluginValidationOperation_singleton; 128 case ResolverError.IMPORT_PACKAGE_USES_CONFLICT: 129 case ResolverError.MISSING_IMPORT_PACKAGE : 130 return toString((ImportPackageSpecification)constraint, type); 131 case ResolverError.REQUIRE_BUNDLE_USES_CONFLICT: 132 case ResolverError.MISSING_REQUIRE_BUNDLE : 133 return toString((BundleSpecification)constraint, type); 134 case ResolverError.MISSING_FRAGMENT_HOST : 135 return toString((HostSpecification)constraint); 136 } 137 return error.toString(); 138 } 139 140 private String toString(BundleSpecification spec, int type) { 141 String name = spec.getName(); 142 if (type == ResolverError.REQUIRE_BUNDLE_USES_CONFLICT) 143 return NLS.bind(PDEUIMessages.PluginValidationOperation_bundle_uses, spec.getName()); 144 145 BundleDescription[] bundles = getState().getBundles(name); 146 for (int i = 0; i < bundles.length; i++) { 147 if (spec.isSatisfiedBy(bundles[i]) && !bundles[i].isResolved()) 148 return NLS.bind(PDEUIMessages.PluginValidationOperation_disabledRequired, name); 149 } 150 if (bundles.length == 0 || spec.getVersionRange().equals(VersionRange.emptyRange)) 151 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingRequired, name); 152 return NLS.bind(PDEUIMessages.PluginValidationOperation_version, 153 new String [] {spec.getVersionRange().toString(), spec.getName()}); 154 } 155 156 private String toString(ImportPackageSpecification spec, int type) { 157 if (type == ResolverError.IMPORT_PACKAGE_USES_CONFLICT) 158 return NLS.bind(PDEUIMessages.PluginValidationOperation_import_uses, spec.getName()); 159 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingImport, spec.getName()); 160 } 161 162 private String toString(HostSpecification spec) { 163 String name = spec.getName(); 164 BundleDescription[] bundles = getState().getBundles(name); 165 for (int i = 0; i < bundles.length; i++) { 166 if (spec.isSatisfiedBy(bundles[i]) && !bundles[i].isResolved()) 167 return NLS.bind(PDEUIMessages.PluginValidationOperation_disabledParent, name); 168 } 169 if (bundles.length == 0 || spec.getVersionRange().equals(VersionRange.emptyRange)) 170 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingParent, name); 171 return NLS.bind(PDEUIMessages.PluginValidationOperation_hostVersion, 172 new String [] {spec.getVersionRange().toString(), spec.getName()}); 173 } 174 175 178 public Image getImage(Object element) { 179 if (element instanceof IPluginModelBase) 180 return super.getImage(element); 181 return fImage; 182 } 183 184 187 public void dispose() { 188 fImage.dispose(); 189 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 190 } 191 } 192 193 class ContentProvider extends DefaultContentProvider implements ITreeContentProvider { 194 195 198 public Object [] getChildren(Object parent) { 199 if (parent instanceof BundleDescription) 200 return getState().getResolverErrors((BundleDescription)parent); 201 202 return (parent instanceof InvalidNode) ? fInvalidModels.toArray() : NO_CHILDREN; 203 } 204 205 208 public Object getParent(Object element) { 209 return null; 210 } 211 212 215 public boolean hasChildren(Object element) { 216 return element instanceof BundleDescription || element instanceof InvalidNode; 217 } 218 219 222 public Object [] getElements(Object inputElement) { 223 ArrayList result = new ArrayList (); 224 BundleDescription[] all = getState().getBundles(); 225 for (int i = 0; i < all.length; i++) { 226 if (!all[i].isResolved()) 227 result.add(all[i]); 228 } 229 if (isProductMissing()) 230 result.add(new MissingProduct()); 231 if (isApplicationMissing()) 232 result.add(new MissingApplication()); 233 if (isCoreMissing()) 234 result.add(new MissingCore()); 235 if (fInvalidModels.size() > 0) 236 result.add(new InvalidNode()); 237 return result.toArray(); 238 } 239 } 240 241 public PluginValidationOperation(ILaunchConfiguration configuration) { 242 fState = new MinimalState(TargetPlatform.getTargetEnvironment()); 243 initialize(configuration); 244 } 245 246 public PluginValidationOperation(IPluginModelBase[] models) { 247 fState = new MinimalState(TargetPlatform.getTargetEnvironment()); 248 fModels = models; 249 } 250 251 private void initialize(ILaunchConfiguration configuration) { 252 try { 253 fModels = LaunchPluginValidator.getPluginList(configuration); 254 if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 255 fProductID = configuration.getAttribute(IPDELauncherConstants.PRODUCT, (String )null); 256 } else { 257 String appToRun = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, (String )null); 258 if(appToRun == null) 259 appToRun = configuration.getAttribute(IPDELauncherConstants.APPLICATION, LaunchConfigurationHelper.getDefaultApplicationName()); 260 fApplicationID = JUnitLaunchConfiguration.CORE_APPLICATION.equals(appToRun) ? null : appToRun; 261 } 262 } catch (CoreException e) { 263 } 264 } 265 266 269 public void run(IProgressMonitor monitor) throws InvocationTargetException , 270 InterruptedException { 271 for (int i = 0; i < fModels.length; i++) { 272 if (fState.addBundle(fModels[i], -1) == null) 273 fInvalidModels.add(fModels[i]); 274 } 275 fState.resolveState(false); 276 } 277 278 public State getState() { 279 return fState.getState(); 280 } 281 282 public boolean hasErrors() { 283 State state = getState(); 284 if (fInvalidModels.size() > 0 || state.getBundles().length > state.getResolvedBundles().length) 285 return true; 286 return isApplicationMissing() || isProductMissing(); 287 } 288 289 private boolean isProductMissing() { 290 if (fProductID == null) 291 return false; 292 293 BundleDescription[] desc = getState().getBundles(getProductPlugin()); 294 for (int i = 0; i < desc.length; i++) { 295 if (desc[i].isResolved()) 296 return false; 297 } 298 return true; 299 } 300 private boolean isApplicationMissing() { 301 if (fApplicationID == null) 302 return false; 303 BundleDescription[] desc = getState().getBundles(getApplicationPlugin()); 304 for (int i = 0; i < desc.length; i++) { 305 if (desc[i].isResolved()) 306 return false; 307 } 308 return true; 309 } 310 311 private String getProductPlugin() { 312 return fProductID.substring(0, fProductID.lastIndexOf('.')); 313 } 314 315 private String getApplicationPlugin() { 316 return fApplicationID.substring(0, fApplicationID.lastIndexOf('.')); 317 } 318 319 private boolean isCoreMissing() { 320 return (getState().getBundles("org.eclipse.osgi").length == 0); } 322 323 public IContentProvider getContentProvider() { 324 return new ContentProvider(); 325 } 326 327 public ILabelProvider getLabelProvider() { 328 return new ConstraintLabelProvider(); 329 } 330 331 } 332 | Popular Tags |