KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > PluginValidationOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.launcher;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
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 JavaDoc[] NO_CHILDREN = new Object JavaDoc[0];
47     
48     private MinimalState fState;
49     private ArrayList JavaDoc fInvalidModels = new ArrayList JavaDoc();
50     private String JavaDoc fProductID;
51     private String JavaDoc fApplicationID;
52
53     private IPluginModelBase[] fModels;
54
55     class InvalidNode {
56         public String JavaDoc 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 JavaDoc toString() {
65             return NLS.bind(PDEUIMessages.PluginValidationOperation_missingCore, "org.eclipse.osgi"); //$NON-NLS-1$
66
}
67     }
68     
69     class MissingApplication {
70         public String JavaDoc toString() {
71             String JavaDoc pluginID = getApplicationPlugin();
72             if (getState().getBundles(pluginID).length == 0)
73                 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingApp, (new String JavaDoc[] {fApplicationID, pluginID}));
74             return NLS.bind(PDEUIMessages.PluginValidationOperation_missingApp2, (new String JavaDoc[] {fApplicationID, pluginID}));
75         }
76     }
77     
78     class MissingProduct {
79         public String JavaDoc toString() {
80             String JavaDoc pluginID = getProductPlugin();
81             if (getState().getBundles(pluginID).length == 0)
82                 return NLS.bind(PDEUIMessages.PluginValidationOperation_missingProduct, (new String JavaDoc[] {fProductID, pluginID}));
83             return NLS.bind(PDEUIMessages.PluginValidationOperation_missingProduct2, (new String JavaDoc[] {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         /* (non-Javadoc)
96          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
97          */

98         public String JavaDoc getText(Object JavaDoc element) {
99             if (element instanceof BundleDescription) {
100                 String JavaDoc 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 JavaDoc toString(ResolverError error) {
117             int type = error.getType();
118             VersionConstraint constraint = error.getUnsatisfiedConstraint();
119             switch (type) {
120             case ResolverError.PLATFORM_FILTER:
121                 String JavaDoc filter = error.getBundle().getPlatformFilter();
122                 return NLS.bind(PDEUIMessages.PluginValidationOperation_platformFilter, filter);
123             case ResolverError.MISSING_EXECUTION_ENVIRONMENT:
124                 String JavaDoc[] 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 JavaDoc toString(BundleSpecification spec, int type) {
141             String JavaDoc 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 JavaDoc[] {spec.getVersionRange().toString(), spec.getName()});
154         }
155         
156         private String JavaDoc 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 JavaDoc toString(HostSpecification spec) {
163             String JavaDoc 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 JavaDoc[] {spec.getVersionRange().toString(), spec.getName()});
173         }
174
175         /* (non-Javadoc)
176          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
177          */

178         public Image getImage(Object JavaDoc element) {
179             if (element instanceof IPluginModelBase)
180                 return super.getImage(element);
181             return fImage;
182         }
183         
184         /* (non-Javadoc)
185          * @see org.eclipse.jface.viewers.LabelProvider#dispose()
186          */

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         /* (non-Javadoc)
196          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
197          */

198         public Object JavaDoc[] getChildren(Object JavaDoc 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         /* (non-Javadoc)
206          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
207          */

208         public Object JavaDoc getParent(Object JavaDoc element) {
209             return null;
210         }
211
212         /* (non-Javadoc)
213          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
214          */

215         public boolean hasChildren(Object JavaDoc element) {
216             return element instanceof BundleDescription || element instanceof InvalidNode;
217         }
218
219         /* (non-Javadoc)
220          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
221          */

222         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
223             ArrayList JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc)null);
256             } else {
257                 String JavaDoc appToRun = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, (String JavaDoc)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     /* (non-Javadoc)
267      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
268      */

269     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
270             InterruptedException JavaDoc {
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 JavaDoc getProductPlugin() {
312         return fProductID.substring(0, fProductID.lastIndexOf('.'));
313     }
314     
315     private String JavaDoc getApplicationPlugin() {
316         return fApplicationID.substring(0, fApplicationID.lastIndexOf('.'));
317     }
318     
319     private boolean isCoreMissing() {
320         return (getState().getBundles("org.eclipse.osgi").length == 0); //$NON-NLS-1$
321
}
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