KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > tasks > BuildManifestTask


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.pde.internal.build.tasks;
12
13 import java.io.*;
14 import java.util.*;
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Task;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.internal.build.*;
20 import org.eclipse.update.core.Feature;
21 import org.eclipse.update.core.IPluginEntry;
22 import org.eclipse.update.internal.core.FeatureExecutableFactory;
23
24 /**
25  * Used to create a build manifest file describing what plug-ins and versions
26  * were included in a build. It has to be executed after a fetch task.
27  */

28 public class BuildManifestTask extends Task implements IPDEBuildConstants, IXMLConstants {
29     private String JavaDoc buildId;
30     protected String JavaDoc buildName;
31     private String JavaDoc buildQualifier;
32     private String JavaDoc buildType;
33     protected boolean children = true;
34     protected String JavaDoc destination;
35     protected Properties directory;
36     protected String JavaDoc directoryLocation;
37     protected String JavaDoc[] elements;
38     protected String JavaDoc installLocation;
39
40     /**
41      * @see org.apache.tools.ant.Task#execute()
42      */

43     public void execute() throws BuildException {
44         try {
45             if (this.elements == null) {
46                 String JavaDoc message = TaskMessages.error_missingElement;
47                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ELEMENT_MISSING, message, null));
48             }
49             readDirectory();
50             PrintWriter output = new PrintWriter(new BufferedOutputStream(new FileOutputStream(destination)));
51             try {
52                 List entries = new ArrayList(20);
53                 for (int i = 0; i < elements.length; i++)
54                     collectEntries(entries, elements[i]);
55                 generatePrologue(output);
56                 generateEntries(output, entries);
57             } finally {
58                 output.close();
59             }
60         } catch (Exception JavaDoc e) {
61             throw new BuildException(e);
62         }
63     }
64
65     /**
66      *
67      * @param output
68      */

69     protected void generatePrologue(PrintWriter output) {
70         output.print("# Build Manifest for "); //$NON-NLS-1$
71
output.println(buildName);
72         output.println();
73         output.println("# The format of this file is:"); //$NON-NLS-1$
74
output.println("# <type>@<element>=<CVS tag>"); //$NON-NLS-1$
75
output.println();
76         String JavaDoc id = getBuildId();
77         if (id != null) {
78             output.print(PROPERTY_BUILD_ID + "="); //$NON-NLS-1$
79
output.println(id);
80         }
81         String JavaDoc type = getBuildType();
82         if (type != null) {
83             output.print(PROPERTY_BUILD_TYPE + "="); //$NON-NLS-1$
84
output.println(type);
85         }
86         String JavaDoc qualifier = getBuildQualifier();
87         if (qualifier != null) {
88             output.print(PROPERTY_BUILD_QUALIFIER + "="); //$NON-NLS-1$
89
output.println(qualifier);
90         }
91         output.println();
92     }
93
94     /**
95      *
96      * @return String
97      */

98     protected String JavaDoc getBuildId() {
99         if (buildId == null)
100             buildId = getProject().getProperty(PROPERTY_BUILD_ID);
101         return buildId;
102     }
103
104     /**
105      *
106      * @return String
107      */

108     protected String JavaDoc getBuildQualifier() {
109         if (buildQualifier == null)
110             buildQualifier = getProject().getProperty(PROPERTY_BUILD_QUALIFIER);
111         return buildQualifier;
112     }
113
114     /**
115      *
116      * @return String
117      */

118     protected String JavaDoc getBuildType() {
119         if (buildType == null)
120             buildType = getProject().getProperty(PROPERTY_BUILD_TYPE);
121         return buildType;
122     }
123
124     /**
125      *
126      * @param output
127      * @param entries
128      */

129     protected void generateEntries(PrintWriter output, List entries) {
130         Collections.sort(entries);
131         for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
132             String JavaDoc entry = (String JavaDoc) iterator.next();
133             output.println(entry);
134         }
135     }
136
137     /**
138      * Collects all the elements that are part of this build.
139      */

140     protected void collectEntries(List entries, String JavaDoc entry) throws CoreException {
141         String JavaDoc cvsInfo = directory.getProperty(entry);
142         if (cvsInfo == null) {
143             String JavaDoc message = NLS.bind(TaskMessages.error_missingDirectoryEntry, entry);
144             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ENTRY_MISSING, message, null));
145         }
146
147         int index = entry.indexOf('@');
148         String JavaDoc type = entry.substring(0, index);
149         String JavaDoc element = entry.substring(index + 1);
150         if (type.equals("plugin") || type.equals("fragment")) { //$NON-NLS-1$ //$NON-NLS-2$
151
String JavaDoc[] cvsFields = Utils.getArrayFromString(cvsInfo);
152             String JavaDoc tag = cvsFields[0];
153             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
154             sb.append(entry);
155             sb.append("="); //$NON-NLS-1$
156
sb.append(tag);
157             entries.add(sb.toString());
158         } else if (children && type.equals("feature")) { //$NON-NLS-1$
159
Feature feature = readFeature(element);
160             collectChildrenEntries(entries, feature);
161         }
162     }
163
164     /**
165      *
166      * @param entries
167      * @param feature
168      * @throws CoreException
169      */

170     protected void collectChildrenEntries(List entries, Feature feature) throws CoreException {
171         IPluginEntry[] pluginEntries = feature.getPluginEntries();
172         for (int i = 0; i < pluginEntries.length; i++) {
173             String JavaDoc elementId = pluginEntries[i].getVersionedIdentifier().getIdentifier();
174             if (pluginEntries[i].isFragment())
175                 collectEntries(entries, "fragment@" + elementId); //$NON-NLS-1$
176
else
177                 collectEntries(entries, "plugin@" + elementId); //$NON-NLS-1$
178
}
179     }
180
181     /**
182      *
183      * @param element
184      * @return Feature
185      * @throws CoreException
186      */

187     protected Feature readFeature(String JavaDoc element) throws CoreException {
188         IPath root = new Path(installLocation);
189         root = root.append(DEFAULT_FEATURE_LOCATION);
190         root = root.append(element);
191         try {
192             FeatureExecutableFactory factory = new FeatureExecutableFactory();
193             return (Feature) factory.createFeature(root.toFile().toURL(), null, null);
194         } catch (Exception JavaDoc e) {
195             String JavaDoc message = NLS.bind(TaskMessages.error_creatingFeature, element);
196             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, e));
197         }
198     }
199
200     /**
201      * Sets the installLocation.
202      */

203     public void setInstall(String JavaDoc installLocation) {
204         this.installLocation = installLocation;
205     }
206
207     /**
208      * Reads directory file at the directoryLocation.
209      */

210     protected void readDirectory() throws CoreException {
211         try {
212             directory = new Properties();
213             File file = new File(directoryLocation);
214             InputStream is = new BufferedInputStream(new FileInputStream(file));
215             try {
216                 directory.load(is);
217             } finally {
218                 is.close();
219             }
220         } catch (IOException e) {
221             String JavaDoc message = NLS.bind(TaskMessages.error_readingDirectory, directoryLocation);
222             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READ_DIRECTORY, message, e));
223         }
224     }
225
226     /**
227      *
228      * @param directory
229      */

230     public void setDirectory(String JavaDoc directory) {
231         directoryLocation = directory;
232     }
233
234     /**
235      *
236      * @param value
237      */

238     public void setElements(String JavaDoc value) {
239         elements = Utils.getArrayFromString(value);
240     }
241
242     /**
243      * Sets the full location of the manifest file.
244      */

245     public void setDestination(String JavaDoc value) {
246         destination = value;
247     }
248
249     /**
250      * Whether children of this element should be taken into account.
251      */

252     public void setChildren(boolean children) {
253         this.children = children;
254     }
255
256     /**
257      *
258      * @param value
259      */

260     public void setBuildName(String JavaDoc value) {
261         buildName = value;
262     }
263
264     /**
265      * Sets the buildId.
266      */

267     public void setBuildId(String JavaDoc buildId) {
268         this.buildId = buildId;
269     }
270
271     /**
272      * Sets the buildQualifier.
273      */

274     public void setBuildQualifier(String JavaDoc buildQualifier) {
275         this.buildQualifier = buildQualifier;
276     }
277
278     /**
279      * Sets the buildType.
280      */

281     public void setBuildType(String JavaDoc buildType) {
282         this.buildType = buildType;
283     }
284
285 }
286
Popular Tags