1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.Dictionary ; 19 import java.util.jar.JarFile ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipFile ; 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; 60 private String [] fExecutionEnvironments; 62 private boolean fNoProfile; 63 64 private static final String SYSTEM_BUNDLE = "org.eclipse.osgi"; 66 protected static boolean DEBUG = false; 67 68 protected static StateObjectFactory stateObjectFactory; 69 70 protected static String DIR; 71 72 static { 73 DEBUG = PDECore.getDefault().isDebugging() 74 && "true".equals(Platform.getDebugOption("org.eclipse.pde.core/cache")); 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 (model.getInstallLocation()), bundleId)); 101 } catch (IOException 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 (model.getInstallLocation()), -1); 111 } catch (IOException e) { 112 } catch (PluginConversionException e) { 113 } catch (CoreException e) { 114 } 115 return null; 116 } 117 118 public BundleDescription addBundle(Dictionary manifest, File bundleLocation, long bundleId) { 119 try { 120 BundleDescription descriptor = stateObjectFactory.createBundleDescription( 121 fState, manifest, bundleLocation.getAbsolutePath(), 122 bundleId == -1 ? getNextId() : bundleId); 123 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 e) { 132 } catch (IllegalArgumentException e) { 133 } 134 return null; 135 } 136 137 public BundleDescription addBundle(File bundleLocation, long bundleId) throws PluginConversionException, CoreException, IOException { 138 Dictionary manifest = loadManifest(bundleLocation); 139 boolean hasBundleStructure = manifest != null && manifest.get(Constants.BUNDLE_SYMBOLICNAME) != null; 140 if (!hasBundleStructure) { 141 if (!bundleLocation.isFile() 142 && !new File (bundleLocation, "plugin.xml").exists() && !new File (bundleLocation, "fragment.xml").exists()) 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)); } 155 BundleDescription desc = addBundle(manifest, bundleLocation, bundleId); 156 if (desc != null && SYSTEM_BUNDLE.equals(desc.getSymbolicName())) { 157 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 manifest, boolean hasBundleStructure) { 169 } 170 171 protected void saveState(File dir) { 172 saveState(fState, dir); 173 } 174 175 protected void saveState(State state, File dir) { 176 try { 177 if (!dir.exists()) 178 dir.mkdirs(); 179 stateObjectFactory.writeState(state, dir); 180 } catch (FileNotFoundException e) { 181 PDECore.log(e); 182 } catch (IOException e) { 183 PDECore.log(e); 184 } finally { 185 } 186 } 187 188 public static Dictionary loadManifest(File bundleLocation) throws IOException { 189 ZipFile jarFile = null; 190 InputStream manifestStream = null; 191 try { 192 String extension = new Path(bundleLocation.getName()).getFileExtension(); 193 if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { jarFile = new ZipFile (bundleLocation, ZipFile.OPEN_READ); 195 ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME); 196 if (manifestEntry != null) { 197 manifestStream = jarFile.getInputStream(manifestEntry); 198 } 199 } else { 200 File file = new File (bundleLocation, JarFile.MANIFEST_NAME); 201 if (file.exists()) 202 manifestStream = new FileInputStream (file); 203 } 204 } catch (IOException e) { 205 } 206 if (manifestStream == null) 207 return null; 208 try { 209 return (Dictionary ) ManifestElement.parseBundleManifest(manifestStream, new Headers(10)); 210 } catch (BundleException e) { 211 } finally { 212 try { 213 if (jarFile != null) 214 jarFile.close(); 215 } catch (IOException 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 [] 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 [] knownExecutionEnviroments = TargetPlatformHelper.getKnownExecutionEnvironments(); 256 if (knownExecutionEnviroments.length == 0) { 257 String jreProfile = System.getProperty("pde.jreProfile"); if (jreProfile != null && jreProfile.length() > 0) 259 if ("none".equals(jreProfile)) fNoProfile = true; 261 } 262 if (!fNoProfile) { 263 fExecutionEnvironments = knownExecutionEnviroments; 264 } 265 fEEListChanged = true; } 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 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", 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 message = "Plug-in located at \"" + all[i].getLocation() + "\" was disabled because plug-in located at \"" + activeBundle.getLocation() + "\" was selected."; System.out.print(message); 314 } 315 } else { 316 for (int j = 0; j < unsatisfiedConstraints.length; j++) { 317 String 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 getResolutionFailureMessage(VersionConstraint unsatisfied) { 330 if (unsatisfied.isResolved()) 331 throw new IllegalArgumentException (); 332 if (unsatisfied instanceof ImportPackageSpecification) 333 return "Missing imported package: " + toString(unsatisfied); if (unsatisfied instanceof BundleSpecification && !((BundleSpecification)unsatisfied).isOptional()) 335 return "Missing required plug-in: " + toString(unsatisfied); if (unsatisfied instanceof HostSpecification) 337 return "Missing Fragment Host: " + toString(unsatisfied); return null; 339 } 340 341 private String 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 |