KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > exports > FeatureExportOperation


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.exports;
12
13 import java.io.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Dictionary JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30
31 import org.eclipse.ant.core.AntCorePlugin;
32 import org.eclipse.ant.core.AntCorePreferences;
33 import org.eclipse.ant.core.AntRunner;
34 import org.eclipse.ant.core.IAntClasspathEntry;
35 import org.eclipse.ant.core.Property;
36 import org.eclipse.core.resources.IFile;
37 import org.eclipse.core.resources.IWorkspaceRunnable;
38 import org.eclipse.core.runtime.CoreException;
39 import org.eclipse.core.runtime.IPath;
40 import org.eclipse.core.runtime.IProgressMonitor;
41 import org.eclipse.core.runtime.IStatus;
42 import org.eclipse.core.runtime.OperationCanceledException;
43 import org.eclipse.core.runtime.Path;
44 import org.eclipse.core.runtime.Platform;
45 import org.eclipse.core.runtime.Preferences;
46 import org.eclipse.core.runtime.Status;
47 import org.eclipse.core.runtime.SubProgressMonitor;
48 import org.eclipse.jdt.core.JavaCore;
49 import org.eclipse.jdt.launching.JavaRuntime;
50 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
51 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
52 import org.eclipse.osgi.service.resolver.BundleDescription;
53 import org.eclipse.osgi.service.resolver.State;
54 import org.eclipse.pde.core.IModel;
55 import org.eclipse.pde.core.build.IBuild;
56 import org.eclipse.pde.core.build.IBuildEntry;
57 import org.eclipse.pde.core.build.IBuildModel;
58 import org.eclipse.pde.core.plugin.IPluginModelBase;
59 import org.eclipse.pde.core.plugin.PluginRegistry;
60 import org.eclipse.pde.core.plugin.TargetPlatform;
61 import org.eclipse.pde.internal.build.AbstractScriptGenerator;
62 import org.eclipse.pde.internal.build.BuildScriptGenerator;
63 import org.eclipse.pde.internal.build.IXMLConstants;
64 import org.eclipse.pde.internal.build.site.QualifierReplacer;
65 import org.eclipse.pde.internal.core.ClasspathHelper;
66 import org.eclipse.pde.internal.core.PDECore;
67 import org.eclipse.pde.internal.core.PDECoreMessages;
68 import org.eclipse.pde.internal.core.TargetPlatformHelper;
69 import org.eclipse.pde.internal.core.XMLPrintHandler;
70 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
71 import org.eclipse.pde.internal.core.feature.FeatureChild;
72 import org.eclipse.pde.internal.core.ifeature.IFeature;
73 import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
74 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
75 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
76 import org.eclipse.pde.internal.core.util.CoreUtility;
77 import org.osgi.framework.InvalidSyntaxException;
78 import org.w3c.dom.DOMException JavaDoc;
79 import org.w3c.dom.Document JavaDoc;
80 import org.w3c.dom.Element JavaDoc;
81
82 import com.ibm.icu.util.Calendar;
83
84 public class FeatureExportOperation implements IWorkspaceRunnable {
85     
86     // write to the ant build listener log
87
private static boolean fHasErrors;
88
89     // Location where the build takes place
90
protected String JavaDoc fBuildTempLocation;
91     private String JavaDoc fDevProperties;
92
93     protected HashMap JavaDoc fAntBuildProperties;
94
95     protected State fStateCopy;
96
97     protected static String JavaDoc FEATURE_POST_PROCESSING = "features.postProcessingSteps.properties"; //$NON-NLS-1$
98
protected static String JavaDoc PLUGIN_POST_PROCESSING = "plugins.postProcessingSteps.properties"; //$NON-NLS-1$
99

100     public static String JavaDoc getDate() {
101         final String JavaDoc empty = ""; //$NON-NLS-1$
102
int monthNbr = Calendar.getInstance().get(Calendar.MONTH) + 1;
103         String JavaDoc month = (monthNbr < 10 ? "0" : empty) + monthNbr; //$NON-NLS-1$
104

105         int dayNbr = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
106         String JavaDoc day = (dayNbr < 10 ? "0" : empty) + dayNbr; //$NON-NLS-1$
107

108         int hourNbr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
109         String JavaDoc hour = (hourNbr < 10 ? "0" : empty) + hourNbr; //$NON-NLS-1$
110

111         int minuteNbr = Calendar.getInstance().get(Calendar.MINUTE);
112         String JavaDoc minute = (minuteNbr < 10 ? "0" : empty) + minuteNbr; //$NON-NLS-1$
113

114         return empty + Calendar.getInstance().get(Calendar.YEAR) + month + day + hour + minute;
115     }
116
117     protected FeatureExportInfo fInfo;
118     
119     public FeatureExportOperation(FeatureExportInfo info) {
120         fInfo = info;
121         String JavaDoc qualifier = info.qualifier;
122         if (qualifier == null)
123             qualifier = getDate();
124         QualifierReplacer.setGlobalQualifier(qualifier);
125         fBuildTempLocation = PDECore.getDefault().getStateLocation().append("temp").toString(); //$NON-NLS-1$
126
}
127
128     public void run(IProgressMonitor monitor) throws CoreException {
129         try {
130             createDestination();
131             String JavaDoc[][] configurations = fInfo.targets;
132             if (configurations == null)
133                 configurations = new String JavaDoc[][] { null };
134             
135             monitor.beginTask("", configurations.length * fInfo.items.length * 10); //$NON-NLS-1$
136
for (int i = 0; i < configurations.length; i++) {
137                 for (int j = 0; j < fInfo.items.length; j++) {
138                     if (monitor.isCanceled())
139                         throw new OperationCanceledException();
140                     try {
141                         doExport((IFeatureModel) fInfo.items[j], configurations[i], new SubProgressMonitor(monitor, 9));
142                     } finally {
143                         cleanup(configurations[i], new SubProgressMonitor(monitor, 1));
144                     }
145                 }
146             }
147         } catch (InvocationTargetException JavaDoc e) {
148             throwCoreException(e);
149         }
150         monitor.done();
151     }
152     
153     protected void throwCoreException(InvocationTargetException JavaDoc e) throws CoreException {
154         String JavaDoc message = e.getTargetException().getMessage();
155         if (message != null && message.length() > 0) {
156             throw new CoreException(new Status(
157                     IStatus.ERROR,
158                     PDECore.PLUGIN_ID,
159                     IStatus.ERROR,
160                     message,
161                     null));
162         }
163     }
164     
165     private void createDestination(String JavaDoc os, String JavaDoc ws, String JavaDoc arch) throws InvocationTargetException JavaDoc {
166         if (!fInfo.toDirectory)
167             return;
168         File JavaDoc file = new File JavaDoc(fInfo.destinationDirectory, os + '.' + ws + '.' + arch);
169         if (!file.exists() || !file.isDirectory()) {
170             if (!file.mkdirs())
171                 throw new InvocationTargetException JavaDoc(new Exception JavaDoc(PDECoreMessages.ExportWizard_badDirectory));
172         }
173     }
174
175     private void doExport(IFeatureModel model, String JavaDoc os, String JavaDoc ws, String JavaDoc arch, IProgressMonitor monitor) throws CoreException, InvocationTargetException JavaDoc {
176         try {
177             String JavaDoc location = model.getInstallLocation();
178             if (fInfo.useJarFormat) {
179                 createPostProcessingFile(new File JavaDoc(location, FEATURE_POST_PROCESSING));
180                 createPostProcessingFile(new File JavaDoc(location, PLUGIN_POST_PROCESSING));
181             }
182             IFeature feature = model.getFeature();
183             doExport(feature.getId(), feature.getVersion(), location, os, ws, arch, monitor);
184         } finally {
185             deleteBuildFiles(model);
186         }
187     }
188     
189     protected void createPostProcessingFile(File JavaDoc file) {
190         FileOutputStream JavaDoc stream = null;
191         try {
192             stream = new FileOutputStream JavaDoc(file);
193             Properties JavaDoc prop = new Properties JavaDoc();
194             prop.put("*", "updateJar"); //$NON-NLS-1$ //$NON-NLS-2$
195
prop.store(stream, ""); //$NON-NLS-1$
196
stream.flush();
197         } catch (IOException JavaDoc e) {
198         } finally {
199             try {
200                 if (stream != null)
201                     stream.close();
202             } catch (IOException JavaDoc e) {
203             }
204         }
205     }
206
207     protected void doExport(String JavaDoc featureID, String JavaDoc version, String JavaDoc featureLocation, String JavaDoc os, String JavaDoc ws, String JavaDoc arch, IProgressMonitor monitor) throws CoreException, InvocationTargetException JavaDoc {
208         fHasErrors = false;
209         monitor.beginTask("", 9); //$NON-NLS-1$
210
monitor.setTaskName(PDECoreMessages.FeatureExportJob_taskName);
211         try {
212             HashMap JavaDoc properties = createAntBuildProperties(os, ws, arch);
213             BuildScriptGenerator generator = new BuildScriptGenerator();
214             setupGenerator(generator, featureID, version, os, ws, arch, featureLocation);
215             generator.generate();
216             monitor.worked(1);
217             runScript(getBuildScriptName(featureLocation), getBuildExecutionTargets(), properties, new SubProgressMonitor(monitor, 2));
218             runScript(getAssemblyScriptName(featureID, os, ws, arch, featureLocation), new String JavaDoc[] {"main"}, //$NON-NLS-1$
219
properties, new SubProgressMonitor(monitor, 2));
220             runScript(getPackagerScriptName(featureID, os, ws, arch, featureLocation), null, properties, new SubProgressMonitor(monitor, 2));
221             properties.put("destination.temp.folder", fBuildTempLocation + "/pde.logs"); //$NON-NLS-1$ //$NON-NLS-2$
222
runScript(getBuildScriptName(featureLocation), new String JavaDoc[] {"gather.logs"}, properties, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$
223
} finally {
224             monitor.done();
225         }
226     }
227
228     public void deleteBuildFiles(Object JavaDoc object) throws CoreException {
229         IModel model = null;
230         if (object instanceof BundleDescription) {
231             model = PluginRegistry.findModel((BundleDescription)object);
232         } else if (object instanceof IModel){
233             model = (IModel)object;
234         }
235         
236         if (model == null)
237             return;
238
239         if (model.getUnderlyingResource() != null && !isCustomBuild(model)) {
240             String JavaDoc directory = (model instanceof IFeatureModel) ? ((IFeatureModel) model).getInstallLocation() : ((IPluginModelBase) model).getInstallLocation();
241             File JavaDoc dir = new File JavaDoc(directory);
242             File JavaDoc[] children = dir.listFiles();
243             if (children != null) {
244                 for (int i = 0; i < children.length; i++) {
245                     if (!children[i].isDirectory()) {
246                         String JavaDoc filename = children[i].getName();
247                         if (filename.equals("build.xml") //$NON-NLS-1$
248
|| (filename.startsWith("javaCompiler.") && filename.endsWith(".args")) //$NON-NLS-1$ //$NON-NLS-2$
249
|| (filename.startsWith("assemble.") && filename.endsWith(".xml")) //$NON-NLS-1$ //$NON-NLS-2$
250
|| (filename.startsWith("package.") && filename.endsWith(".xml")) //$NON-NLS-1$ //$NON-NLS-2$
251
|| filename.equals(FEATURE_POST_PROCESSING)
252                                 || filename.equals(PLUGIN_POST_PROCESSING)) {
253                                 children[i].delete();
254                         }
255                     } else if (children[i].getName().equals("temp.folder")) { //$NON-NLS-1$
256
CoreUtility.deleteContent(children[i]);
257                     }
258                 }
259             }
260         }
261
262         if (model instanceof IFeatureModel) {
263             IFeature feature = ((IFeatureModel) model).getFeature();
264             IFeatureChild[] children = feature.getIncludedFeatures();
265             for (int i = 0; i < children.length; i++) {
266                 IFeature ref = ((FeatureChild) children[i]).getReferencedFeature();
267                 if (ref != null) {
268                     deleteBuildFiles(ref.getModel());
269                 }
270             }
271
272             IFeaturePlugin[] plugins = feature.getPlugins();
273             for (int i = 0; i < plugins.length; i++) {
274                 IPluginModelBase plugin = PluginRegistry.findModel(plugins[i].getId());
275                 if (plugin != null) {
276                     deleteBuildFiles(plugin);
277                 }
278             }
279         }
280     }
281
282     private String JavaDoc getBuildScriptName(String JavaDoc featureLocation) {
283         return featureLocation + IPath.SEPARATOR + "build.xml"; //$NON-NLS-1$
284
}
285
286     protected String JavaDoc getAssemblyScriptName(String JavaDoc featureID, String JavaDoc os, String JavaDoc ws, String JavaDoc arch, String JavaDoc featureLocation) {
287         return featureLocation + IPath.SEPARATOR + "assemble." //$NON-NLS-1$
288
+ featureID + "." + os + "." //$NON-NLS-1$ //$NON-NLS-2$
289
+ ws + "." + arch //$NON-NLS-1$
290
+ ".xml"; //$NON-NLS-1$
291
}
292
293     private String JavaDoc[] getBuildExecutionTargets() {
294         if (fInfo.exportSource)
295             return new String JavaDoc[] {"build.jars", "build.sources"}; //$NON-NLS-1$ //$NON-NLS-2$
296
return new String JavaDoc[] {"build.jars"}; //$NON-NLS-1$
297
}
298
299     protected void runScript(String JavaDoc location, String JavaDoc[] targets, Map JavaDoc properties, IProgressMonitor monitor) throws InvocationTargetException JavaDoc, CoreException {
300         AntRunner runner = new AntRunner();
301         runner.addUserProperties(properties);
302         runner.setAntHome(location);
303         runner.setBuildFileLocation(location);
304         runner.addBuildListener("org.eclipse.pde.internal.core.ant.ExportBuildListener"); //$NON-NLS-1$
305
runner.setExecutionTargets(targets);
306         if (fInfo.signingInfo != null) {
307             AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences();
308             IAntClasspathEntry entry = preferences.getToolsJarEntry();
309             if (entry != null) {
310                 IAntClasspathEntry[] classpath = preferences.getAntHomeClasspathEntries();
311                 URL JavaDoc[] urls = new URL JavaDoc[classpath.length + 2];
312                 for (int i = 0; i < classpath.length; i++) {
313                     urls[i] = classpath[i].getEntryURL();
314                 }
315                 IPath path = new Path(entry.getEntryURL().toString()).removeLastSegments(2);
316                 path = path.append("bin"); //$NON-NLS-1$
317
try {
318                     urls[classpath.length] = new URL JavaDoc(path.toString());
319                 } catch (MalformedURLException JavaDoc e) {
320                     urls[classpath.length] = entry.getEntryURL();
321                 } finally {
322                     urls[classpath.length + 1] = entry.getEntryURL();
323                 }
324                 runner.setCustomClasspath(urls);
325             }
326         }
327         runner.run(monitor);
328     }
329
330     protected String JavaDoc getPackagerScriptName(String JavaDoc featureID, String JavaDoc os, String JavaDoc ws, String JavaDoc arch, String JavaDoc featureLocation) {
331         return featureLocation + IPath.SEPARATOR + "package." //$NON-NLS-1$
332
+ featureID + "." + os + "." //$NON-NLS-1$ //$NON-NLS-2$
333
+ ws + "." + arch //$NON-NLS-1$
334
+ ".xml"; //$NON-NLS-1$
335
}
336
337     protected HashMap JavaDoc createAntBuildProperties(String JavaDoc os, String JavaDoc ws, String JavaDoc arch) {
338         if (fAntBuildProperties == null) {
339             fAntBuildProperties = new HashMap JavaDoc(15);
340             
341             List JavaDoc defaultProperties= AntCorePlugin.getPlugin().getPreferences().getDefaultProperties();
342             ListIterator JavaDoc li = defaultProperties.listIterator();
343             while (li.hasNext()) {
344                 Property prop = (Property)li.next();
345                 fAntBuildProperties.put(prop.getName(), prop.getValue());
346             }
347             
348             if (fInfo.signingInfo != null) {
349                 fAntBuildProperties.put("sign.alias", fInfo.signingInfo[0]); //$NON-NLS-1$
350
fAntBuildProperties.put("sign.keystore", fInfo.signingInfo[1]); //$NON-NLS-1$
351
fAntBuildProperties.put("sign.storepass", fInfo.signingInfo[2]); //$NON-NLS-1$
352
}
353             if (fInfo.jnlpInfo != null) {
354                 fAntBuildProperties.put("jnlp.codebase", fInfo.jnlpInfo[0]); //$NON-NLS-1$
355
fAntBuildProperties.put("jnlp.j2se", fInfo.jnlpInfo[1]); //$NON-NLS-1$
356
}
357
358             fAntBuildProperties.put(IXMLConstants.PROPERTY_BUILD_TEMP, fBuildTempLocation + "/destination"); //$NON-NLS-1$
359
fAntBuildProperties.put(IXMLConstants.PROPERTY_FEATURE_TEMP_FOLDER, fBuildTempLocation + "/destination"); //$NON-NLS-1$
360
fAntBuildProperties.put(IXMLConstants.PROPERTY_INCLUDE_CHILDREN, "true"); //$NON-NLS-1$
361
fAntBuildProperties.put("eclipse.running", "true"); //$NON-NLS-1$ //$NON-NLS-2$
362
fAntBuildProperties.put(IXMLConstants.PROPERTY_BASE_OS, os);
363             fAntBuildProperties.put(IXMLConstants.PROPERTY_BASE_WS, ws);
364             fAntBuildProperties.put(IXMLConstants.PROPERTY_BASE_ARCH, arch);
365             fAntBuildProperties.put(IXMLConstants.PROPERTY_BASE_NL, TargetPlatform.getNL());
366             fAntBuildProperties.put(IXMLConstants.PROPERTY_BOOTCLASSPATH, BuildUtilities.getBootClasspath());
367             IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
368             IExecutionEnvironment[] envs = manager.getExecutionEnvironments();
369             for (int i = 0; i < envs.length; i++) {
370                 String JavaDoc id = envs[i].getId();
371                 if (id != null)
372                     fAntBuildProperties.put(id, BuildUtilities.getBootClasspath(id));
373             }
374             fAntBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_FAIL_ON_ERROR, "false"); //$NON-NLS-1$
375
fAntBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_DEBUG_INFO, "on"); //$NON-NLS-1$
376
fAntBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_VERBOSE, "false"); //$NON-NLS-1$
377

378             Preferences pref = JavaCore.getPlugin().getPluginPreferences();
379             String JavaDoc source = pref.getString(JavaCore.COMPILER_SOURCE);
380             fAntBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE, source);
381             String JavaDoc target = pref.getString(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
382             fAntBuildProperties.put(IXMLConstants.PROPERTY_JAVAC_TARGET, target);
383
384             // for the assembler...
385
fAntBuildProperties.put(IXMLConstants.PROPERTY_BUILD_DIRECTORY, fBuildTempLocation + "/assemblyLocation"); //$NON-NLS-1$
386
fAntBuildProperties.put(IXMLConstants.PROPERTY_BUILD_LABEL, "."); //$NON-NLS-1$
387
fAntBuildProperties.put(IXMLConstants.PROPERTY_COLLECTING_FOLDER, "."); //$NON-NLS-1$
388
String JavaDoc prefix = Platform.getOS().equals("macosx") ? "." : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
389
fAntBuildProperties.put(IXMLConstants.PROPERTY_ARCHIVE_PREFIX, prefix);
390
391             if (!fInfo.toDirectory) {
392                 String JavaDoc filename = fInfo.zipFileName;
393                 if (fInfo.targets != null) {
394                     int i = filename.lastIndexOf('.');
395                     filename = filename.substring(0, i) + '.' + os + '.' + ws + '.' + arch + filename.substring(i);
396                 }
397                 fAntBuildProperties.put(IXMLConstants.PROPERTY_ARCHIVE_FULLPATH, fInfo.destinationDirectory + File.separator + filename);
398             } else {
399                 String JavaDoc dir = fInfo.destinationDirectory;
400                 if (fInfo.targets != null)
401                     dir += File.separatorChar + os + '.' + ws + '.' + arch;
402                 fAntBuildProperties.put(IXMLConstants.PROPERTY_ASSEMBLY_TMP, dir);
403             }
404             fAntBuildProperties.put(IXMLConstants.PROPERTY_TAR_ARGS, ""); //$NON-NLS-1$
405
}
406         return fAntBuildProperties;
407     }
408
409     private String JavaDoc getOS(IFeature feature) {
410         String JavaDoc os = feature.getOS();
411         if (os == null || os.trim().length() == 0 || os.indexOf(',') != -1 || os.equals("*")) //$NON-NLS-1$
412
return TargetPlatform.getOS();
413         return os;
414     }
415
416     private String JavaDoc getWS(IFeature feature) {
417         String JavaDoc ws = feature.getWS();
418         if (ws == null || ws.trim().length() == 0 || ws.indexOf(',') != -1 || ws.equals("*")) //$NON-NLS-1$
419
return TargetPlatform.getWS();
420         return ws;
421     }
422
423     private String JavaDoc getOSArch(IFeature feature) {
424         String JavaDoc arch = feature.getArch();
425         if (arch == null || arch.trim().length() == 0 || arch.indexOf(',') != -1 || arch.equals("*")) //$NON-NLS-1$
426
return TargetPlatform.getOSArch();
427         return arch;
428     }
429
430     protected void createDestination() throws InvocationTargetException JavaDoc {
431         File JavaDoc file = new File JavaDoc(fInfo.destinationDirectory);
432         if (!file.exists() || !file.isDirectory()) {
433             if (!file.mkdirs())
434                 throw new InvocationTargetException JavaDoc(new Exception JavaDoc(PDECoreMessages.ExportWizard_badDirectory));
435         }
436     }
437
438     protected void doExport(IFeatureModel model, String JavaDoc[] config, IProgressMonitor monitor) throws CoreException, InvocationTargetException JavaDoc {
439         if (config == null) {
440             IFeature feature = model.getFeature();
441             doExport(model, getOS(feature), getWS(feature), getOSArch(feature), monitor);
442         } else {
443             createDestination(config[0], config[1], config[2]);
444             doExport(model, config[0], config[1], config[2], monitor);
445         }
446     }
447         
448     protected void setupGenerator(BuildScriptGenerator generator, String JavaDoc featureID, String JavaDoc versionId, String JavaDoc os, String JavaDoc ws, String JavaDoc arch, String JavaDoc featureLocation) throws CoreException {
449         generator.setBuildingOSGi(true);
450         generator.setChildren(true);
451         generator.setWorkingDirectory(featureLocation);
452         generator.setDevEntries(getDevProperties());
453         generator.setElements(new String JavaDoc[] {"feature@" + featureID + (versionId == null ? "" : ":" + versionId)}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
454
generator.setPluginPath(getPaths());
455         generator.setReportResolutionErrors(false);
456         generator.setIgnoreMissingPropertiesFile(true);
457         generator.setSignJars(fInfo.signingInfo != null);
458         generator.setGenerateJnlp(fInfo.jnlpInfo != null);
459         String JavaDoc config = os + ',' + ws + ',' + arch;
460         AbstractScriptGenerator.setConfigInfo(config); //This needs to be set before we set the format
461
String JavaDoc format;
462         if (fInfo.toDirectory)
463             format = config + '-' + IXMLConstants.FORMAT_FOLDER;
464         else
465             format = config + '-' + IXMLConstants.FORMAT_ANTZIP;
466         generator.setArchivesFormat(format);
467         generator.setPDEState(getState(os, ws, arch));
468         generator.setNextId(TargetPlatformHelper.getPDEState().getNextId());
469         generator.setStateExtraData(TargetPlatformHelper.getBundleClasspaths(TargetPlatformHelper.getPDEState()), TargetPlatformHelper.getPatchMap(TargetPlatformHelper.getPDEState()));
470         AbstractScriptGenerator.setForceUpdateJar(false);
471         AbstractScriptGenerator.setEmbeddedSource(fInfo.exportSource);
472     }
473     
474     protected State getState(String JavaDoc os, String JavaDoc ws, String JavaDoc arch) {
475         State main = TargetPlatformHelper.getState();
476         if (os.equals(TargetPlatform.getOS())
477                 && ws.equals(TargetPlatform.getWS())
478                 && arch.equals(TargetPlatform.getOSArch())) {
479             return main;
480         }
481         if (fStateCopy == null) {
482             copyState(main);
483         }
484         
485         Dictionary JavaDoc[] dictionaries = fStateCopy.getPlatformProperties();
486         for (int i = 0; i < dictionaries.length; i++) {
487             Dictionary JavaDoc properties = dictionaries[i];
488             properties.put("osgi.os", os); //$NON-NLS-1$
489
properties.put("osgi.ws", ws); //$NON-NLS-1$
490
properties.put("osgi.arch", arch); //$NON-NLS-1$
491
}
492         fStateCopy.resolve(false);
493         return fStateCopy;
494     }
495     
496     protected void copyState(State state) {
497         fStateCopy = state.getFactory().createState(state);
498         fStateCopy.setResolver(Platform.getPlatformAdmin().getResolver());
499         fStateCopy.setPlatformProperties(state.getPlatformProperties());
500     }
501     
502     private String JavaDoc getDevProperties() {
503         if (fDevProperties == null) {
504             fDevProperties = ClasspathHelper.getDevEntriesProperties(fBuildTempLocation + "/dev.properties", false); //$NON-NLS-1$
505
}
506         return fDevProperties;
507     }
508
509
510     protected boolean isCustomBuild(IModel model) throws CoreException {
511         IBuildModel buildModel = null;
512         IFile buildFile = model.getUnderlyingResource().getProject().getFile("build.properties"); //$NON-NLS-1$
513
if (buildFile.exists()) {
514             buildModel = new WorkspaceBuildModel(buildFile);
515             buildModel.load();
516         }
517         if (buildModel != null) {
518             IBuild build = buildModel.getBuild();
519             if (build == null)
520                 return false;
521             IBuildEntry entry = build.getEntry("custom"); //$NON-NLS-1$
522
if (entry != null) {
523                 String JavaDoc[] tokens = entry.getTokens();
524                 for (int i = 0; i < tokens.length; i++) {
525                     if (tokens[i].equals("true")) //$NON-NLS-1$
526
return true;
527                 }
528             }
529         }
530         return false;
531     }
532
533     protected String JavaDoc[] getPaths() {
534         return TargetPlatformHelper.getFeaturePaths();
535     }
536     
537     protected void cleanup(String JavaDoc[] config, IProgressMonitor monitor) {
538         monitor.beginTask("", 2); //$NON-NLS-1$
539
// clear out some cached values that depend on the configuration being built.
540
fDevProperties = null;
541         fAntBuildProperties = null;
542
543         File JavaDoc scriptFile = null;
544         try {
545             scriptFile = createScriptFile("zip.xml"); //$NON-NLS-1$
546
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
547             Document JavaDoc doc = factory.newDocumentBuilder().newDocument();
548             
549             Element JavaDoc root = doc.createElement("project"); //$NON-NLS-1$
550
root.setAttribute("name", "temp"); //$NON-NLS-1$ //$NON-NLS-2$
551
root.setAttribute("default", "clean"); //$NON-NLS-1$ //$NON-NLS-2$
552
root.setAttribute("basedir", "."); //$NON-NLS-1$ //$NON-NLS-2$
553
doc.appendChild(root);
554             
555             Element JavaDoc target = doc.createElement("target"); //$NON-NLS-1$
556
target.setAttribute("name", "clean"); //$NON-NLS-1$ //$NON-NLS-2$
557
Element JavaDoc child = doc.createElement("delete"); //$NON-NLS-1$
558
child.setAttribute("dir", fBuildTempLocation); //$NON-NLS-1$
559
target.appendChild(child);
560             root.appendChild(target);
561                             
562             if (fHasErrors) {
563                 target = doc.createElement("target"); //$NON-NLS-1$
564
target.setAttribute("name", "zip.logs"); //$NON-NLS-1$ //$NON-NLS-2$
565
child = doc.createElement("zip"); //$NON-NLS-1$
566
child.setAttribute("zipfile", fInfo.destinationDirectory + logName(config)); //$NON-NLS-1$
567
child.setAttribute("basedir", fBuildTempLocation + "/pde.logs"); //$NON-NLS-1$ //$NON-NLS-2$
568
target.appendChild(child);
569                 root.appendChild(target);
570             }
571             XMLPrintHandler.writeFile(doc, scriptFile);
572             
573             String JavaDoc[] targets = fHasErrors
574                         ? new String JavaDoc[] {"zip.logs", "clean"} //$NON-NLS-1$ //$NON-NLS-2$
575
: new String JavaDoc[] {"clean"}; //$NON-NLS-1$
576
AntRunner runner = new AntRunner();
577             runner.setBuildFileLocation(scriptFile.getAbsolutePath());
578             runner.setExecutionTargets(targets);
579             runner.run(new SubProgressMonitor(monitor, 1));
580         } catch (FactoryConfigurationError JavaDoc e) {
581         } catch (ParserConfigurationException JavaDoc e) {
582         } catch (CoreException e) {
583         } catch (IOException JavaDoc e) {
584         } finally {
585             if (scriptFile != null && scriptFile.exists())
586                 scriptFile.delete();
587             monitor.done();
588         }
589     }
590
591     protected File JavaDoc createScriptFile(String JavaDoc filename) throws IOException JavaDoc {
592         String JavaDoc path = PDECore.getDefault().getStateLocation().toOSString();
593         File JavaDoc zip = new File JavaDoc(path, filename);
594         if (zip.exists()) {
595             zip.delete();
596             zip.createNewFile();
597         }
598         return zip;
599     }
600
601     private String JavaDoc logName(String JavaDoc[] config) {
602         if (config == null)
603             return "/logs.zip"; //$NON-NLS-1$
604
return "/logs." + config[0] + '.' + config[1] + '.' + config[2] + ".zip"; //$NON-NLS-1$ //$NON-NLS-2$
605
}
606
607     protected void createFeature(String JavaDoc featureID, String JavaDoc featureLocation, String JavaDoc[] config, boolean includeLauncher) throws IOException JavaDoc {
608         File JavaDoc file = new File JavaDoc(featureLocation);
609         if (!file.exists() || !file.isDirectory())
610             file.mkdirs();
611         
612         try {
613             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
614             Document JavaDoc doc = factory.newDocumentBuilder().newDocument();
615             Element JavaDoc root = doc.createElement("feature"); //$NON-NLS-1$
616
root.setAttribute("id", featureID); //$NON-NLS-1$
617
root.setAttribute("version", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$
618
doc.appendChild(root);
619             
620             if (includeLauncher) {
621                 IFeatureModel model = PDECore.getDefault().getFeatureModelManager().getDeltaPackFeature();
622                 if (model != null) {
623                     IFeature feature = model.getFeature();
624                     Element JavaDoc includes = doc.createElement("includes"); //$NON-NLS-1$
625
includes.setAttribute("id", feature.getId()); //$NON-NLS-1$
626
includes.setAttribute("version", feature.getVersion()); //$NON-NLS-1$
627
root.appendChild(includes);
628                 }
629             }
630             Dictionary JavaDoc environment = new Hashtable JavaDoc(4);
631             environment.put("osgi.os", config[0]); //$NON-NLS-1$
632
environment.put("osgi.ws", config[1]); //$NON-NLS-1$
633
environment.put("osgi.arch", config[2]); //$NON-NLS-1$
634
environment.put("osgi.nl", config[3]); //$NON-NLS-1$
635

636             for (int i = 0; i < fInfo.items.length; i++) {
637                 if (fInfo.items[i] instanceof IFeatureModel) {
638                     IFeature feature = ((IFeatureModel) fInfo.items[i]).getFeature();
639                     Element JavaDoc includes = doc.createElement("includes"); //$NON-NLS-1$
640
includes.setAttribute("id", feature.getId()); //$NON-NLS-1$
641
includes.setAttribute("version", feature.getVersion()); //$NON-NLS-1$
642
root.appendChild(includes);
643                 } else {
644                     BundleDescription bundle = null;
645                     if (fInfo.items[i] instanceof IPluginModelBase) {
646                         bundle = ((IPluginModelBase)fInfo.items[i]).getBundleDescription();
647                     }
648                     if (bundle == null) {
649                         if (fInfo.items[i] instanceof BundleDescription)
650                             bundle = (BundleDescription)fInfo.items[i];
651                     }
652                     if (bundle == null)
653                         continue;
654                     if (shouldAddPlugin(bundle, environment)) {
655                         Element JavaDoc plugin = doc.createElement("plugin"); //$NON-NLS-1$
656
plugin.setAttribute("id", bundle.getSymbolicName()); //$NON-NLS-1$
657
plugin.setAttribute("version", bundle.getVersion().toString()); //$NON-NLS-1$
658
setAdditionalAttributes(plugin, bundle);
659                         root.appendChild(plugin);
660                     }
661                 }
662             }
663             XMLPrintHandler.writeFile(doc, new File JavaDoc(file, "feature.xml")); //$NON-NLS-1$
664
} catch (DOMException JavaDoc e1) {
665         } catch (FactoryConfigurationError JavaDoc e1) {
666         } catch (ParserConfigurationException JavaDoc e1) {
667         }
668     }
669     
670     protected void setAdditionalAttributes(Element JavaDoc plugin, BundleDescription bundle){
671     }
672     
673     public boolean hasErrors() {
674         return fHasErrors;
675     }
676
677     public static void errorFound() {
678         fHasErrors = true;
679     }
680
681     protected boolean shouldAddPlugin(BundleDescription bundle, Dictionary JavaDoc environment) {
682         String JavaDoc filterSpec = bundle.getPlatformFilter();
683         try {
684             return (filterSpec == null|| PDECore.getDefault().getBundleContext().createFilter(filterSpec).match(environment));
685         } catch (InvalidSyntaxException e) {
686         }
687         return false;
688     }
689 }
690
Popular Tags