KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Ben Pryor - Bug 148288
11  *******************************************************************************/

12 package org.eclipse.pde.internal.build;
13
14 import java.io.*;
15 import java.util.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.internal.build.builder.*;
20 import org.eclipse.pde.internal.build.packager.PackageScriptGenerator;
21 import org.eclipse.pde.internal.build.site.BuildTimeSiteFactory;
22 import org.eclipse.update.core.*;
23 import org.osgi.framework.Version;
24
25 public class BuildScriptGenerator extends AbstractScriptGenerator {
26     /**
27      * Indicates whether the assemble script should contain the archive
28      * generation statement.
29      */

30     protected boolean generateArchive = true;
31     /**
32      * Indicates whether scripts for a feature's children should be generated.
33      */

34     protected boolean children = true;
35
36     /**
37      * Indicates whether the resulting archive will contain a group of all the configurations
38      */

39     protected boolean groupConfigs = false;
40
41     /**
42      * Source elements for script generation.
43      */

44     protected String JavaDoc[] elements;
45
46     /**
47      * Additional dev entries for the compile classpath.
48      */

49     protected DevClassPathHelper devEntries;
50
51     protected boolean recursiveGeneration = true;
52     protected boolean generateBuildScript = true;
53     protected boolean includePlatformIndependent = true;
54     protected boolean signJars = false;
55     protected boolean generateJnlp = false;
56     protected boolean generateFeatureVersionSuffix = false;
57     private String JavaDoc product;
58     //Map configuration with the expected output format: key: Config, value: string
59
private HashMap archivesFormat;
60
61     private String JavaDoc archivesFormatAsString;
62     
63     /**
64      * flag indicating if the assemble script should be generated
65      */

66     private boolean generateAssembleScript = true;
67
68     /** flag indicating if missing properties file should be logged */
69     private boolean ignoreMissingPropertiesFile = true;
70
71     /** flag indicating if we should generate the plugin & feature versions lists */
72     private boolean generateVersionsList = false;
73
74     private Properties antProperties = null;
75     private BundleDescription[] bundlesToBuild;
76     
77     private static final String JavaDoc PROPERTY_ARCHIVESFORMAT = "archivesFormat"; //$NON-NLS-1$
78

79     /**
80      *
81      * @throws CoreException
82      */

83     public void generate() throws CoreException {
84         
85         if (archivesFormatAsString != null) {
86             realSetArchivesFormat(archivesFormatAsString);
87             archivesFormatAsString = null;
88         }
89         
90         List plugins = new ArrayList(5);
91         List features = new ArrayList(5);
92         try {
93             AbstractScriptGenerator.setStaticAntProperties(antProperties);
94             
95             sortElements(features, plugins);
96             pluginsForFilterRoots = plugins;
97             featuresForFilterRoots = features;
98             getSite(true); //This forces the creation of the siteFactory which contains all the parameters necessary to initialize.
99
//TODO To avoid this. What would be necessary is use the BuildTimeSiteFactory to store the values that are stored in the AbstractScriptGenerator and to pass the parameters to a new BuildTimeSiteFacotry when created.
100
//More over this would allow us to remove some of the setters when creating a new featurebuildscriptgenerator.
101

102             // It is not required to filter in the two first generateModels, since
103
// it is only for the building of a single plugin
104
generateModels(plugins);
105             generateFeatures(features);
106             flushState();
107         } finally {
108             AbstractScriptGenerator.setStaticAntProperties(null);
109         }
110     }
111
112     /**
113      * Separate elements by kind.
114      */

115     protected void sortElements(List features, List plugins) {
116         if (elements == null)
117             return;
118         for (int i = 0; i < elements.length; i++) {
119             int index = elements[i].indexOf('@');
120             String JavaDoc type = elements[i].substring(0, index);
121             String JavaDoc element = elements[i].substring(index + 1);
122             if (type.equals("plugin") || type.equals("fragment")) //$NON-NLS-1$ //$NON-NLS-2$
123
plugins.add(element);
124             else if (type.equals("feature")) //$NON-NLS-1$
125
features.add(element);
126         }
127     }
128
129     /**
130      *
131      * @param models
132      * @throws CoreException
133      */

134     protected void generateModels(List models) throws CoreException {
135         ModelBuildScriptGenerator generator = null;
136         try {
137             for (Iterator iterator = models.iterator(); iterator.hasNext();) {
138                 generator = new ModelBuildScriptGenerator();
139                 generator.setReportResolutionErrors(reportResolutionErrors);
140                 generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile);
141                 //Filtering is not required here, since we are only generating the build for a plugin or a fragment
142
String JavaDoc[] modelInfo = getNameAndVersion((String JavaDoc) iterator.next());
143                 generator.setBuildSiteFactory(siteFactory);
144                 generator.setModelId(modelInfo[0], modelInfo[1]);
145
146                 generator.setPluginPath(pluginPath);
147                 generator.setDevEntries(devEntries);
148                 generator.setCompiledElements(generator.getCompiledElements());
149                 generator.setBuildingOSGi(isBuildingOSGi());
150                 generator.setSignJars(signJars);
151                 generator.generate();
152             }
153             if (bundlesToBuild != null)
154                 for (int i = 0; i < bundlesToBuild.length; i++) {
155                     generator = new ModelBuildScriptGenerator();
156                     generator.setReportResolutionErrors(reportResolutionErrors);
157                     generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile);
158                     //Filtering is not required here, since we are only generating the build for a plugin or a fragment
159
generator.setBuildSiteFactory(siteFactory);
160                     generator.setModel(bundlesToBuild[i]);
161
162                     generator.setPluginPath(pluginPath);
163                     generator.setDevEntries(devEntries);
164                     generator.setCompiledElements(generator.getCompiledElements());
165                     generator.setBuildingOSGi(isBuildingOSGi());
166                     generator.setSignJars(signJars);
167                     generator.generate();
168                 }
169         } finally {
170             if (generator != null)
171                 generator.getSite(false).getRegistry().cleanupOriginalState();
172         }
173     }
174
175     private String JavaDoc[] getNameAndVersion(String JavaDoc id) {
176         int versionPosition = id.indexOf(":"); //$NON-NLS-1$
177
String JavaDoc[] result = new String JavaDoc[2];
178         if (versionPosition != -1) {
179             result[1] = id.substring(versionPosition + 1);
180             result[0] = id.substring(0, versionPosition);
181         } else
182             result[0] = id;
183         return result;
184     }
185
186     protected void generateFeatures(List features) throws CoreException {
187         AssemblyInformation assemblageInformation = null;
188         assemblageInformation = new AssemblyInformation();
189
190         FeatureBuildScriptGenerator generator = null;
191         try {
192             for (Iterator i = features.iterator(); i.hasNext();) {
193                 String JavaDoc[] featureInfo = getNameAndVersion((String JavaDoc) i.next());
194                 generator = new FeatureBuildScriptGenerator(featureInfo[0], featureInfo[1], assemblageInformation);
195                 generator.setGenerateIncludedFeatures(this.recursiveGeneration);
196                 generator.setAnalyseChildren(this.children);
197                 generator.setSourceFeatureGeneration(false);
198                 generator.setBinaryFeatureGeneration(true);
199                 generator.setScriptGeneration(generateBuildScript);
200                 generator.setPluginPath(pluginPath);
201                 generator.setBuildSiteFactory(siteFactory);
202                 generator.setDevEntries(devEntries);
203                 generator.setSourceToGather(new SourceFeatureInformation());//
204
generator.setCompiledElements(generator.getCompiledElements());
205                 generator.setBuildingOSGi(isBuildingOSGi());
206                 generator.includePlatformIndependent(includePlatformIndependent);
207                 generator.setReportResolutionErrors(reportResolutionErrors);
208                 generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile);
209                 generator.setSignJars(signJars);
210                 generator.setGenerateJnlp(generateJnlp);
211                 generator.setGenerateVersionSuffix(generateFeatureVersionSuffix);
212                 generator.setProduct(product);
213                 generator.generate();
214             }
215
216             if (generator != null && generateAssembleScript == true) {
217                 String JavaDoc[] featureInfo = null;
218                 if (features.size() == 1)
219                     featureInfo = getNameAndVersion((String JavaDoc) features.get(0));
220                 else
221                     featureInfo = new String JavaDoc[] {"all"}; //$NON-NLS-1$
222

223                 generateAssembleScripts(assemblageInformation, featureInfo, generator.siteFactory);
224
225                 if (features.size() == 1)
226                     featureInfo = getNameAndVersion((String JavaDoc) features.get(0));
227                 else
228                     featureInfo = new String JavaDoc[] {""}; //$NON-NLS-1$
229

230                 generatePackageScripts(assemblageInformation, featureInfo, generator.siteFactory);
231             }
232             if (generateVersionsList)
233                 generateVersionsLists(assemblageInformation);
234         } finally {
235             if (generator != null)
236                 generator.getSite(false).getRegistry().cleanupOriginalState();
237         }
238     }
239
240     protected void generateVersionsLists(AssemblyInformation assemblageInformation) throws CoreException {
241         if (assemblageInformation == null)
242             return;
243         List configs = getConfigInfos();
244         Set features = new HashSet();
245         Set plugins = new HashSet();
246         Properties versions = new Properties();
247
248         //For each configuration, save the version of all the features in a file
249
//and save the version of all the plug-ins in another file
250
for (Iterator iter = configs.iterator(); iter.hasNext();) {
251             Config config = (Config) iter.next();
252             String JavaDoc configString = config.toStringReplacingAny("_", ANY_STRING); //$NON-NLS-1$
253

254             //Features
255
Collection list = assemblageInformation.getFeatures(config);
256             versions.clear();
257             features.addAll(list);
258             String JavaDoc featureFile = DEFAULT_FEATURE_VERSION_FILENAME_PREFIX + '.' + configString + PROPERTIES_FILE_SUFFIX;
259             readVersions(versions, featureFile);
260             for (Iterator i = list.iterator(); i.hasNext();) {
261                 IFeature feature = (IFeature) i.next();
262                 VersionedIdentifier id = feature.getVersionedIdentifier();
263                 recordVersion(id.getIdentifier(), new Version(id.getVersion().toString()), versions);
264             }
265             saveVersions(versions, featureFile);
266
267             //Plugins
268
list = assemblageInformation.getPlugins(config);
269             versions.clear();
270             plugins.addAll(list);
271             String JavaDoc pluginFile = DEFAULT_PLUGIN_VERSION_FILENAME_PREFIX + '.' + configString + PROPERTIES_FILE_SUFFIX;
272             readVersions(versions, pluginFile);
273             for (Iterator i = list.iterator(); i.hasNext();) {
274                 BundleDescription bundle = (BundleDescription) i.next();
275                 recordVersion(bundle.getSymbolicName(), bundle.getVersion(), versions);
276             }
277             saveVersions(versions, pluginFile);
278         }
279
280         //Create a file containing all the feature versions
281
versions.clear();
282         String JavaDoc featureFile = DEFAULT_FEATURE_VERSION_FILENAME_PREFIX + PROPERTIES_FILE_SUFFIX;
283         readVersions(versions, featureFile);
284         for (Iterator i = features.iterator(); i.hasNext();) {
285             IFeature feature = (IFeature) i.next();
286             VersionedIdentifier id = feature.getVersionedIdentifier();
287             recordVersion(id.getIdentifier(), new Version(id.getVersion().toString()), versions);
288         }
289         saveVersions(versions, featureFile);
290
291         //Create a file containing all the plugin versions
292
versions.clear();
293         String JavaDoc pluginVersion = DEFAULT_PLUGIN_VERSION_FILENAME_PREFIX + PROPERTIES_FILE_SUFFIX;
294         readVersions(versions, pluginVersion);
295         for (Iterator i = plugins.iterator(); i.hasNext();) {
296             BundleDescription bundle = (BundleDescription) i.next();
297             recordVersion(bundle.getSymbolicName(), bundle.getVersion(), versions);
298         }
299         saveVersions(versions, pluginVersion);
300     }
301
302     protected void recordVersion(String JavaDoc name, Version version, Properties properties) {
303         String JavaDoc versionString = version.toString();
304         if (properties.containsKey(name)) {
305             Version existing = new Version((String JavaDoc) properties.get(name));
306             if (version.compareTo(existing) >= 0) {
307                 properties.put(name, versionString);
308             }
309         } else {
310             properties.put(name, versionString);
311         }
312         String JavaDoc suffix = '_' + String.valueOf(version.getMajor()) + '.' + String.valueOf(version.getMinor()) + '.' + String.valueOf(version.getMicro());
313         properties.put(name + suffix, versionString);
314     }
315
316     private String JavaDoc getFilePath(String JavaDoc fileName) {
317         return workingDirectory + '/' + fileName;
318     }
319
320     protected void readVersions(Properties properties, String JavaDoc fileName) {
321         String JavaDoc location = getFilePath(fileName);
322         try {
323             InputStream is = new BufferedInputStream(new FileInputStream(location));
324             try {
325                 properties.load(is);
326             } finally {
327                 is.close();
328             }
329         } catch (IOException e) {
330             //Ignore
331
}
332     }
333
334     protected void saveVersions(Properties properties, String JavaDoc fileName) throws CoreException {
335         String JavaDoc location = getFilePath(fileName);
336         try {
337             OutputStream os = new BufferedOutputStream(new FileOutputStream(location));
338             try {
339                 properties.store(os, null);
340             } finally {
341                 os.close();
342             }
343         } catch (IOException e) {
344             String JavaDoc message = NLS.bind(Messages.exception_writingFile, location);
345             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, null));
346         }
347     }
348
349     protected void generatePackageScripts(AssemblyInformation assemblageInformation, String JavaDoc[] featureInfo, BuildTimeSiteFactory factory) throws CoreException {
350         PackageScriptGenerator assembler = null;
351         assembler = new PackageScriptGenerator(workingDirectory, assemblageInformation, featureInfo[0]);
352         assembler.setSignJars(signJars);
353         assembler.setGenerateJnlp(generateJnlp);
354         assembler.setArchivesFormat(getArchivesFormat());
355         assembler.setProduct(product);
356         assembler.setBuildSiteFactory(factory);
357         assembler.setGroupConfigs(groupConfigs);
358         assembler.generate();
359     }
360
361     private void generateAssembleScripts(AssemblyInformation assemblageInformation, String JavaDoc[] featureInfo, BuildTimeSiteFactory factory) throws CoreException {
362         AssembleScriptGenerator assembler = new AssembleScriptGenerator(workingDirectory, assemblageInformation, featureInfo[0]);
363         assembler.setSignJars(signJars);
364         assembler.setGenerateJnlp(generateJnlp);
365         assembler.setArchivesFormat(getArchivesFormat());
366         assembler.setProduct(product);
367         assembler.setBuildSiteFactory(factory);
368         assembler.setGroupConfigs(groupConfigs);
369         assembler.generate();
370     }
371
372     public void setGenerateArchive(boolean generateArchive) {
373         this.generateArchive = generateArchive;
374     }
375
376     /**
377      *
378      * @param children
379      */

380     public void setChildren(boolean children) {
381         this.children = children;
382     }
383
384     /**
385      *
386      * @param devEntries
387      */

388     public void setDevEntries(String JavaDoc devEntries) {
389         if (devEntries != null)
390             this.devEntries = new DevClassPathHelper(devEntries);
391     }
392
393     /**
394      *
395      * @param elements
396      */

397     public void setElements(String JavaDoc[] elements) {
398         this.elements = elements;
399     }
400
401     /**
402      * Sets the recursiveGeneration.
403      *
404      * @param recursiveGeneration
405      * The recursiveGeneration to set
406      */

407     public void setRecursiveGeneration(boolean recursiveGeneration) {
408         this.recursiveGeneration = recursiveGeneration;
409     }
410
411     /**
412      * @param generateAssembleScript
413      * The generateAssembleScript to set.
414      */

415     public void setGenerateAssembleScript(boolean generateAssembleScript) {
416         this.generateAssembleScript = generateAssembleScript;
417     }
418
419     /**
420      * Whether or not to generate plugin & feature versions lists
421      * @param generateVersionsList
422      */

423     public void setGenerateVersionsList(boolean generateVersionsList) {
424         this.generateVersionsList = generateVersionsList;
425     }
426
427     /**
428      * @param value The reportResolutionErrors to set.
429      */

430     public void setReportResolutionErrors(boolean value) {
431         this.reportResolutionErrors = value;
432     }
433
434     /**
435      * @param value The ignoreMissingPropertiesFile to set.
436      */

437     public void setIgnoreMissingPropertiesFile(boolean value) {
438         ignoreMissingPropertiesFile = value;
439     }
440
441     public void setProduct(String JavaDoc value) {
442         product = value;
443     }
444
445     public void setSignJars(boolean value) {
446         signJars = value;
447     }
448
449     public void setGenerateJnlp(boolean value) {
450         generateJnlp = value;
451     }
452
453     public void setGenerateFeatureVersionSuffix(boolean value) {
454         generateFeatureVersionSuffix = value;
455     }
456
457     private class ArchiveTable extends HashMap {
458         private static final long serialVersionUID = -3063402400461435816L;
459
460         public ArchiveTable(int size) {
461             super(size);
462         }
463
464         public Object JavaDoc get(Object JavaDoc arg0) {
465             Object JavaDoc result = super.get(arg0);
466             if (result == null)
467                 result = IXMLConstants.FORMAT_ANTZIP;
468             return result;
469         }
470     }
471     
472     public void setArchivesFormat(String JavaDoc archivesFormatAsString) {
473         this.archivesFormatAsString = archivesFormatAsString;
474     }
475     
476     public void realSetArchivesFormat(String JavaDoc archivesFormatAsString) throws CoreException {
477         if (Utils.getPropertyFormat(PROPERTY_ARCHIVESFORMAT).equalsIgnoreCase(archivesFormatAsString)) {
478             archivesFormat = new ArchiveTable(0);
479             return;
480         }
481
482         archivesFormat = new ArchiveTable(getConfigInfos().size());
483         String JavaDoc[] configs = Utils.getArrayFromStringWithBlank(archivesFormatAsString, "&"); //$NON-NLS-1$
484
for (int i = 0; i < configs.length; i++) {
485             String JavaDoc[] configElements = Utils.getArrayFromStringWithBlank(configs[i], ","); //$NON-NLS-1$
486
if (configElements.length != 3) {
487                 IStatus error = new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, IPDEBuildConstants.EXCEPTION_CONFIG_FORMAT, NLS.bind(Messages.error_configWrongFormat, configs[i]), null);
488                 throw new CoreException(error);
489             }
490             String JavaDoc[] archAndFormat = Utils.getArrayFromStringWithBlank(configElements[2], "-"); //$NON-NLS-1$
491
if (archAndFormat.length != 2) {
492                 String JavaDoc message = NLS.bind(Messages.invalid_archivesFormat, archivesFormatAsString);
493                 IStatus status = new Status(IStatus.ERROR,IPDEBuildConstants.PI_PDEBUILD , message);
494                 throw new CoreException(status);
495             }
496
497             Config aConfig = new Config(configElements[0], configElements[1], archAndFormat[0]);
498             if (getConfigInfos().contains(aConfig)) {
499                 archivesFormat.put(aConfig, archAndFormat[1]);
500             }
501         }
502     }
503
504     protected HashMap getArchivesFormat() {
505         if (archivesFormat == null) {
506             try {
507                 //If not set, pass in the empty property to trigger the default value to be loaded
508
realSetArchivesFormat(Utils.getPropertyFormat(PROPERTY_ARCHIVESFORMAT));
509             } catch (CoreException e) {
510                 //ignore
511
}
512         }
513         return archivesFormat;
514     }
515
516     public void includePlatformIndependent(boolean b) {
517         includePlatformIndependent = b;
518     }
519
520     public void setGroupConfigs(boolean value) {
521         groupConfigs = value;
522     }
523
524     public void setImmutableAntProperties(Properties properties) {
525         antProperties = properties;
526     }
527     
528     public void setBundles(BundleDescription[] bundles) {
529         bundlesToBuild = bundles;
530     }
531 }
532
Popular Tags