KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > MinimalState


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.Dictionary JavaDoc;
19 import java.util.jar.JarFile JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipFile JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.MultiStatus;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.osgi.service.pluginconversion.PluginConversionException;
30 import org.eclipse.osgi.service.pluginconversion.PluginConverter;
31 import org.eclipse.osgi.service.resolver.BundleDescription;
32 import org.eclipse.osgi.service.resolver.BundleSpecification;
33 import org.eclipse.osgi.service.resolver.HostSpecification;
34 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
35 import org.eclipse.osgi.service.resolver.State;
36 import org.eclipse.osgi.service.resolver.StateDelta;
37 import org.eclipse.osgi.service.resolver.StateHelper;
38 import org.eclipse.osgi.service.resolver.StateObjectFactory;
39 import org.eclipse.osgi.service.resolver.VersionConstraint;
40 import org.eclipse.osgi.service.resolver.VersionRange;
41 import org.eclipse.osgi.util.ManifestElement;
42 import org.eclipse.pde.core.plugin.IPluginModelBase;
43 import org.eclipse.pde.internal.core.util.Headers;
44 import org.osgi.framework.BundleException;
45 import org.osgi.framework.Constants;
46 import org.osgi.util.tracker.ServiceTracker;
47
48 public class MinimalState {
49
50     protected State fState;
51         
52     protected long fId;
53
54     private PluginConverter fConverter = null;
55
56     private boolean fEEListChanged = false; // indicates that the EE has changed
57
// this could be due to the system bundle changing location
58
// or initially when the ee list is first created.
59

60     private String JavaDoc[] fExecutionEnvironments; // an ordered list of known/supported execution environments
61

62     private boolean fNoProfile;
63
64     private static final String JavaDoc SYSTEM_BUNDLE = "org.eclipse.osgi"; //$NON-NLS-1$
65

66     protected static boolean DEBUG = false;
67
68     protected static StateObjectFactory stateObjectFactory;
69
70     protected static String JavaDoc DIR;
71
72     static {
73         DEBUG = PDECore.getDefault().isDebugging()
74                 && "true".equals(Platform.getDebugOption("org.eclipse.pde.core/cache")); //$NON-NLS-1$ //$NON-NLS-2$
75
DIR = PDECore.getDefault().getStateLocation().toOSString();
76         stateObjectFactory = Platform.getPlatformAdmin().getFactory();
77     }
78     
79     protected MinimalState(MinimalState state) {
80         this.fState = stateObjectFactory.createState(state.fState);
81         this.fState.setPlatformProperties(state.fState.getPlatformProperties());
82         this.fState.setResolver(Platform.getPlatformAdmin().getResolver());
83         this.fId = state.fId;
84         this.fEEListChanged = state.fEEListChanged;
85         this.fExecutionEnvironments = state.fExecutionEnvironments;
86         this.fNoProfile = state.fNoProfile;
87     }
88     
89     protected MinimalState() {
90     }
91     
92     public void addBundle(IPluginModelBase model, boolean update) {
93         if (model == null)
94             return;
95         
96         BundleDescription desc = model.getBundleDescription();
97         long bundleId = desc == null || !update ? -1 : desc.getBundleId();
98         try {
99             model.setBundleDescription(
100                     addBundle(new File JavaDoc(model.getInstallLocation()), bundleId));
101         } catch (IOException JavaDoc e) {
102         } catch (PluginConversionException e) {
103         } catch (CoreException e) {
104             PDECore.log(e);
105         }
106     }
107     
108     public BundleDescription addBundle(IPluginModelBase model, long bundleId) {
109         try {
110             return addBundle(new File JavaDoc(model.getInstallLocation()), -1);
111         } catch (IOException JavaDoc e) {
112         } catch (PluginConversionException e) {
113         } catch (CoreException e) {
114         }
115         return null;
116     }
117
118     public BundleDescription addBundle(Dictionary JavaDoc manifest, File JavaDoc bundleLocation, long bundleId) {
119         try {
120             BundleDescription descriptor = stateObjectFactory.createBundleDescription(
121                     fState, manifest, bundleLocation.getAbsolutePath(),
122                     bundleId == -1 ? getNextId() : bundleId);
123             // new bundle
124
if (bundleId == -1) {
125                 fState.addBundle(descriptor);
126             } else if (!fState.updateBundle(descriptor)) {
127                 fState.addBundle(descriptor);
128             }
129             return descriptor;
130         } catch (BundleException e) {
131         } catch (NumberFormatException JavaDoc e) {
132         } catch (IllegalArgumentException JavaDoc e) {
133         }
134         return null;
135     }
136
137     public BundleDescription addBundle(File JavaDoc bundleLocation, long bundleId) throws PluginConversionException, CoreException, IOException JavaDoc {
138         Dictionary JavaDoc manifest = loadManifest(bundleLocation);
139         boolean hasBundleStructure = manifest != null && manifest.get(Constants.BUNDLE_SYMBOLICNAME) != null;
140         if (!hasBundleStructure) {
141             if (!bundleLocation.isFile()
142                     && !new File JavaDoc(bundleLocation, "plugin.xml").exists() //$NON-NLS-1$
143
&& !new File JavaDoc(bundleLocation, "fragment.xml").exists()) //$NON-NLS-1$
144
return null;
145             PluginConverter converter = acquirePluginConverter();
146             manifest = converter.convertManifest(bundleLocation, false, null, false, null);
147             if (manifest == null
148                     || manifest.get(Constants.BUNDLE_SYMBOLICNAME) == null)
149                 throw new CoreException(new Status(
150                         IStatus.ERROR,
151                         PDECore.PLUGIN_ID,
152                         IStatus.ERROR,
153                         "Error parsing plug-in manifest file at " + bundleLocation.toString(), null)); //$NON-NLS-1$
154
}
155         BundleDescription desc = addBundle(manifest, bundleLocation, bundleId);
156         if (desc != null && SYSTEM_BUNDLE.equals(desc.getSymbolicName())) {
157             // if this is the system bundle then
158
// indicate that the javaProfile has changed since the new system
159
// bundle may not contain profiles for all EE's in the list
160
fEEListChanged = true;
161         }
162         if (desc != null) {
163             addAuxiliaryData(desc, manifest, hasBundleStructure);
164         }
165         return desc;
166     }
167     
168     protected void addAuxiliaryData(BundleDescription desc, Dictionary JavaDoc manifest, boolean hasBundleStructure) {
169     }
170
171     protected void saveState(File JavaDoc dir) {
172         saveState(fState, dir);
173     }
174     
175     protected void saveState(State state, File JavaDoc dir) {
176         try {
177             if (!dir.exists())
178                 dir.mkdirs();
179             stateObjectFactory.writeState(state, dir);
180         } catch (FileNotFoundException JavaDoc e) {
181             PDECore.log(e);
182         } catch (IOException JavaDoc e) {
183             PDECore.log(e);
184         } finally {
185         }
186     }
187
188     public static Dictionary JavaDoc loadManifest(File JavaDoc bundleLocation) throws IOException JavaDoc {
189         ZipFile JavaDoc jarFile = null;
190         InputStream JavaDoc manifestStream = null;
191         try {
192             String JavaDoc extension = new Path(bundleLocation.getName()).getFileExtension();
193             if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$
194
jarFile = new ZipFile JavaDoc(bundleLocation, ZipFile.OPEN_READ);
195                 ZipEntry JavaDoc manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME);
196                 if (manifestEntry != null) {
197                     manifestStream = jarFile.getInputStream(manifestEntry);
198                 }
199             } else {
200                 File JavaDoc file = new File JavaDoc(bundleLocation, JarFile.MANIFEST_NAME);
201                 if (file.exists())
202                     manifestStream = new FileInputStream JavaDoc(file);
203             }
204         } catch (IOException JavaDoc e) {
205         }
206         if (manifestStream == null)
207             return null;
208         try {
209             return (Dictionary JavaDoc) ManifestElement.parseBundleManifest(manifestStream, new Headers(10));
210         } catch (BundleException e) {
211         } finally {
212             try {
213                 if (jarFile != null)
214                     jarFile.close();
215             } catch (IOException JavaDoc e2) {
216             }
217         }
218         return null;
219     }
220
221     public StateDelta resolveState(boolean incremental) {
222         return internalResolveState(incremental);
223     }
224
225     private synchronized StateDelta internalResolveState(boolean incremental) {
226         boolean fullBuildRequired = initializePlatformProperties();
227         return fState.resolve(incremental && !fullBuildRequired);
228     }
229     
230     protected boolean initializePlatformProperties() {
231         if (fExecutionEnvironments == null && !fNoProfile)
232             setExecutionEnvironments();
233     
234         if (fEEListChanged) {
235             fEEListChanged = false;
236             return fState.setPlatformProperties(getProfilePlatformProperties());
237         }
238         return false;
239     }
240
241     private Dictionary JavaDoc[] getProfilePlatformProperties() {
242         return TargetPlatformHelper.getPlatformProperties(fExecutionEnvironments, this);
243     }
244
245     public void removeBundleDescription(BundleDescription description) {
246         if (description != null)
247             fState.removeBundle(description);
248     }
249
250     public State getState() {
251         return fState;
252     }
253
254     private void setExecutionEnvironments() {
255         String JavaDoc[] knownExecutionEnviroments = TargetPlatformHelper.getKnownExecutionEnvironments();
256         if (knownExecutionEnviroments.length == 0) {
257             String JavaDoc jreProfile = System.getProperty("pde.jreProfile"); //$NON-NLS-1$
258
if (jreProfile != null && jreProfile.length() > 0)
259                 if ("none".equals(jreProfile)) //$NON-NLS-1$
260
fNoProfile = true;
261         }
262         if (!fNoProfile) {
263             fExecutionEnvironments = knownExecutionEnviroments;
264         }
265         fEEListChanged = true; // alway indicate the list has changed
266
}
267     
268     public void addBundleDescription(BundleDescription toAdd) {
269         if (toAdd != null)
270             fState.addBundle(toAdd);
271     }
272
273     private PluginConverter acquirePluginConverter() {
274         if (fConverter == null) {
275             ServiceTracker tracker = new ServiceTracker(PDECore.getDefault()
276                     .getBundleContext(), PluginConverter.class.getName(), null);
277             tracker.open();
278             fConverter = (PluginConverter) tracker.getService();
279             tracker.close();
280         }
281         return fConverter;
282     }
283
284     public long getNextId() {
285         return ++fId;
286     }
287
288     private BundleDescription findActiveBundle(String JavaDoc symbolicName) {
289         BundleDescription[] bundles = fState.getBundles(symbolicName);
290         for (int i = 0; i < bundles.length; i++) {
291             if (bundles[i].isResolved())
292                 return bundles[i];
293         }
294         return null;
295     }
296
297     protected void logResolutionErrors() {
298         MultiStatus errors = new MultiStatus(PDECore.PLUGIN_ID, 1,
299                 "Problems occurred during the resolution of the target platform", //$NON-NLS-1$
300
null);
301
302         StateHelper helper = Platform.getPlatformAdmin().getStateHelper();
303         BundleDescription[] all = fState.getBundles();
304         for (int i = 0; i < all.length; i++) {
305             if (!all[i].isResolved()) {
306                 VersionConstraint[] unsatisfiedConstraints = helper
307                         .getUnsatisfiedConstraints(all[i]);
308                 if (unsatisfiedConstraints.length == 0) {
309                     if (DEBUG) {
310                         BundleDescription activeBundle = findActiveBundle(all[i]
311                                 .getSymbolicName());
312                         String JavaDoc message = "Plug-in located at \"" + all[i].getLocation() + "\" was disabled because plug-in located at \"" + activeBundle.getLocation() + "\" was selected."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
313
System.out.print(message);
314                     }
315                 } else {
316                     for (int j = 0; j < unsatisfiedConstraints.length; j++) {
317                         String JavaDoc message = getResolutionFailureMessage(unsatisfiedConstraints[j]);
318                         if (message != null)
319                             errors.add(new Status(IStatus.WARNING, all[i]
320                                     .getSymbolicName(), IStatus.WARNING, message, null));
321                     }
322                 }
323             }
324         }
325         if (errors.getChildren().length > 0)
326             PDECore.log(errors);
327     }
328
329     private String JavaDoc getResolutionFailureMessage(VersionConstraint unsatisfied) {
330         if (unsatisfied.isResolved())
331             throw new IllegalArgumentException JavaDoc();
332         if (unsatisfied instanceof ImportPackageSpecification)
333             return "Missing imported package: " + toString(unsatisfied); //$NON-NLS-1$
334
if (unsatisfied instanceof BundleSpecification && !((BundleSpecification)unsatisfied).isOptional())
335             return "Missing required plug-in: " + toString(unsatisfied); //$NON-NLS-1$
336
if (unsatisfied instanceof HostSpecification)
337             return "Missing Fragment Host: " + toString(unsatisfied); //$NON-NLS-1$
338
return null;
339     }
340
341     private String JavaDoc toString(VersionConstraint constraint) {
342         VersionRange versionRange = constraint.getVersionRange();
343         if (versionRange == null || versionRange.getMinimum() != null)
344             return constraint.getName();
345         return constraint.getName() + '_' + versionRange;
346     }
347
348 }
349
Popular Tags