1 11 package org.eclipse.pde.internal.build; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.osgi.service.resolver.BundleDescription; 18 import org.eclipse.pde.internal.build.site.PDEState; 19 20 public class ProductGenerator extends AbstractScriptGenerator { 21 private static final String BUNDLE_EQUINOX_COMMON = "org.eclipse.equinox.common"; private static final String BUNDLE_OSGI = "org.eclipse.osgi"; private static final String BUNDLE_CORE_RUNTIME = "org.eclipse.core.runtime"; private static final String BUNDLE_UPDATE_CONFIGURATOR = "org.eclipse.update.configurator"; private static final String START_LEVEL_2 = "@2:start"; private static final String START_LEVEL_3 = "@3:start"; private static final String START = "@start"; 29 private String product = null; 30 private ProductFile productFile = null; 31 private String root = null; 32 private boolean refactoredRuntime = false; 33 private Properties buildProperties; 34 35 38 public void generate() throws CoreException { 39 initialize(); 40 41 if (productFile == null) 42 return; 43 44 if (productFile.getId() == null) { 46 return; 47 } 48 49 String custom = findFile(productFile.getConfigIniPath(), false); 50 String location = null, fileList = null; 51 for (Iterator iter = getConfigInfos().iterator(); iter.hasNext();) { 52 Config config = (Config) iter.next(); 53 location = DEFAULT_PRODUCT_ROOT_FILES_DIR + '/' + config.toStringReplacingAny(".", ANY_STRING); 55 String rootLocation = root + location; 56 File rootDir = new File(rootLocation); 57 if ((!rootDir.exists() && !rootDir.mkdirs()) || rootDir.isFile()) 58 continue; 60 if (buildProperties != null) { 62 fileList = buildProperties.getProperty(ROOT_PREFIX + config.toString("."), ""); fileList += (fileList.length() > 0) ? ',' + location : location; 64 buildProperties.put(ROOT_PREFIX + config.toString("."), fileList); } 66 67 if (custom != null) { 69 copyFile(custom, rootLocation + "/configuration/config.ini"); } else { 71 createConfigIni(config, rootLocation); 72 } 73 74 if (config.getOs().equals(Config.ANY)) 76 continue; 77 78 createEclipseProductFile(rootLocation); 80 81 createLauncherIniFile(rootLocation, config.getOs()); 83 } 84 85 } 86 87 private void initialize() throws CoreException { 88 loadProduct(); 89 90 PDEState state = getSite(false).getRegistry(); 91 refactoredRuntime = state.getResolvedBundle(BUNDLE_EQUINOX_COMMON) != null; 92 } 93 94 private void loadProduct() throws CoreException { 95 if (product == null || product.startsWith("${")) { productFile = null; 97 return; 98 } 99 String productPath = findFile(product, false); 100 File f = null; 101 if (productPath != null) { 102 f = new File(productPath); 103 } else { 104 f = new File(product); 106 if (!f.exists() || !f.isFile()) { 107 f = new File(getWorkingDirectory(), product); 109 if (!f.exists() || !f.isFile()) { 110 f = new File(getWorkingDirectory() + "/" + DEFAULT_PLUGIN_LOCATION, product); } 112 } 113 } 114 115 productFile = new ProductFile(f.getAbsolutePath(), null); 118 } 119 120 private void copyFile(String src, String dest) { 121 File source = new File(src); 122 if (!source.exists()) 123 return; 124 File destination = new File(dest); 125 File destDir = destination.getParentFile(); 126 if ((!destDir.exists() && !destDir.mkdirs()) || destDir.isFile()) 127 return; 129 InputStream in = null; 130 OutputStream out = null; 131 try { 132 in = new BufferedInputStream(new FileInputStream(source)); 133 out = new BufferedOutputStream(new FileOutputStream(destination)); 134 135 byte[] buf = new byte[1024]; 137 int len; 138 while ((len = in.read(buf)) > 0) { 139 out.write(buf, 0, len); 140 } 141 } catch (IOException e) { 142 } finally { 144 if (in != null) { 145 try { 146 in.close(); 147 } catch (IOException e) { 148 } 150 } 151 if (out != null) { 152 try { 153 out.close(); 154 } catch (IOException e) { 155 } 157 } 158 } 159 } 160 161 private void createConfigIni(Config config, String location) throws CoreException { 162 File configDir = new File(location + "/configuration"); if ((!configDir.exists() && !configDir.mkdirs()) || configDir.isFile()) 164 return; 166 PDEState state = getSite(false).getRegistry(); 167 168 StringBuffer buffer = new StringBuffer (); 169 buffer.append("#Product Runtime Configuration File\n"); 171 String splash = getSplashLocation(config); 172 if (splash != null) 173 buffer.append("osgi.splashPath=" + splash + '\n'); 175 String application = productFile.getApplication(); 176 if (application != null) 177 buffer.append("eclipse.application=" + application + '\n'); buffer.append("eclipse.product=" + productFile.getId() + '\n'); buffer.append("osgi.bundles="); if (productFile.useFeatures() || productFile.containsPlugin(BUNDLE_UPDATE_CONFIGURATOR)) { 182 if (refactoredRuntime) { 183 buffer.append(BUNDLE_EQUINOX_COMMON); 186 buffer.append(START_LEVEL_2); 187 buffer.append(','); 188 buffer.append(BUNDLE_UPDATE_CONFIGURATOR); 190 buffer.append(START_LEVEL_3); 191 buffer.append(','); 192 buffer.append(BUNDLE_CORE_RUNTIME); 194 buffer.append(START); 195 buffer.append('\n'); 196 } else { 197 buffer.append(BUNDLE_CORE_RUNTIME); 199 buffer.append(START_LEVEL_2); 200 buffer.append(','); 201 buffer.append(BUNDLE_UPDATE_CONFIGURATOR); 202 buffer.append(START_LEVEL_3); 203 buffer.append('\n'); 204 } 205 } else { 206 Dictionary environment = new Hashtable(3); 208 environment.put("osgi.os", config.getOs()); environment.put("osgi.ws", config.getWs()); environment.put("osgi.arch", config.getArch()); 212 List pluginList = productFile.getPlugins(); 213 BundleHelper helper = BundleHelper.getDefault(); 214 boolean first = true; 215 for (Iterator iter = pluginList.iterator(); iter.hasNext();) { 216 String id = (String ) iter.next(); 217 BundleDescription bundle = state.getResolvedBundle(id); 218 if (bundle == null) { 219 continue; 221 } 222 String filter = bundle.getPlatformFilter(); 223 if (filter == null || helper.createFilter(filter).match(environment)) { 224 if (BUNDLE_OSGI.equals(id)) 225 continue; 226 if (first) 227 first = false; 228 else 229 buffer.append(","); buffer.append(id); 231 if (BUNDLE_EQUINOX_COMMON.equals(id)) { 232 buffer.append(START_LEVEL_2); 233 } else if (BUNDLE_CORE_RUNTIME.equals(id)) { 234 if (refactoredRuntime) { 235 buffer.append(START); 236 } else { 237 buffer.append(START_LEVEL_2); 238 } 239 } 240 } 241 } 242 buffer.append('\n'); 243 } 244 buffer.append("osgi.bundles.defaultStartLevel=4\n"); 246 FileWriter writer = null; 247 try { 248 writer = new FileWriter(new File(configDir, "config.ini")); writer.write(buffer.toString()); 250 } catch (IOException e) { 251 } finally { 253 try { 254 if (writer != null) 255 writer.close(); 256 } catch (IOException e) { 257 } 259 } 260 261 } 262 263 private void createEclipseProductFile(String directory) throws CoreException { 264 File dir = new File(directory); 265 if ((!dir.exists() && !dir.mkdirs()) || dir.isFile()) 266 return; 268 Properties properties = new Properties(); 269 if (productFile.getProductName() != null) 270 properties.put("name", productFile.getProductName()); if (productFile.getId() != null) 272 properties.put("id", productFile.getId()); 274 BundleDescription bundle = getSite(false).getRegistry().getResolvedBundle(getBrandingPlugin()); 275 if (bundle != null) 276 properties.put("version", bundle.getVersion().toString()); 278 OutputStream stream = null; 279 try { 280 File file = new File(dir, ".eclipseproduct"); stream = new BufferedOutputStream(new FileOutputStream(file)); 282 properties.store(stream, "Eclipse Product File"); stream.flush(); 284 } catch (IOException e) { 285 } finally { 287 if (stream != null) { 288 try { 289 stream.close(); 290 } catch (IOException e) { 291 } 293 } 294 } 295 } 296 297 private String getBrandingPlugin() { 298 String id = productFile.getId(); 299 if (id == null) 300 return null; 301 int dot = id.lastIndexOf('.'); 302 return (dot != -1) ? id.substring(0, dot) : null; 303 } 304 305 private String getSplashLocation(Config config) throws CoreException { 306 String plugin = productFile.getSplashLocation(); 307 308 if (plugin == null) { 309 plugin = getBrandingPlugin(); 310 } 311 312 if (plugin == null) 313 return null; 314 315 StringBuffer buffer = new StringBuffer ("platform:/base/plugins/"); buffer.append(plugin); 317 318 Dictionary environment = new Hashtable(4); 319 environment.put("osgi.os", config.getOs()); environment.put("osgi.ws", config.getWs()); environment.put("osgi.arch", config.getArch()); 323 PDEState state = getSite(false).getRegistry(); 324 BundleHelper helper = BundleHelper.getDefault(); 325 BundleDescription bundle = state.getResolvedBundle(plugin); 326 if (bundle != null) { 327 BundleDescription[] fragments = bundle.getFragments(); 328 for (int i = 0; i < fragments.length; i++) { 329 String filter = fragments[i].getPlatformFilter(); 330 if (filter == null || helper.createFilter(filter).match(environment)) { 331 String fragmentId = fragments[i].getSymbolicName(); 332 if (productFile.containsPlugin(fragmentId)) { 333 buffer.append(",platform:/base/plugins/"); buffer.append(fragmentId); 335 } 336 } 337 } 338 } 339 return buffer.toString(); 340 } 341 342 private void createLauncherIniFile(String directory, String os) { 343 String launcher = getLauncherName(); 344 345 if (os.equals(Platform.OS_MACOSX)) { 346 directory += "/" + launcher + ".app/Contents/MacOS"; } 348 File dir = new File(directory); 349 if ((!dir.exists() && !dir.mkdirs()) || dir.isFile()) 350 return; 352 String programArgs = productFile.getProgramArguments(os); 353 String vmArgs = productFile.getVMArguments(os); 354 355 if ((programArgs == null || programArgs.length() == 0) && (vmArgs == null || vmArgs.length() == 0)) 356 return; 357 358 String lineDelimiter = Platform.OS_WIN32.equals(os) ? "\r\n" : "\n"; PrintWriter writer = null; 360 try { 361 writer = new PrintWriter(new FileWriter(new File(dir, launcher + ".ini"))); if (programArgs != null && programArgs.length() > 0) { 363 StringReader reader = new StringReader(programArgs); 364 StreamTokenizer tokenizer = new StreamTokenizer(reader); 365 tokenizer.resetSyntax(); 366 tokenizer.whitespaceChars(0,0x20); 367 tokenizer.wordChars(0x21, 0xFF); 368 tokenizer.quoteChar('"'); 369 tokenizer.quoteChar('\''); 370 while (tokenizer.nextToken() != StreamTokenizer.TT_EOF){ 371 writer.print(tokenizer.sval); 372 writer.print(lineDelimiter); 373 } 374 } 375 if (vmArgs != null && vmArgs.length() > 0) { 376 writer.print("-vmargs"); writer.print(lineDelimiter); 378 StringReader reader = new StringReader(vmArgs); 379 StreamTokenizer tokenizer = new StreamTokenizer(reader); 380 tokenizer.resetSyntax(); 381 tokenizer.whitespaceChars(0,0x20); 382 tokenizer.wordChars(0x21, 0xFF); 383 tokenizer.quoteChar('"'); 384 tokenizer.quoteChar('\''); 385 while (tokenizer.nextToken() != StreamTokenizer.TT_EOF){ 386 writer.print(tokenizer.sval); 387 writer.print(lineDelimiter); 388 } 389 } 390 } catch (IOException e) { 391 } finally { 393 if (writer != null) { 394 writer.close(); 395 } 396 } 397 } 398 399 private String getLauncherName() { 400 String name = productFile.getLauncherName(); 401 402 if (name != null && name.length() > 0) { 403 name = name.trim(); 404 if (name.endsWith(".exe")) name = name.substring(0, name.length() - 4); 406 return name; 407 } 408 return "eclipse"; } 410 411 public void setProduct(String product) { 412 this.product = product; 413 } 414 415 public void setRoot(String root) { 416 this.root = root; 417 } 418 419 public void setBuildProperties(Properties buildProperties) { 420 this.buildProperties = buildProperties; 421 } 422 423 } 424 | Popular Tags |