KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > AssembleConfigScriptGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 - Initial API and implementation
10  * G&H Softwareentwicklung GmbH - internationalization implementation (bug 150933)
11  * Prosyst - create proper OSGi bundles (bug 174157)
12  *******************************************************************************/

13 package org.eclipse.pde.internal.build;
14
15 import java.io.File JavaDoc;
16 import java.util.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.service.resolver.BundleDescription;
19 import org.eclipse.pde.build.Constants;
20 import org.eclipse.pde.internal.build.ant.*;
21 import org.eclipse.pde.internal.build.builder.ModelBuildScriptGenerator;
22 import org.eclipse.update.core.IFeature;
23 import org.eclipse.update.core.PluginEntry;
24
25 /**
26  * Generate an assemble script for a given feature and a given config. It
27  * generates all the instruction to zip the listed plugins and features.
28  */

29 public class AssembleConfigScriptGenerator extends AbstractScriptGenerator {
30     protected String JavaDoc directory; // representing the directory where to generate the file
31
protected String JavaDoc featureId;
32     protected Config configInfo;
33     protected IFeature[] features; // the features that will be assembled
34
protected IFeature[] allFeatures; //the set of all the features that have been considered
35
protected BundleDescription[] plugins;
36     protected String JavaDoc filename;
37     protected Collection rootFileProviders;
38     protected Properties pluginsPostProcessingSteps;
39     protected Properties featuresPostProcessingSteps;
40     protected ArrayList addedByPermissions = new ArrayList(); //contains the list of files and folders that have been added to an archive by permission management
41

42     private static final String JavaDoc PROPERTY_SOURCE = "source"; //$NON-NLS-1$
43
private static final String JavaDoc PROPERTY_ELEMENT_NAME = "elementName"; //$NON-NLS-1$
44

45     private static final String JavaDoc UPDATEJAR = "updateJar"; //$NON-NLS-1$
46
private static final String JavaDoc FLAT = "flat"; //$NON-NLS-1$
47

48     private static final byte BUNDLE = 0;
49     private static final byte FEATURE = 1;
50
51     protected static final String JavaDoc FOLDER = "folder"; //$NON-NLS-1$
52
protected static final String JavaDoc FILE = "file"; //$NON-NLS-1$
53
protected String JavaDoc PROPERTY_ECLIPSE_PLUGINS = "eclipse.plugins"; //$NON-NLS-1$
54
protected String JavaDoc PROPERTY_ECLIPSE_FEATURES = "eclipse.features"; //$NON-NLS-1$
55
private boolean signJars;
56     private boolean generateJnlp;
57
58     private String JavaDoc archiveFormat;
59     private boolean groupConfigs = false;
60     private String JavaDoc product;
61     private ProductFile productFile = null;
62
63     public AssembleConfigScriptGenerator() {
64         super();
65     }
66
67     public void initialize(String JavaDoc directoryName, String JavaDoc feature, Config configurationInformation, Collection elementList, Collection featureList, Collection allFeaturesList, Collection rootProviders) throws CoreException {
68         this.directory = directoryName;
69         this.featureId = feature;
70         this.configInfo = configurationInformation;
71         this.rootFileProviders = rootProviders != null ? rootProviders : new ArrayList(0);
72
73         this.features = new IFeature[featureList.size()];
74         featureList.toArray(this.features);
75
76         this.allFeatures = new IFeature[allFeaturesList.size()];
77         allFeaturesList.toArray(this.allFeatures);
78
79         this.plugins = new BundleDescription[elementList.size()];
80         this.plugins = (BundleDescription[]) elementList.toArray(this.plugins);
81
82         openScript(directoryName, getTargetName() + ".xml"); //$NON-NLS-1$
83
loadPostProcessingSteps();
84     }
85
86     private void loadProduct() {
87         if (product == null || product.startsWith("${")) { //$NON-NLS-1$
88
productFile = null;
89             return;
90         }
91         String JavaDoc productPath = findFile(product, false);
92         if (productPath == null)
93             productPath = product;
94         File JavaDoc f = new File JavaDoc(productPath);
95         if (f.exists() && f.isFile()) {
96             try {
97                 productFile = new ProductFile(productPath, configInfo.getOs());
98             } catch (CoreException e) {
99                 // TODO log
100
}
101         } else {
102             //TODO log
103
}
104     }
105
106     private String JavaDoc computeIconsList() {
107         String JavaDoc result = Utils.getPropertyFormat(PROPERTY_LAUNCHER_ICONS);
108         if (productFile == null)
109             return result;
110         String JavaDoc[] icons = productFile.getIcons();
111         for (int i = 0; i < icons.length; i++) {
112             String JavaDoc location = findFile(icons[i], true);
113             if (location != null)
114                 result += ", " + Utils.getPropertyFormat(PROPERTY_BASEDIR) + '/' + location; //$NON-NLS-1$
115
else {
116                 result += ", " + Utils.getPropertyFormat(PROPERTY_BUILD_DIRECTORY) + '/' + DEFAULT_PLUGIN_LOCATION + '/' + icons[i]; //$NON-NLS-1$
117
result += ", " + Utils.getPropertyFormat(PROPERTY_BUILD_DIRECTORY) + '/' + DEFAULT_FEATURE_LOCATION + '/' + icons[i]; //$NON-NLS-1$
118
}
119         }
120         return result;
121     }
122
123     private void loadPostProcessingSteps() {
124         try {
125             pluginsPostProcessingSteps = readProperties(AbstractScriptGenerator.getWorkingDirectory(), DEFAULT_PLUGINS_POSTPROCESSINGSTEPS_FILENAME_DESCRIPTOR, IStatus.INFO);
126             featuresPostProcessingSteps = readProperties(AbstractScriptGenerator.getWorkingDirectory(), DEFAULT_FEATURES_POSTPROCESSINGSTEPS_FILENAME_DESCRIPTOR, IStatus.INFO);
127         } catch (CoreException e) {
128             //Ignore
129
}
130     }
131
132     public void generate() {
133         loadProduct();
134         generatePrologue();
135         generateInitializationSteps();
136         generateGatherBinPartsCalls();
137         if (embeddedSource)
138             generateGatherSourceCalls();
139         generatePostProcessingSteps();
140         generateBrandingCalls();
141         generateArchivingSteps();
142         generateEpilogue();
143     }
144
145     /**
146      *
147      */

148     private void generateBrandingCalls() {
149         String JavaDoc install = Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER); //$NON-NLS-1$
150
script.printBrandTask(install, computeIconsList(), Utils.getPropertyFormat(PROPERTY_LAUNCHER_NAME), Utils.getPropertyFormat(PROPERTY_OS));
151     }
152
153     private void generateArchivingSteps() {
154         if (FORMAT_FOLDER.equalsIgnoreCase(archiveFormat)) {
155             generateMoveRootFiles();
156             return;
157         }
158
159         if (FORMAT_ZIP.equalsIgnoreCase(archiveFormat)) {
160             generateZipTarget();
161             return;
162         }
163
164         if (FORMAT_ANTZIP.equalsIgnoreCase(archiveFormat)) {
165             generateAntZipTarget();
166             return;
167         }
168
169         if (FORMAT_ANTTAR.equalsIgnoreCase(archiveFormat)) {
170             generateAntTarTarget();
171             return;
172         }
173
174         if (FORMAT_TAR.equalsIgnoreCase(archiveFormat)) {
175             generateTarGZTasks(true);
176             return;
177         }
178     }
179
180     private void generateMoveRootFiles() {
181         if (rootFileProviders.size() == 0)
182             return;
183         
184         for (Iterator iter = rootFileProviders.iterator(); iter.hasNext();) {
185             Properties featureProperties = null;
186             try {
187                 featureProperties = AbstractScriptGenerator.readProperties(new Path(((IFeature) iter.next()).getURL().getFile()).removeLastSegments(1).toOSString(), PROPERTIES_FILE, IStatus.OK);
188                 Utils.generatePermissions(featureProperties, configInfo, PROPERTY_ECLIPSE_BASE, script);
189             } catch (CoreException e) {
190                 //do nothing
191
}
192         }
193         
194         if (Platform.getOS().equals("win32")) { //$NON-NLS-1$
195
FileSet[] rootFiles = new FileSet[1];
196             rootFiles[0] = new FileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER), null, "**/**", null, null, null, null); //$NON-NLS-1$//$NON-NLS-2$
197
script.printMoveTask(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE), rootFiles, false);
198             script.printDeleteTask(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING), null, null); //$NON-NLS-1$
199
} else {
200             List params = new ArrayList(3);
201             params.add("-R"); //$NON-NLS-1$
202
params.add("."); //$NON-NLS-1$
203
params.add('\'' + Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '\'');
204             String JavaDoc rootFileFolder = Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING); //$NON-NLS-1$
205
script.printExecTask("cp", rootFileFolder + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER), params, null); //$NON-NLS-1$
206
script.printDeleteTask(rootFileFolder, null, null);
207         }
208     }
209
210     protected void generateGatherSourceCalls() {
211         Map properties = new HashMap(1);
212         properties.put(PROPERTY_DESTINATION_TEMP_FOLDER, Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS));
213         
214         for (int i = 0; i < plugins.length; i++) {
215             BundleDescription plugin = plugins[i];
216             String JavaDoc placeToGather = getLocation(plugin);
217             
218             script.printAntTask(DEFAULT_BUILD_SCRIPT_FILENAME, Utils.makeRelative(new Path(placeToGather), new Path(workingDirectory)).toOSString(), TARGET_GATHER_SOURCES, null, null, properties);
219             
220             Properties bundleProperties = (Properties) plugin.getUserObject();
221             //Source code for plugins with . on the classpath must be put in a folder in the final jar.
222
if (bundleProperties.get(WITH_DOT) == Boolean.TRUE) {
223                 String JavaDoc targetLocation = Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS) + '/' +ModelBuildScriptGenerator.getNormalizedName(plugin);
224                 String JavaDoc targetLocationSrc = targetLocation + "/src"; //$NON-NLS-1$
225

226                 //Find the source zip where it has been gathered and extract it in a folder
227
script.println("<unzip dest=\"" + AntScript.getEscaped(targetLocationSrc) + "\">"); //$NON-NLS-1$//$NON-NLS-2$
228
script.println("\t<fileset dir=\"" + AntScript.getEscaped(targetLocation) + "\" includes=\"**/*src.zip\" casesensitive=\"false\"/>"); //$NON-NLS-1$//$NON-NLS-2$
229
script.println("</unzip>"); //$NON-NLS-1$
230

231                 // Delete the source zip where it has been gathered since we extracted it
232
script.printDeleteTask(null, null, new FileSet[] {new FileSet(targetLocation, null, "**/*src.zip", null, null, null, "false")}); //$NON-NLS-1$ //$NON-NLS-2$//$NON-bNLS-3$
233
}
234         }
235
236         properties = new HashMap(1);
237         properties.put(PROPERTY_FEATURE_BASE, Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE));
238         for (int i = 0; i < features.length; i++) {
239             IFeature feature = features[i];
240             String JavaDoc placeToGather = feature.getURL().getPath();
241             int j = placeToGather.lastIndexOf(Constants.FEATURE_FILENAME_DESCRIPTOR);
242             if (j != -1)
243                 placeToGather = placeToGather.substring(0, j);
244             script.printAntTask(DEFAULT_BUILD_SCRIPT_FILENAME, Utils.makeRelative(new Path(placeToGather), new Path(workingDirectory)).toOSString(), TARGET_GATHER_SOURCES, null, null, properties);
245         }
246     }
247
248     protected void generatePackagingTargets() {
249         String JavaDoc fileName = Utils.getPropertyFormat(PROPERTY_SOURCE) + '/' + Utils.getPropertyFormat(PROPERTY_ELEMENT_NAME);
250         String JavaDoc fileExists = Utils.getPropertyFormat(PROPERTY_SOURCE) + '/' + Utils.getPropertyFormat(PROPERTY_ELEMENT_NAME) + "_exists"; //$NON-NLS-1$
251

252         script.printComment("Beginning of the jarUp task"); //$NON-NLS-1$
253
script.printTargetDeclaration(TARGET_JARUP, null, null, null, Messages.assemble_jarUp);
254         script.printAvailableTask(fileExists, fileName);
255         Map params = new HashMap(2);
256         params.put(PROPERTY_SOURCE, Utils.getPropertyFormat(PROPERTY_SOURCE));
257         params.put(PROPERTY_ELEMENT_NAME, Utils.getPropertyFormat(PROPERTY_ELEMENT_NAME));
258         script.printAntCallTask(TARGET_JARING, true, params);
259         script.printTargetEnd();
260
261         script.printTargetDeclaration(TARGET_JARING, null, fileExists, null, null);
262         script.printJarTask(fileName + ".jar", fileName, null, "merge"); //$NON-NLS-1$ //$NON-NLS-2$
263
script.printDeleteTask(fileName, null, null);
264
265         script.printTargetEnd();
266         script.printComment("End of the jarUp task"); //$NON-NLS-1$
267

268         script.printComment("Beginning of the jar signing target"); //$NON-NLS-1$
269
script.printTargetDeclaration(TARGET_JARSIGNING, null, null, null, Messages.sign_Jar);
270         if (generateJnlp)
271             script.printProperty(PROPERTY_UNSIGN, "true"); //$NON-NLS-1$
272
script.println("<eclipse.jarProcessor sign=\"" + Utils.getPropertyFormat(PROPERTY_SIGN) + "\" pack=\"" + Utils.getPropertyFormat(PROPERTY_PACK)+ "\" unsign=\"" + Utils.getPropertyFormat(PROPERTY_UNSIGN) + "\" jar=\"" + fileName + ".jar" + "\" alias=\"" + Utils.getPropertyFormat(PROPERTY_SIGN_ALIAS) + "\" keystore=\"" + Utils.getPropertyFormat(PROPERTY_SIGN_KEYSTORE) + "\" storepass=\"" + Utils.getPropertyFormat(PROPERTY_SIGN_STOREPASS) + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
273
script.printTargetEnd();
274         script.printComment("End of the jarUp task"); //$NON-NLS-1$
275
}
276
277     protected void generateGZipTarget(boolean assembling) {
278         //during the assemble stage, only zip if we aren't running the packager
279
script.printTargetDeclaration(TARGET_GZIP_RESULTS, null, null, assembling ? PROPERTY_RUN_PACKAGER : null, null);
280         script.println("<move file=\"" //$NON-NLS-1$
281
+ Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH) + "\" tofile=\"" //$NON-NLS-1$
282
+ Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/'
283                 + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER) + "/tmp.tar\"/>"); //$NON-NLS-1$
284
script.printGZip(Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER) + "/tmp.tar", //$NON-NLS-1$
285
Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH));
286         script.printTargetEnd();
287     }
288
289     protected void generatePrologue() {
290         script.printProjectDeclaration("Assemble " + featureId, TARGET_MAIN, null); //$NON-NLS-1$
291
script.printProperty(PROPERTY_ARCHIVE_NAME, computeArchiveName());
292         script.printProperty(PROPERTY_OS, configInfo.getOs());
293         script.printProperty(PROPERTY_WS, configInfo.getWs());
294         script.printProperty(PROPERTY_ARCH, configInfo.getArch());
295         script.printProperty(PROPERTY_SIGN, (signJars ? Boolean.TRUE : Boolean.FALSE).toString());
296         script.printProperty(PROPERTY_ASSEMBLY_TMP, Utils.getPropertyFormat(PROPERTY_BUILD_DIRECTORY) + "/tmp"); //$NON-NLS-1$
297
script.printProperty(PROPERTY_ECLIPSE_BASE, Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER));
298         script.printProperty(PROPERTY_ECLIPSE_PLUGINS, Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_PLUGIN_LOCATION);
299         script.printProperty(PROPERTY_ECLIPSE_FEATURES, Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_FEATURE_LOCATION);
300         script.printProperty(PROPERTY_ARCHIVE_FULLPATH, Utils.getPropertyFormat(PROPERTY_BASEDIR) + '/' + Utils.getPropertyFormat(PROPERTY_BUILD_LABEL) + '/' + Utils.getPropertyFormat(PROPERTY_ARCHIVE_NAME));
301         if (productFile != null && productFile.getLauncherName() != null)
302             script.printProperty(PROPERTY_LAUNCHER_NAME, productFile.getLauncherName());
303         script.printProperty(PROPERTY_TAR_ARGS, ""); //$NON-NLS-1$
304
generatePackagingTargets();
305         script.printTargetDeclaration(TARGET_MAIN, null, null, null, null);
306     }
307
308     private void generateInitializationSteps() {
309         if (BundleHelper.getDefault().isDebugging()) {
310             script.printEchoTask("basedir : " + Utils.getPropertyFormat(PROPERTY_BASEDIR)); //$NON-NLS-1$
311
script.printEchoTask("assemblyTempDir : " + Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP)); //$NON-NLS-1$
312
script.printEchoTask("eclipse.base : " + Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE)); //$NON-NLS-1$
313
script.printEchoTask("collectingFolder : " + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER)); //$NON-NLS-1$
314
script.printEchoTask("archivePrefix : " + Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX)); //$NON-NLS-1$
315
}
316
317         script.println("<condition property=\"" + PROPERTY_PLUGIN_ARCHIVE_PREFIX + "\" value=\"" + DEFAULT_PLUGIN_LOCATION + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
318
script.println("\t<equals arg1=\"" + Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
319
script.println("</condition>"); //$NON-NLS-1$
320
script.printProperty(PROPERTY_PLUGIN_ARCHIVE_PREFIX, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + DEFAULT_PLUGIN_LOCATION);
321
322         script.println();
323         script.println("<condition property=\"" + PROPERTY_FEATURE_ARCHIVE_PREFIX + "\" value=\"" + DEFAULT_FEATURE_LOCATION + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
324
script.println("\t<equals arg1=\"" + Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
325
script.println("</condition>"); //$NON-NLS-1$
326
script.printProperty(PROPERTY_FEATURE_ARCHIVE_PREFIX, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + DEFAULT_FEATURE_LOCATION);
327
328         script.println();
329
330         script.printDirName(PROPERTY_ARCHIVE_PARENT, Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH));
331         script.printMkdirTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_PARENT));
332         script.printMkdirTask(Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP));
333         script.printMkdirTask(Utils.getPropertyFormat(PROPERTY_BUILD_LABEL));
334     }
335
336     protected void generatePostProcessingSteps() {
337         for (int i = 0; i < plugins.length; i++) {
338             BundleDescription plugin = plugins[i];
339             generatePostProcessingSteps(plugin.getSymbolicName(), plugin.getVersion().toString(), (String JavaDoc) getFinalShape(plugin)[1], BUNDLE);
340         }
341
342         for (int i = 0; i < features.length; i++) {
343             IFeature feature = features[i];
344             generatePostProcessingSteps(feature.getVersionedIdentifier().getIdentifier(), feature.getVersionedIdentifier().getVersion().toString(), (String JavaDoc) getFinalShape(feature)[1], FEATURE);
345         }
346     }
347
348     protected void generateGatherBinPartsCalls() {
349         Map properties = new HashMap(1);
350         properties.put(PROPERTY_DESTINATION_TEMP_FOLDER, Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS));
351         for (int i = 0; i < plugins.length; i++) {
352             BundleDescription plugin = plugins[i];
353             String JavaDoc placeToGather = getLocation(plugin);
354             script.printAntTask(DEFAULT_BUILD_SCRIPT_FILENAME, Utils.makeRelative(new Path(placeToGather), new Path(workingDirectory)).toOSString(), TARGET_GATHER_BIN_PARTS, null, null, properties);
355         }
356
357         properties = new HashMap(1);
358         properties.put(PROPERTY_FEATURE_BASE, Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE));
359         for (int i = 0; i < features.length; i++) {
360             IFeature feature = features[i];
361             String JavaDoc placeToGather = feature.getURL().getPath();
362             int j = placeToGather.lastIndexOf(Constants.FEATURE_FILENAME_DESCRIPTOR);
363             if (j != -1)
364                 placeToGather = placeToGather.substring(0, j);
365             script.printAntTask(DEFAULT_BUILD_SCRIPT_FILENAME, Utils.makeRelative(new Path(placeToGather), new Path(workingDirectory)).toOSString(), TARGET_GATHER_BIN_PARTS, null, null, properties);
366         }
367
368         //This will generate gather.bin.parts call to features that provides files for the root
369
properties = new HashMap(1);
370         properties.put(PROPERTY_FEATURE_BASE, Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE));
371         for (Iterator iter = rootFileProviders.iterator(); iter.hasNext();) {
372             IFeature feature = (IFeature) iter.next();
373             String JavaDoc placeToGather = feature.getURL().getPath();
374             int j = placeToGather.lastIndexOf(Constants.FEATURE_FILENAME_DESCRIPTOR);
375             if (j != -1)
376                 placeToGather = placeToGather.substring(0, j);
377             script.printAntTask(DEFAULT_BUILD_SCRIPT_FILENAME, Utils.makeRelative(new Path(placeToGather), new Path(workingDirectory)).toOSString(), TARGET_GATHER_BIN_PARTS, null, null, properties);
378         }
379     }
380
381     private void generateSignJarCall(String JavaDoc name, String JavaDoc version, byte type) {
382         if (!signJars)
383             return;
384         Map properties = new HashMap(2);
385         properties.put(PROPERTY_SOURCE, type == BUNDLE ? Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS) : Utils.getPropertyFormat(PROPERTY_ECLIPSE_FEATURES));
386         properties.put(PROPERTY_ELEMENT_NAME, name + '_' + version);
387         script.printAntCallTask(TARGET_JARSIGNING, true, properties);
388     }
389
390     //generate the appropriate postProcessingCall
391
private void generatePostProcessingSteps(String JavaDoc name, String JavaDoc version, String JavaDoc style, byte type) {
392         if (FOLDER.equalsIgnoreCase(style))
393             return;
394         if (FILE.equalsIgnoreCase(style)) {
395             generateJarUpCall(name, version, type);
396             generateSignJarCall(name, version, type);
397             generateJNLPCall(name, version, type);
398             return;
399         }
400     }
401
402     private void generateJNLPCall(String JavaDoc name, String JavaDoc version, byte type) {
403         if (generateJnlp == false)
404             return;
405         if (type != FEATURE)
406             return;
407
408         String JavaDoc dir = type == BUNDLE ? Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS) : Utils.getPropertyFormat(PROPERTY_ECLIPSE_FEATURES);
409         String JavaDoc location = dir + '/' + name + '_' + version + ".jar"; //$NON-NLS-1$
410
script.println("<eclipse.jnlpGenerator feature=\"" + AntScript.getEscaped(location) + "\" codebase=\"" + Utils.getPropertyFormat(PROPERTY_JNLP_CODEBASE) + "\" j2se=\"" + Utils.getPropertyFormat(PROPERTY_JNLP_J2SE) + "\" locale=\"" + Utils.getPropertyFormat(PROPERTY_JNLP_LOCALE) + "\" generateOfflineAllowed=\"" + Utils.getPropertyFormat(PROPERTY_JNLP_GENOFFLINE) + "\" configInfo=\"" + Utils.getPropertyFormat(PROPERTY_JNLP_CONFIGS) + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
411
}
412
413     private boolean getUnpackClause(BundleDescription bundle) {
414         Set entries = (Set) ((Properties)bundle.getUserObject()).get(PLUGIN_ENTRY);
415         return ((PluginEntry) entries.iterator().next()).isUnpack();
416     }
417
418     protected Object JavaDoc[] getFinalShape(BundleDescription bundle) {
419         String JavaDoc style = getUnpackClause(bundle) ? FLAT : UPDATEJAR;
420         return getFinalShape(bundle.getSymbolicName(), bundle.getVersion().toString(), style, BUNDLE);
421     }
422
423     protected Object JavaDoc[] getFinalShape(IFeature feature) {
424         return getFinalShape(feature.getVersionedIdentifier().getIdentifier(), feature.getVersionedIdentifier().getVersion().toString(), FLAT, FEATURE);
425     }
426
427     protected Object JavaDoc[] getFinalShape(String JavaDoc name, String JavaDoc version, String JavaDoc initialShape, byte type) {
428         String JavaDoc style = initialShape;
429         style = getShapeOverride(name, type, style);
430
431         if (FLAT.equalsIgnoreCase(style)) {
432             //do nothing
433
return new Object JavaDoc[] {name + '_' + version, FOLDER};
434         }
435         if (UPDATEJAR.equalsIgnoreCase(style)) {
436             return new Object JavaDoc[] {name + '_' + version + ".jar", FILE}; //$NON-NLS-1$
437
}
438         return new Object JavaDoc[] {name + '_' + version, FOLDER};
439     }
440
441     private String JavaDoc getShapeOverride(String JavaDoc name, byte type, String JavaDoc initialStyle) {
442         String JavaDoc result = initialStyle;
443         Properties currentProperties = type == BUNDLE ? pluginsPostProcessingSteps : featuresPostProcessingSteps;
444         if (currentProperties.size() > 0) {
445             String JavaDoc styleFromFile = currentProperties.getProperty(name);
446             if (styleFromFile == null)
447                 styleFromFile = currentProperties.getProperty(DEFAULT_FINAL_SHAPE);
448             result = styleFromFile;
449         }
450         if (forceUpdateJarFormat)
451             result = UPDATEJAR;
452         return result;
453     }
454
455     private void generateJarUpCall(String JavaDoc name, String JavaDoc version, byte type) {
456         Map properties = new HashMap(2);
457         properties.put(PROPERTY_SOURCE, type == BUNDLE ? Utils.getPropertyFormat(PROPERTY_ECLIPSE_PLUGINS) : Utils.getPropertyFormat(PROPERTY_ECLIPSE_FEATURES));
458         properties.put(PROPERTY_ELEMENT_NAME, name + '_' + version);
459         script.printAntCallTask(TARGET_JARUP, true, properties);
460     }
461
462     private void generateEpilogue() {
463         if (!FORMAT_FOLDER.equalsIgnoreCase(archiveFormat))
464             script.printDeleteTask(Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP), null, null);
465         script.printTargetEnd();
466         if (FORMAT_TAR.equalsIgnoreCase(archiveFormat))
467             generateGZipTarget(true);
468         script.printProjectEnd();
469         script.close();
470         script = null;
471     }
472
473     public String JavaDoc getTargetName() {
474         return DEFAULT_ASSEMBLE_NAME + (featureId.equals("") ? "" : ('.' + featureId)) + (configInfo.equals(Config.genericConfig()) ? "" : ('.' + configInfo.toStringReplacingAny(".", ANY_STRING))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
475
}
476
477     private void generateZipTarget() {
478         final int parameterSize = 15;
479         List parameters = new ArrayList(parameterSize + 1);
480         for (int i = 0; i < plugins.length; i++) {
481             parameters.add(Utils.getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + (String JavaDoc) getFinalShape(plugins[i])[0]);
482             if (i % parameterSize == 0) {
483                 createZipExecCommand(parameters);
484                 parameters.clear();
485             }
486         }
487         if (!parameters.isEmpty()) {
488             createZipExecCommand(parameters);
489             parameters.clear();
490         }
491
492         if (!parameters.isEmpty()) {
493             createZipExecCommand(parameters);
494             parameters.clear();
495         }
496
497         for (int i = 0; i < features.length; i++) {
498             parameters.add(Utils.getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + (String JavaDoc) getFinalShape(features[i])[0]);
499             if (i % parameterSize == 0) {
500                 createZipExecCommand(parameters);
501                 parameters.clear();
502             }
503         }
504         if (!parameters.isEmpty()) {
505             createZipExecCommand(parameters);
506             parameters.clear();
507         }
508
509         createZipRootFileCommand();
510     }
511
512     /**
513      * Zip the root files
514      */

515     private void createZipRootFileCommand() {
516         if (rootFileProviders.size() == 0)
517             return;
518
519         List parameters = new ArrayList(1);
520         parameters.add("-r -q ${zipargs} '" + Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH) + "' . "); //$NON-NLS-1$ //$NON-NLS-2$
521
script.printExecTask("zip", Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING), parameters, null); //$NON-NLS-1$ //$NON-NLS-2$
522
}
523
524     private void createZipExecCommand(List parameters) {
525         parameters.add(0, "-r -q " + Utils.getPropertyFormat(PROPERTY_ZIP_ARGS) + " '" + Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH) + '\''); //$NON-NLS-1$ //$NON-NLS-2$
526
script.printExecTask("zip", Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP), parameters, null); //$NON-NLS-1$
527
}
528
529     protected String JavaDoc computeArchiveName() {
530         String JavaDoc extension = (FORMAT_TAR.equalsIgnoreCase(archiveFormat) || FORMAT_ANTTAR.equalsIgnoreCase(archiveFormat)) ? ".tar.gz" : ".zip"; //$NON-NLS-1$ //$NON-NLS-2$
531
return featureId + "-" + Utils.getPropertyFormat(PROPERTY_BUILD_ID_PARAM) + (configInfo.equals(Config.genericConfig()) ? "" : ("-" + configInfo.toStringReplacingAny(".", ANY_STRING))) + extension; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
532
}
533
534     public void generateTarGZTasks(boolean assembling) {
535         //This task only supports creation of archive with eclipse at the root
536
//Need to do the copy using cp because of the link
537
List parameters = new ArrayList(2);
538         if (rootFileProviders.size() > 0) {
539             parameters.add("-r '" + Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER) + "' '" + Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '\''); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
540
script.printExecTask("cp", Utils.getPropertyFormat(PROPERTY_BASEDIR), parameters, null); //$NON-NLS-1$
541

542             parameters.clear();
543             parameters.add("-rf '" + Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '\''); //$NON-NLS-1$ //$NON-NLS-2$
544
script.printExecTask("rm", Utils.getPropertyFormat(PROPERTY_BASEDIR), parameters, null); //$NON-NLS-1$
545
}
546         parameters.clear();
547         String JavaDoc tarArgs = assembling ? "-cvf '" : "-rvf '"; //$NON-NLS-1$//$NON-NLS-2$
548
parameters.add(Utils.getPropertyFormat(PROPERTY_TAR_ARGS) + tarArgs + Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH) + "' " + Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + ' '); //$NON-NLS-1$
549
script.printExecTask("tar", Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP), parameters, null); //$NON-NLS-1$
550

551         script.printAntCallTask(TARGET_GZIP_RESULTS, true, null );
552         
553         List args = new ArrayList(2);
554         args.add("-rf"); //$NON-NLS-1$
555
args.add('\'' + Utils.getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '\'');
556         script.printExecTask("rm", null, args, null); //$NON-NLS-1$
557
}
558
559     //TODO this code and the generateAntTarTarget() should be refactored using a factory or something like that.
560
protected void generateAntZipTarget() {
561         FileSet[] filesPlugins = new FileSet[plugins.length];
562         for (int i = 0; i < plugins.length; i++) {
563             Object JavaDoc[] shape = getFinalShape(plugins[i]);
564             filesPlugins[i] = new ZipFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_PLUGIN_LOCATION + '/' + (String JavaDoc) shape[0], shape[1] == FILE, null, null, null, null, null, Utils.getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + (String JavaDoc) shape[0], null, null);
565         }
566         if (plugins.length != 0)
567             script.printZipTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, filesPlugins);
568
569         FileSet[] filesFeatures = new FileSet[features.length];
570         for (int i = 0; i < features.length; i++) {
571             Object JavaDoc[] shape = getFinalShape(features[i]);
572             filesFeatures[i] = new ZipFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_FEATURE_LOCATION + '/' + (String JavaDoc) shape[0], shape[1] == FILE, null, null, null, null, null, Utils.getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + (String JavaDoc) shape[0], null, null);
573         }
574         if (features.length != 0)
575             script.printZipTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, filesFeatures);
576
577         if (rootFileProviders.size() == 0)
578             return;
579
580         if (groupConfigs) {
581             List allConfigs = getConfigInfos();
582             FileSet[] rootFiles = new FileSet[allConfigs.size()];
583             int i = 0;
584             for (Iterator iter = allConfigs.iterator(); iter.hasNext();) {
585                 Config elt = (Config) iter.next();
586                 rootFiles[i++] = new ZipFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + elt.toStringReplacingAny(".", ANY_STRING), false, null, "**/**", null, null, null, elt.toStringReplacingAny(".", ANY_STRING), null, null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
587
}
588             script.printZipTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, rootFiles);
589         } else {
590             FileSet[] permissionSets = generatePermissions(true);
591             FileSet[] rootFiles = new FileSet[permissionSets.length + 1];
592             String JavaDoc toExcludeFromArchive = Utils.getStringFromCollection(this.addedByPermissions, ","); //$NON-NLS-1$
593
System.arraycopy(permissionSets, 0, rootFiles, 1, permissionSets.length);
594             rootFiles[0] = new ZipFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER), false, null, "**/**", null, toExcludeFromArchive, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX), null, null); //$NON-NLS-1$//$NON-NLS-2$
595
script.printZipTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, rootFiles);
596         }
597     }
598
599     protected FileSet[] generatePermissions(boolean zip) {
600         String JavaDoc configInfix = configInfo.toString("."); //$NON-NLS-1$
601
String JavaDoc prefixPermissions = ROOT_PREFIX + configInfix + '.' + PERMISSIONS + '.';
602         String JavaDoc commonPermissions = ROOT_PREFIX + PERMISSIONS + '.';
603         ArrayList fileSets = new ArrayList();
604
605         for (Iterator iter = rootFileProviders.iterator(); iter.hasNext();) {
606             Properties featureProperties = null;
607             try {
608                 featureProperties = AbstractScriptGenerator.readProperties(new Path(((IFeature) iter.next()).getURL().getFile()).removeLastSegments(1).toOSString(), PROPERTIES_FILE, IStatus.OK);
609             } catch (CoreException e) {
610                 return new FileSet[0];
611             }
612
613             for (Iterator iter2 = featureProperties.entrySet().iterator(); iter2.hasNext();) {
614                 Map.Entry permission = (Map.Entry) iter2.next();
615                 String JavaDoc instruction = (String JavaDoc) permission.getKey();
616                 String JavaDoc parameters = (String JavaDoc) permission.getValue();
617                 String JavaDoc[] values = Utils.getArrayFromString(parameters);
618                 for (int i = 0; i < values.length; i++) {
619                     boolean isFile = ! values[i].endsWith("/"); //$NON-NLS-1$
620
String JavaDoc prefix = Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER); //$NON-NLS-1$
621
if (instruction.startsWith(prefixPermissions)) {
622                         addedByPermissions.add(values[i]);
623                         if (zip)
624                             fileSets.add(new ZipFileSet(prefix + (isFile ? '/' + values[i] : ""), isFile, null, isFile ? null : values[i] + "/**", null, null, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + (isFile ? '/' + values[i] : ""), null, instruction.substring(prefixPermissions.length()))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
625
else
626                             fileSets.add(new TarFileSet(prefix + (isFile ? '/' + values[i] : ""), isFile, null, isFile ? null : values[i] + "/**", null, null, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + (isFile ? '/' + values[i] : ""), null, instruction.substring(prefixPermissions.length()))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
627
continue;
628                     }
629                     if (instruction.startsWith(commonPermissions)) {
630                         addedByPermissions.add(values[i]);
631                         if (zip)
632                             fileSets.add(new ZipFileSet(prefix + (isFile ? '/' + values[i] : ""), isFile, null, isFile ? null : values[i] + "/**", null, null, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + (isFile ? '/' + values[i] : ""), null, instruction.substring(commonPermissions.length()))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
633
else
634                             fileSets.add(new TarFileSet(prefix + (isFile ? '/' + values[i] : ""), isFile, null, isFile ? null : values[i] + "/**", null, null, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + (isFile ? '/' + values[i] : ""), null, instruction.substring(commonPermissions.length()))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
635
continue;
636                     }
637                 }
638             }
639         }
640         return (FileSet[]) fileSets.toArray(new FileSet[fileSets.size()]);
641     }
642
643     //TODO this code andn the generateAntZipTarget() should be refactored using a factory or something like that.
644
private void generateAntTarTarget() {
645         FileSet[] filesPlugins = new FileSet[plugins.length];
646         for (int i = 0; i < plugins.length; i++) {
647             Object JavaDoc[] shape = getFinalShape(plugins[i]);
648             filesPlugins[i] = new TarFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_PLUGIN_LOCATION + '/' + (String JavaDoc) shape[0], shape[1] == FILE, null, null, null, null, null, Utils.getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + (String JavaDoc) shape[0], null, null);
649         }
650         if (plugins.length != 0)
651             script.printTarTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, filesPlugins);
652
653         FileSet[] filesFeatures = new FileSet[features.length];
654         for (int i = 0; i < features.length; i++) {
655             Object JavaDoc[] shape = getFinalShape(features[i]);
656             filesFeatures[i] = new TarFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + DEFAULT_FEATURE_LOCATION + '/' + (String JavaDoc) shape[0], shape[1] == FILE, null, null, null, null, null, Utils.getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + (String JavaDoc) shape[0], null, null);
657         }
658         if (features.length != 0)
659             script.printTarTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, filesFeatures);
660
661         if (rootFileProviders.size() == 0)
662             return;
663
664         FileSet[] permissionSets = generatePermissions(false);
665         FileSet[] rootFiles = new FileSet[permissionSets.length + 1];
666         System.arraycopy(permissionSets, 0, rootFiles, 1, permissionSets.length);
667         rootFiles[0] = new TarFileSet(Utils.getPropertyFormat(PROPERTY_ECLIPSE_BASE) + '/' + configInfo.toStringReplacingAny(".", ANY_STRING) + '/' + Utils.getPropertyFormat(PROPERTY_COLLECTING_FOLDER), false, null, "**/**", null, null, null, Utils.getPropertyFormat(PROPERTY_ARCHIVE_PREFIX), null, null); //$NON-NLS-1$//$NON-NLS-2$
668
script.printTarTask(Utils.getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, rootFiles);
669     }
670
671     public void setGenerateJnlp(boolean value) {
672         generateJnlp = value;
673     }
674
675     public void setSignJars(boolean value) {
676         signJars = value;
677     }
678
679     public void setProduct(String JavaDoc value) {
680         product = value;
681     }
682
683     public void setArchiveFormat(String JavaDoc archiveFormat) {
684         this.archiveFormat = archiveFormat;
685     }
686     
687     public void setGroupConfigs(boolean group) {
688         groupConfigs = group;
689     }
690 }
691
Popular Tags