1 11 package org.eclipse.pde.internal.build; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.osgi.service.resolver.*; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.pde.build.Constants; 19 import org.eclipse.pde.internal.build.site.PDEState; 20 import org.eclipse.update.core.IFeature; 21 import org.osgi.framework.Version; 22 23 public class FeatureGenerator extends AbstractScriptGenerator { 24 25 private static final String FEATURE_PLATFORM_LAUNCHERS = "org.eclipse.platform.launchers"; private static final String FEATURE_EXECUTABLE = "org.eclipse.equinox.executable"; private static final String BUNDLE_OSGI = "org.eclipse.osgi"; private static final String BUNDLE_LAUNCHER = "org.eclipse.equinox.launcher"; 30 private String featureId = null; 31 private String productFile = null; 32 private String [] pluginList = null; 33 private String [] fragmentList = null; 34 private String [] featureList = null; 35 36 private boolean includeLaunchers = true; 37 38 private ProductFile product = null; 39 40 private boolean verify = false; 41 42 private Properties antProperties; 43 44 48 private static Set createSet(String [] contents) { 49 if (contents == null) 50 return new LinkedHashSet(0); 51 Set result = new LinkedHashSet(contents.length); 52 for (int i = 0; i < contents.length; i++) 53 if (contents[i] != null) 54 result.add(contents[i]); 55 return result; 56 } 57 58 61 public void generate() throws CoreException { 62 AbstractScriptGenerator.setStaticAntProperties(antProperties); 63 try { 64 initialize(); 65 Set plugins = createSet(pluginList); 66 Set features = createSet(featureList); 67 Set fragments = createSet(fragmentList); 68 if (product != null) { 69 if (product.useFeatures()) { 70 features.addAll(product.getFeatures()); 71 } else { 72 plugins.addAll(product.getPlugins(false)); 73 fragments.addAll(product.getFragments()); 74 } 75 } 76 try { 77 createFeature(featureId, plugins, fragments, features); 78 } catch (FileNotFoundException e) { 79 IStatus status = new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.error_creatingFeature, e.getLocalizedMessage()), e); 80 throw new CoreException(status); 81 } 82 } finally { 83 AbstractScriptGenerator.setStaticAntProperties(null); 84 } 85 } 86 87 public void setProductFile(String productFile) { 88 this.productFile = productFile; 89 } 90 91 public void setPluginList(String [] pluginList) { 92 this.pluginList = pluginList; 93 } 94 95 public void setFeatureList(String [] featureList) { 96 this.featureList = featureList; 97 } 98 99 public void setFragmentList(String [] fragmentList) { 100 this.fragmentList = fragmentList; 101 } 102 103 public void setFeatureId(String featureId) { 104 this.featureId = featureId; 105 } 106 107 public void setIncludeLaunchers(boolean includeLaunchers) { 108 this.includeLaunchers = includeLaunchers; 109 } 110 111 private void initialize() throws CoreException { 112 File dir = new File(getWorkingDirectory(), IPDEBuildConstants.DEFAULT_FEATURE_LOCATION + '/' + featureId); 114 File xml = new File(dir, Constants.FEATURE_FILENAME_DESCRIPTOR); 115 if (xml.exists()) { 116 xml.delete(); 117 } 118 119 if (productFile != null && !productFile.startsWith("${") && productFile.length() > 0) { String productPath = findFile(productFile, false); 121 File f = null; 122 if (productPath != null) { 123 f = new File(productPath); 124 } else { 125 f = new File(productFile); 127 if (!f.exists() || !f.isFile()) { 128 f = new File(getWorkingDirectory(), productFile); 130 if (!f.exists() || !f.isFile()) { 131 f = new File(getWorkingDirectory() + "/" + DEFAULT_PLUGIN_LOCATION, productFile); } 133 } 134 } 135 if (f.exists() && f.isFile()) { 136 product = new ProductFile(f.getAbsolutePath(), null); 137 } else { 138 IStatus error = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FILE, NLS.bind(Messages.exception_missingElement, productFile), null); 139 throw new CoreException(error); 140 } 141 } 142 } 143 144 148 private void addLauncher(PDEState state, Set plugins, Set fragments, Set features) { 149 BundleDescription bundle = state.getResolvedBundle(BUNDLE_OSGI); 150 if (bundle == null) 151 return; 152 Version version = bundle.getVersion(); 153 if (version.compareTo(new Version("3.3")) < 0) { features.add(FEATURE_PLATFORM_LAUNCHERS); 156 } else { 157 IFeature executableFeature = null; 160 try { 161 executableFeature = getSite(false).findFeature(FEATURE_EXECUTABLE, null, false); 162 } catch (CoreException e) { 163 } 165 if (executableFeature != null) { 166 167 features.add(FEATURE_EXECUTABLE); 168 } else { 169 plugins.add(BUNDLE_LAUNCHER); 171 List configs = getConfigInfos(); 172 for (Iterator iterator = configs.iterator(); iterator.hasNext();) { 175 Config config = (Config) iterator.next(); 176 String fragment = BUNDLE_LAUNCHER + '.' + config.getWs() + '.' + config.getOs(); 177 if (config.getOs().compareToIgnoreCase("macosx") != 0) fragment += '.' + config.getArch(); 180 181 fragments.add(fragment); 182 } 183 } 184 } 185 } 186 187 198 protected void createFeature(String feature, Set plugins, Set fragments, Set features) throws CoreException, FileNotFoundException { 199 String location = IPDEBuildConstants.DEFAULT_FEATURE_LOCATION + '/' + feature; 200 File directory = new File(getWorkingDirectory(), location); 201 if (!directory.exists()) 202 directory.mkdirs(); 203 204 PDEState state = verify ? getSite(false).getRegistry() : null; 205 BundleHelper helper = BundleHelper.getDefault(); 206 207 if (verify && includeLaunchers) 208 addLauncher(state, plugins, fragments, features); 209 210 File file = new File(directory, Constants.FEATURE_FILENAME_DESCRIPTOR); 212 OutputStream output = new BufferedOutputStream(new FileOutputStream(file)); 213 XMLWriter writer = null; 214 try { 215 writer = new XMLWriter(output); 216 } catch (UnsupportedEncodingException e) { 217 return; 219 } 220 try { 221 Map parameters = new HashMap(); 222 Dictionary environment = new Hashtable(3); 223 224 parameters.put("id", feature); parameters.put("version", "1.0.0"); writer.startTag("feature", parameters, true); 228 boolean fragment = false; 229 List configs = getConfigInfos(); 230 configs.remove(Config.genericConfig()); 232 Iterator configIterator = configs.iterator(); 233 Iterator listIter = plugins.iterator(); 234 if (!listIter.hasNext()) { 235 fragment = true; 237 listIter = fragments.iterator(); 238 } 239 for (Config currentConfig = Config.genericConfig(); currentConfig != null; currentConfig = (Config) configIterator.next()) { 240 environment.put("osgi.os", currentConfig.getOs()); environment.put("osgi.ws", currentConfig.getWs()); environment.put("osgi.arch", currentConfig.getArch()); for (; listIter.hasNext();) { 244 String name = (String ) listIter.next(); 245 boolean unpack = true; 246 boolean writeBundle = !verify; 247 if (verify) { 248 BundleDescription bundle = state.getResolvedBundle(name); 249 if (bundle != null) { 250 String filterSpec = bundle.getPlatformFilter(); 252 if (filterSpec == null || helper.createFilter(filterSpec).match(environment)) { 253 writeBundle = true; 254 unpack = guessUnpack(bundle, (String []) state.getExtraData().get(new Long (bundle.getBundleId()))); 255 if (currentConfig.equals(Config.genericConfig())) { 256 listIter.remove(); 257 } 258 } 259 } else { 260 BundleDescription[] bundles = state.getState().getBundles(name); 262 boolean error = true; 263 if (bundles != null && bundles.length > 0) { 264 ResolverError[] errors = state.getState().getResolverErrors(bundles[0]); 265 for (int i = 0; i < errors.length; i++) { 266 if ((errors[i].getType() & ResolverError.PLATFORM_FILTER) != 0) { 267 error = false; 269 break; 270 } 271 } 272 } 273 if (error) { 274 String message = NLS.bind(Messages.exception_missingPlugin, name); 276 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message, null)); 277 } 278 } 279 } 280 281 if (writeBundle) { 282 parameters.clear(); 283 parameters.put("id", name); parameters.put("version", "0.0.0"); parameters.put("unpack", unpack ? "true" : "false"); if (!currentConfig.equals(Config.genericConfig())) { 287 parameters.put("os", currentConfig.getOs()); parameters.put("ws", currentConfig.getWs()); parameters.put("arch", currentConfig.getArch()); } 291 if (fragment) 292 parameters.put("fragment", "true"); writer.printTag("plugin", parameters, true, true, true); } 295 296 if (!fragment && !listIter.hasNext() && fragments.size() > 0) { 297 fragment = true; 299 listIter = fragments.iterator(); 300 } 301 } 302 if (!verify || !configIterator.hasNext()) { 303 break; 304 } else if (plugins.size() > 0) { 305 fragment = false; 306 listIter = plugins.iterator(); 307 } else { 308 listIter = fragments.iterator(); 309 } 310 } 311 312 for (Iterator iter = features.iterator(); iter.hasNext();) { 313 String name = (String ) iter.next(); 314 if (verify) { 315 getSite(false).findFeature(name, null, true); 317 } 318 parameters.clear(); 319 parameters.put("id", name); parameters.put("version", "0.0.0"); writer.printTag("includes", parameters, true, true, true); } 323 writer.endTag("feature"); } finally { 325 writer.close(); 326 } 327 328 file = new File(directory, IPDEBuildConstants.PROPERTIES_FILE); 330 Properties prop = new Properties(); 331 prop.put("pde", "marker"); OutputStream stream = null; 333 try { 334 stream = new BufferedOutputStream(new FileOutputStream(file)); 335 prop.store(stream, "Marker File so that the file gets written"); stream.flush(); 337 } catch (IOException e) { 338 } finally { 340 if (stream != null) { 341 try { 342 stream.close(); 343 } catch (IOException e1) { 344 } 346 } 347 } 348 } 349 350 public void setVerify(boolean verify) { 351 this.verify = verify; 352 reportResolutionErrors = verify; 353 } 354 355 public boolean guessUnpack(BundleDescription bundle, String [] classpath) { 356 if (bundle == null) 357 return true; 358 359 if (bundle.getHost() != null && bundle.getName().startsWith(BUNDLE_LAUNCHER)) 362 return true; 363 364 if (new File(bundle.getLocation()).isFile()) 365 return false; 366 367 if (classpath.length == 0) 368 return false; 369 370 for (int i = 0; i < classpath.length; i++) { 371 if (classpath[i].equals(".")) return false; 373 } 374 return true; 375 } 376 377 public void setImmutableAntProperties(Properties properties) { 378 antProperties = properties; 379 } 380 } 381 | Popular Tags |