1 11 package org.eclipse.pde.internal.ui.wizards.exports; 12 import java.io.*; 13 import java.lang.reflect.*; 14 import java.util.*; 15 import org.eclipse.ant.core.*; 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.jobs.*; 19 import org.eclipse.jdt.core.*; 20 import org.eclipse.jface.dialogs.*; 21 import org.eclipse.jface.preference.*; 22 import org.eclipse.pde.core.*; 23 import org.eclipse.pde.core.build.*; 24 import org.eclipse.pde.core.plugin.*; 25 import org.eclipse.pde.internal.build.*; 26 import org.eclipse.pde.internal.core.*; 27 import org.eclipse.pde.internal.core.build.*; 28 import org.eclipse.pde.internal.core.feature.FeatureChild; 29 import org.eclipse.pde.internal.core.ifeature.*; 30 import org.eclipse.pde.internal.ui.*; 31 import org.eclipse.pde.internal.ui.build.*; 32 import org.eclipse.swt.widgets.*; 33 34 public class FeatureExportJob extends Job implements IPreferenceConstants { 35 36 public static final int EXPORT_AS_ZIP = 0; 38 public static final int EXPORT_AS_DIRECTORY = 1; 39 public static final int EXPORT_AS_UPDATE_JARS = 2; 40 41 protected static PrintWriter writer; 43 protected static File logFile; 44 45 protected int fExportType; 47 protected boolean fExportSource; 48 protected String fDestinationDirectory; 49 protected String fZipFilename; 50 protected Object [] fItems; 51 52 protected String fBuildTempLocation; 54 private String fDevProperties; 55 56 protected HashMap fBuildProperties; 57 58 class SchedulingRule implements ISchedulingRule { 59 60 63 public boolean contains(ISchedulingRule rule) { 64 return rule instanceof SchedulingRule; 65 } 66 67 70 public boolean isConflicting(ISchedulingRule rule) { 71 return rule instanceof SchedulingRule; 72 } 73 74 } 75 76 public FeatureExportJob(int exportType, boolean exportSource, String destination, String zipFileName, Object [] items) { 77 super(PDEPlugin.getResourceString("FeatureExportJob.name")); fExportType = exportType; 79 fExportSource = exportSource; 80 fDestinationDirectory = destination; 81 fZipFilename = zipFileName; 82 fItems = items; 83 fBuildTempLocation = PDEPlugin.getDefault().getStateLocation().append("temp").toString(); setRule(new SchedulingRule()); 85 } 86 87 92 protected IStatus run(IProgressMonitor monitor) { 93 String errorMessage = null; 94 try { 95 createLogWriter(); 96 doExports(monitor); 97 } catch (final CoreException e) { 98 final Display display = getStandardDisplay(); 99 display.asyncExec(new Runnable () { 100 public void run() { 101 ErrorDialog.openError(display.getActiveShell(), PDEPlugin.getResourceString("FeatureExportJob.error"), PDEPlugin.getResourceString("FeatureExportJob.problems"), e.getStatus()); done(new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null)); } 104 }); 105 return Job.ASYNC_FINISH; 106 } catch (InvocationTargetException e) { 107 String message = e.getTargetException().getMessage(); 108 if (message != null && message.length() > 0) { 109 errorMessage = e.getTargetException().getMessage(); 110 } 111 } finally { 112 if (writer != null) 113 writer.close(); 114 } 115 if (errorMessage == null && logFile != null && logFile.exists() && logFile.length() > 0) { 116 errorMessage = getLogFoundMessage(); 117 } 118 119 if (errorMessage != null) { 120 final String em = errorMessage; 121 getStandardDisplay().asyncExec(new Runnable () { 122 public void run() { 123 asyncNotifyExportException(em); 124 } 125 }); 126 return Job.ASYNC_FINISH; 127 } 128 return new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null); } 130 131 132 protected void doExports(IProgressMonitor monitor) throws InvocationTargetException, CoreException { 133 createDestination(); 134 monitor.beginTask("", fItems.length + 1); try { 136 for (int i = 0; i < fItems.length; i++) { 137 IFeatureModel model = (IFeatureModel)fItems[i]; 138 try { 139 IFeature feature = model.getFeature(); 140 String id = feature.getId(); 141 String os = getOS(feature); 142 String ws = getWS(feature); 143 String arch = getOSArch(feature); 144 doExport(id, model.getFeature().getVersion(), model.getInstallLocation(), os, ws, arch, new SubProgressMonitor(monitor, 1)); 145 } finally { 146 deleteBuildFiles(model); 147 } 148 } 149 } finally { 150 cleanup(new SubProgressMonitor(monitor, 1)); 151 monitor.done(); 152 } 153 } 154 155 private String getOS(IFeature feature) { 156 String os = feature.getOS(); 157 if (os == null || os.trim().length() == 0 || os.indexOf(',') != -1 || os.equals("*")) return TargetPlatform.getOS(); 159 return os; 160 } 161 162 private String getWS(IFeature feature) { 163 String ws = feature.getWS(); 164 if (ws == null || ws.trim().length() == 0 || ws.indexOf(',') != -1 || ws.equals("*")) return TargetPlatform.getWS(); 166 return ws; 167 } 168 169 private String getOSArch(IFeature feature) { 170 String arch = feature.getArch(); 171 if (arch == null || arch.trim().length() == 0 || arch.indexOf(',') != -1 || arch.equals("*")) return TargetPlatform.getOSArch(); 173 return arch; 174 } 175 176 private void createDestination() throws InvocationTargetException { 177 File file = new File(fDestinationDirectory); 178 if (!file.exists() || !file.isDirectory()) { 179 if (!file.mkdirs()) 180 throw new InvocationTargetException(new Exception (PDEPlugin.getResourceString("ExportWizard.badDirectory"))); } 182 } 183 184 protected void doExport(String featureID, String version, String featureLocation, String os, String ws, String arch, IProgressMonitor monitor) 185 throws CoreException, InvocationTargetException { 186 monitor.beginTask("", 5); monitor.setTaskName(PDEPlugin.getResourceString("FeatureExportJob.taskName")); try { 189 HashMap properties = createBuildProperties(os, ws, arch); 190 makeScript(featureID, version, os, ws, arch, featureLocation); 191 monitor.worked(1); 192 runScript(getBuildScriptName(featureLocation), getBuildExecutionTargets(), 193 properties, new SubProgressMonitor(monitor, 2)); 194 runScript(getAssemblyScriptName(featureID, os, ws, arch, featureLocation), new String []{"main"}, properties, new SubProgressMonitor(monitor, 2)); 196 } finally { 197 monitor.done(); 198 } 199 } 200 201 protected HashMap createBuildProperties(String os, String ws, String arch) { 202 if (fBuildProperties == null) { 203 fBuildProperties = new HashMap(15); 204 fBuildProperties.put(IXMLConstants.PROPERTY_BUILD_TEMP, fBuildTempLocation + "/destination"); fBuildProperties.put(IXMLConstants.PROPERTY_TEMP_FOLDER, fBuildTempLocation + "/temp.folder"); fBuildProperties.put(IXMLConstants.PROPERTY_FEATURE_TEMP_FOLDER, fBuildTempLocation + "/destination"); fBuildProperties.put(IXMLConstants.PROPERTY_INCLUDE_CHILDREN, "true"); fBuildProperties.put("eclipse.running", "true"); fBuildProperties.put(IXMLConstants.PROPERTY_BASE_OS, os); 210 fBuildProperties.put(IXMLConstants.PROPERTY_BASE_WS, ws); 211 fBuildProperties.put(IXMLConstants.PROPERTY_BASE_ARCH, arch); 212 fBuildProperties.put(IXMLConstants.PROPERTY_BASE_NL, TargetPlatform.getNL()); 213 fBuildProperties.put(IXMLConstants.PROPERTY_BOOTCLASSPATH, BaseBuildAction.getBootClasspath()); 214 IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore(); 215 fBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_FAIL_ON_ERROR, "false"); fBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_DEBUG_INFO, store.getBoolean(PROP_JAVAC_DEBUG_INFO) ? "on" : "off"); fBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_VERBOSE, store.getString(PROP_JAVAC_VERBOSE)); 218 219 Preferences pref = JavaCore.getPlugin().getPluginPreferences(); 220 fBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE, pref.getString(JavaCore.COMPILER_SOURCE)); 221 fBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_TARGET, pref.getString(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM)); 222 223 fBuildProperties.put(IXMLConstants.PROPERTY_BUILD_DIRECTORY, fBuildTempLocation + "/assemblyLocation"); fBuildProperties.put(IXMLConstants.PROPERTY_BUILD_LABEL, "."); fBuildProperties.put(IXMLConstants.PROPERTY_COLLECTING_FOLDER, "."); String prefix = Platform.getOS().equals("macosx") ? "." : ""; fBuildProperties.put(IXMLConstants.PROPERTY_ARCHIVE_PREFIX, prefix); 229 if (fExportType == EXPORT_AS_ZIP) 230 fBuildProperties.put(IXMLConstants.PROPERTY_ARCHIVE_FULLPATH, fDestinationDirectory + File.separator + fZipFilename); 231 else 232 fBuildProperties.put(IXMLConstants.PROPERTY_ASSEMBLY_TMP, fDestinationDirectory); 233 } 234 return fBuildProperties; 235 } 236 237 private void makeScript(String featureID, String versionId, String os, String ws, String arch, String featureLocation) throws CoreException { 238 BuildScriptGenerator generator = new BuildScriptGenerator(); 239 generator.setBuildingOSGi(PDECore.getDefault().getModelManager().isOSGiRuntime()); 240 generator.setChildren(true); 241 generator.setWorkingDirectory(featureLocation); 242 generator.setDevEntries(getDevProperties()); 243 generator.setElements(new String [] {"feature@" + featureID + (versionId == null ? "" : ":" + versionId)}); generator.setPluginPath(getPaths()); 245 String format; 246 if (fExportType == EXPORT_AS_ZIP) 247 format = Platform.getOS().equals("macosx") ? "tarGz" : "antZip"; else 249 format = "folder"; BuildScriptGenerator.setOutputFormat(format); 251 BuildScriptGenerator.setForceUpdateJar(fExportType == EXPORT_AS_UPDATE_JARS); 252 BuildScriptGenerator.setEmbeddedSource(fExportSource && fExportType != EXPORT_AS_UPDATE_JARS); 253 BuildScriptGenerator.setConfigInfo(os + "," + ws + "," + arch); generator.generate(); 255 } 256 257 private String getDevProperties() { 258 if (fDevProperties == null) { 259 fDevProperties = ClasspathHelper.getDevEntriesProperties(fBuildTempLocation + "/dev.properties", false); } 261 return fDevProperties; 262 } 263 264 protected void runScript(String location, String [] targets, Map properties, 265 IProgressMonitor monitor) throws InvocationTargetException, CoreException { 266 AntRunner runner = new AntRunner(); 267 runner.addUserProperties(properties); 268 runner.setAntHome(location); 269 runner.setBuildFileLocation(location); 270 runner.addBuildListener("org.eclipse.pde.internal.ui.ant.ExportBuildListener"); runner.setExecutionTargets(targets); 272 runner.run(monitor); 273 } 274 275 private String getBuildScriptName(String featureLocation) { 276 return featureLocation + Path.SEPARATOR + "build.xml"; } 278 279 protected String getAssemblyScriptName(String featureID, String os, String ws, String arch, String featureLocation) { 280 return featureLocation + Path.SEPARATOR + "assemble." + featureID + "." + os + "." + ws + "." + arch + ".xml"; } 285 286 private String [] getBuildExecutionTargets() { 287 if (fExportSource && fExportType != EXPORT_AS_UPDATE_JARS) 288 return new String [] {"build.jars", "build.sources", "gather.logs"}; return new String [] {"build.jars", "gather.logs"}; } 291 292 public void deleteBuildFiles(IModel model) throws CoreException { 293 if (model == null) 294 return; 295 296 String directory = 297 (model instanceof IFeatureModel) 298 ? ((IFeatureModel) model).getInstallLocation() 299 : ((IPluginModelBase) model).getInstallLocation(); 300 301 if (model.getUnderlyingResource() != null && !isCustomBuild(model)) { 302 File dir = new File(directory); 303 File[] children = dir.listFiles(); 304 if (children != null) { 305 for (int i = 0; i < children.length; i++) { 306 if (!children[i].isDirectory()) { 307 String filename = children[i].getName(); 308 if (filename.equals("build.xml") || (filename.startsWith("assemble.") && filename.endsWith(".xml"))) { children[i].delete(); 311 } 312 } 313 } 314 } 315 } 316 317 if (model instanceof IFeatureModel) { 318 IFeature feature = ((IFeatureModel)model).getFeature(); 319 IFeatureChild[] children = feature.getIncludedFeatures(); 320 for (int i = 0; i < children.length; i++) { 321 IFeature ref = ((FeatureChild)children[i]).getReferencedFeature(); 322 if (ref != null) { 323 deleteBuildFiles(ref.getModel()); 324 } 325 } 326 327 IFeaturePlugin[] plugins = feature.getPlugins(); 328 PluginModelManager manager = PDECore.getDefault().getModelManager(); 329 for (int i = 0; i < plugins.length; i++) { 330 ModelEntry entry = manager.findEntry(plugins[i].getId()); 331 if (entry != null) { 332 deleteBuildFiles(entry.getActiveModel()); 333 } 334 } 335 } 336 } 337 338 protected boolean isCustomBuild(IModel model) throws CoreException { 339 IBuildModel buildModel = null; 340 IFile buildFile = model.getUnderlyingResource().getProject().getFile("build.properties"); if (buildFile.exists()) { 342 buildModel = new WorkspaceBuildModel(buildFile); 343 buildModel.load(); 344 } 345 if (buildModel != null) { 346 IBuild build = buildModel.getBuild(); 347 IBuildEntry entry = build.getEntry("custom"); if (entry != null) { 349 String [] tokens = entry.getTokens(); 350 for (int i = 0; i < tokens.length; i++) { 351 if (tokens[i].equals("true")) return true; 353 } 354 } 355 } 356 return false; 357 } 358 359 protected String [] getPaths() throws CoreException { 360 ArrayList paths = new ArrayList(); 361 IFeatureModel[] models = PDECore.getDefault().getWorkspaceModelManager().getFeatureModels(); 362 for (int i = 0; i < models.length; i++) { 363 paths.add(models[i].getInstallLocation() + Path.SEPARATOR + "feature.xml"); } 365 366 String [] plugins = TargetPlatform.createPluginPath(); 367 String [] features = (String []) paths.toArray(new String [paths.size()]); 368 String [] all = new String [plugins.length + paths.size()]; 369 System.arraycopy(plugins, 0, all, 0, plugins.length); 370 System.arraycopy(features, 0, all, plugins.length, features.length); 371 return all; 372 } 373 374 private static void createLogWriter() { 375 try { 376 String path = PDEPlugin.getDefault().getStateLocation().toOSString(); 377 logFile = new File(path, "exportLog.txt"); if (logFile.exists()) { 379 logFile.delete(); 380 logFile.createNewFile(); 381 } 382 writer = new PrintWriter(new FileWriter(logFile), true); 383 } catch (IOException e) { 384 } 385 } 386 387 public static PrintWriter getWriter() { 388 if (writer == null) 389 createLogWriter(); 390 return writer; 391 } 392 393 protected void cleanup(IProgressMonitor monitor) { 394 File scriptFile = null; 395 try { 396 scriptFile = createScriptFile(); 397 writer = new PrintWriter(new FileWriter(scriptFile), true); 398 generateHeader(writer); 399 generateDeleteZipTarget(writer); 400 generateCleanTarget(writer); 401 boolean errors = generateZipLogsTarget(writer); 402 generateClosingTag(writer); 403 writer.close(); 404 405 ArrayList targets = new ArrayList(); 406 targets.add("deleteZip"); if (errors) 408 targets.add("zip.logs"); targets.add("clean"); AntRunner runner = new AntRunner(); 411 runner.setBuildFileLocation(scriptFile.getAbsolutePath()); 412 runner.setExecutionTargets((String []) targets.toArray(new String [targets.size()])); 413 runner.run(monitor); 414 } catch (IOException e) { 415 } catch (CoreException e) { 416 } finally { 417 if (scriptFile != null && scriptFile.exists()) 418 scriptFile.delete(); 419 } 420 } 421 private File createScriptFile() throws IOException { 422 String path = PDEPlugin.getDefault().getStateLocation().toOSString(); 423 File zip = new File(path, "zip.xml"); if (zip.exists()) { 425 zip.delete(); 426 zip.createNewFile(); 427 } 428 return zip; 429 } 430 private void generateHeader(PrintWriter writer) { 431 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<project name=\"temp\" default=\"clean\" basedir=\".\">"); } 434 private void generateCleanTarget(PrintWriter writer) { 435 writer.println("<target name=\"clean\">"); writer.println("<delete dir=\"" + fBuildTempLocation + "\"/>"); writer.println("</target>"); } 439 private void generateDeleteZipTarget(PrintWriter writer) { 440 writer.println("<target name=\"deleteZip\">"); writer.println("<delete file=\"" + fDestinationDirectory + "/logs.zip\"/>"); writer.println("</target>"); } 444 445 private boolean generateZipLogsTarget(PrintWriter writer) { 446 if (logFile != null && logFile.exists() && logFile.length() > 0) { 447 writer.println("<target name=\"zip.logs\">"); writer.println("<zip zipfile=\"" + fDestinationDirectory + "/logs.zip\" basedir=\"" + fBuildTempLocation + "/temp.folder\"/>"); writer.println("</target>"); return true; 451 } 452 return false; 453 } 454 private void generateClosingTag(PrintWriter writer) { 455 writer.println("</project>"); } 457 462 public static Display getStandardDisplay() { 463 Display display; 464 display = Display.getCurrent(); 465 if (display == null) 466 display = Display.getDefault(); 467 return display; 468 } 469 470 private void asyncNotifyExportException(String errorMessage) { 471 getStandardDisplay().beep(); 472 MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEPlugin.getResourceString("FeatureExportJob.error"), errorMessage); done(new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null)); } 475 476 protected String getLogFoundMessage() { 477 return PDEPlugin.getFormattedMessage("ExportJob.error.message", fDestinationDirectory + File.separator + "logs.zip"); } 479 } 480 | Popular Tags |