KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > descriptor > ReleaseDescriptor


1 package org.antmod.descriptor;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.text.ParseException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import org.antmod.conf.AntmodProperties;
15 import org.antmod.scm.ScmSystem;
16 import org.antmod.scm.ScmSystemFactory;
17 import org.antmod.scm.ScmUrl;
18 import org.apache.commons.io.FileUtils;
19 import org.apache.tools.ant.Project;
20
21
22 /**
23  * Represents a release descriptor, with modules and versions in it.
24  * This is the central concept of Antmod, and enables smooth release management.
25  *
26  * @author Klaas Waslander
27  */

28 public class ReleaseDescriptor {
29     private String JavaDoc name;
30     private String JavaDoc versionString;
31     private ArrayList JavaDoc modules = new ArrayList JavaDoc();
32     private ArrayList JavaDoc javalibs = new ArrayList JavaDoc();
33     private ArrayList JavaDoc includes = new ArrayList JavaDoc();
34     private Properties JavaDoc properties = new Properties JavaDoc();
35     
36     // TODO: move to XML-from-SCM provider implementation
37
private File JavaDoc xmlFile;
38     private String JavaDoc xmlFileRelativePath;
39
40     /**
41      * Construct a new release descriptor with the given name and version.
42      * @param name
43      * @param version
44      */

45     public ReleaseDescriptor(String JavaDoc name, String JavaDoc versionString) {
46         this.name = name;
47         this.versionString = versionString;
48     }
49     
50     /**
51      * Verify that everything is okay in this release descriptor.
52      * @throws IllegalStateException When the state has been verified to be illegal
53      */

54     public void verify() throws IllegalStateException JavaDoc {
55         // check for main module
56
List JavaDoc mainModules = getModulesByType(Module.TYPE_MAIN);
57         if (mainModules.size() > 1) {
58             throw new IllegalStateException JavaDoc("Multiple 'main' modules in release description " + this.name + "-" + this.versionString + " - make sure there is only one module with type 'main'.");
59         }
60         if (mainModules.isEmpty()) {
61             throw new IllegalStateException JavaDoc("No 'main' module in release description " + this.name + "-" + this.versionString + " - make sure there is one module with type 'main'.");
62         }
63
64         // ensure there is no module with same name as "javalib" module
65
try {
66             String JavaDoc javalibUrl = AntmodProperties.getProperty("antmod.javalib.url");
67             if (javalibUrl == null) {
68                 throw new IllegalStateException JavaDoc("\"antmod.javalib.url\" property is not set. Cannot continue.");
69             }
70             if (javalibUrl.startsWith("scm:")) {
71                 ScmUrl javalibScmUrl = new ScmUrl(javalibUrl);
72                 if (containsModule(javalibScmUrl.getModule())) {
73                     throw new IllegalStateException JavaDoc("'" + javalibScmUrl.getModule() + "' module found in list with modules - this is not allowed! Instead, specify library dependencies using '<javalib>' tag in release descriptor");
74                 }
75             }
76         } catch (ParseException JavaDoc pe) {
77             // throw runtime exception if javalib url could not be parsed
78
throw new RuntimeException JavaDoc("Javalib url in property 'antmod.javalib.repos.url' could not be parsed.", pe);
79         }
80
81         // ensure there is no module with same name as "build" directory
82
String JavaDoc releaseBuildDir = AntmodProperties.getProperty("antmod.release.dirs.build");
83         if (containsModule(releaseBuildDir)) {
84             throw new IllegalStateException JavaDoc("'" + releaseBuildDir + "' module found in list with modules - this is not allowed! Make sure the module is named differently, such that it does not conflict with this release-level build directory.");
85         }
86     }
87
88     /**
89      * Register the various attributes of this release as Ant properties
90      * into the given Ant project, allowing Ant buildfiles to use these
91      * properties.
92      * @param antProject The ant project to register this release's attributes into
93      */

94     public void registerReleaseAttributesInto(Project antProject) {
95         antProject.setProperty("antmod.release.mainmodule.name", this.getMainModule().getName());
96
97         File JavaDoc releaseDir = ((new File JavaDoc(antProject.getBaseDir(), AntmodProperties.getProperty("antmod.release.metadata.file")).exists()) ? antProject.getBaseDir() : antProject.getBaseDir().getParentFile());
98         antProject.setProperty("antmod.release.mainmodule.dir", new File JavaDoc(releaseDir, this.getMainModule().getName()).getPath());
99
100         antProject.setProperty("antmod.release.file.relativepath", this.xmlFileRelativePath);
101         antProject.setProperty("antmod.release.file.path", this.xmlFile.getPath());
102         antProject.setProperty("antmod.release.file.absolutepath", this.xmlFile.getAbsolutePath());
103     }
104
105     /**
106      * @param module The module to be added because it is part of this release.
107      */

108     public void addModule(Module module) {
109         if (module.getName() == null) {
110             throw new IllegalArgumentException JavaDoc("Module without name not allowed in release descriptor " + name + "-" + versionString);
111         }
112         modules.add(module);
113     }
114
115     /**
116      * @param include The other release to be included.
117      */

118     public void addInclude(Include include) {
119         includes.add(include);
120     }
121
122     /**
123      * Invoked by Ant to add libs to this release, if declared in the XML file.
124      * @param lib The library to be added because it is part of this release.
125      */

126     public void addJavaLib(JavaLib javalib) {
127         javalibs.add(javalib);
128     }
129
130     /**
131      * Get the antmod configuration properties that have been overridden
132      * as part of this release.
133      */

134     public Properties JavaDoc getProperties() {
135         return this.properties;
136     }
137
138     /**
139      * Invoked during parsing of release descriptor to set the properties
140      * that have been declared as part of the release and that override
141      * the defaults for those properties.
142      */

143     public void setPropertiesFromText(String JavaDoc propertiesText) {
144         InputStream JavaDoc in = null;
145         try {
146             in = new ByteArrayInputStream JavaDoc(propertiesText.getBytes());
147             Properties JavaDoc newProps = new Properties JavaDoc();
148             newProps.load(in);
149             this.properties = newProps;
150         } catch (IOException JavaDoc e) {
151             e.printStackTrace();
152         }
153         finally {
154             if (in != null) {
155                 try {
156                     in.close();
157                 }
158                 catch (IOException JavaDoc e1) {
159                     e1.printStackTrace();
160                 }
161             }
162         }
163     }
164
165     /**
166      * Check whether a module with the given name is part of this release.
167      */

168     public boolean containsModule(String JavaDoc moduleName) {
169         return getModuleByName(moduleName) != null;
170     }
171
172     /**
173      * Get the module with the given name, if any.
174      * @param moduleName The name of the module that you are searching for
175      * @return The module, or null if no matching module was found.
176      */

177     public Module getModuleByName(String JavaDoc moduleName) {
178         Iterator JavaDoc iter = this.modules.iterator();
179         Module currentMod;
180         while (iter.hasNext()) {
181             currentMod = (Module)iter.next();
182             if (currentMod.getName().equals(moduleName)) {
183                 return currentMod;
184             }
185         }
186         return null;
187     }
188
189     public List JavaDoc getModulesByType(String JavaDoc moduleType) {
190         ArrayList JavaDoc result = new ArrayList JavaDoc();
191         Iterator JavaDoc iter = modules.iterator();
192         while (iter.hasNext()) {
193             Module mod = (Module)iter.next();
194             if (mod.getType().equals(moduleType)) {
195                 result.add(mod);
196             }
197         }
198         return result;
199     }
200     
201     /**
202      * Returns the module that is used to build the release,
203      * this is usually the main module.
204      */

205     public Module getBuildReleaseModule() {
206         List JavaDoc buildrelease = getModulesByType(Module.TYPE_BUILDRELEASE);
207         if (buildrelease.isEmpty()) {
208             return getMainModule();
209         } else {
210             return (Module)buildrelease.get(0);
211         }
212     }
213
214     public Module getMainModule() {
215         return (Module)getModulesByType(Module.TYPE_MAIN).get(0);
216     }
217
218     public List JavaDoc getModules() {
219         return Collections.unmodifiableList(this.modules);
220     }
221     
222     public List JavaDoc getIncludes() {
223         return Collections.unmodifiableList(this.includes);
224     }
225     
226     public List JavaDoc getJavaLibs() {
227         return Collections.unmodifiableList(this.javalibs);
228     }
229
230     public String JavaDoc getName() {
231         return name;
232     }
233     
234     public String JavaDoc getVersionString() {
235         return versionString;
236     }
237
238     public String JavaDoc getScmRevision() {
239         // TODO: move this to XML-from-SCM provider implementation
240
try {
241             ScmSystem scm = ScmSystemFactory.getScmSystemByUrl(AntmodProperties.getProperty("antmod.descriptor.xml.repos.url"));
242             return scm.getRevisionNumber(this.xmlFile);
243         } catch (Exception JavaDoc e) {
244             e.printStackTrace();
245         }
246         return null;
247     }
248     
249     public void setXmlFile(File JavaDoc xmlFile, String JavaDoc xmlFileRelativePath) {
250         this.xmlFile = xmlFile;
251         this.xmlFileRelativePath = xmlFileRelativePath;
252     }
253
254
255
256     /**
257      * Lib represent a library name that a release depends on.
258      */

259     public static class JavaLib {
260         /**
261          * The name of the library.
262          */

263         private String JavaDoc name;
264
265         public void setName(String JavaDoc name) {
266             this.name = name;
267         }
268
269         public String JavaDoc getName() {
270             return name;
271         }
272     }
273
274     /**
275      * Include
276      *
277      * @author Herko ter Horst
278      */

279     public static class Include {
280         private String JavaDoc releaseName;
281
282         public void setReleaseName(String JavaDoc releaseName) {
283             this.releaseName = releaseName;
284         }
285
286         public String JavaDoc getReleaseName() {
287             return releaseName;
288         }
289     }
290
291     /**
292      * Module represent a name that a release depends on.
293      */

294     public static class Module {
295         public static String JavaDoc TYPE_MAIN = "main";
296         public static String JavaDoc TYPE_BUILDRELEASE = "buildrelease";
297         public static String JavaDoc TYPE_DIST = "dist";
298         public static String JavaDoc TYPE_LIBRARY = "library";
299
300         /**
301          * The name of the module.
302          */

303         private String JavaDoc name;
304         /**
305          * The version of the module.
306          */

307         private String JavaDoc version = "trunk";
308         /**
309          * The type of dependency of the module.
310          */

311         private String JavaDoc type = TYPE_LIBRARY;
312
313         /**
314          * The repository where this module is version controlled.
315          */

316         private String JavaDoc repos;
317
318         /**
319          * Construct a new Module.
320          */

321         public Module() {
322         }
323
324         /**
325          * Set the name of the module.
326          * @param name the name of the module
327          */

328         public void setName(String JavaDoc name) {
329             this.name = name;
330         }
331
332         /**
333          * @return the name of the module
334          */

335         public String JavaDoc getName() {
336             return this.name;
337         }
338
339         /**
340          * Set the version of the module.
341          * @param version the version of the module
342          */

343         public void setVersion(String JavaDoc version) {
344             this.version = version;
345         }
346
347         /**
348          * @return the version of the module
349          */

350         public String JavaDoc getVersion() {
351             return this.version;
352         }
353
354         /**
355          * Set the type of the module.
356          * @param type
357          */

358         public void setType(String JavaDoc type) {
359             this.type = type;
360         }
361
362         /**
363          * @return the type of the module.
364          */

365         public String JavaDoc getType() {
366             return this.type;
367         }
368         
369         public void setRepos(String JavaDoc repos) {
370             this.repos = repos;
371         }
372         
373         public String JavaDoc getRepos() {
374             return this.repos;
375         }
376
377         public boolean equals(Object JavaDoc other) {
378             boolean result = false;
379             if (other instanceof Module) {
380                 Module otherModule = (Module) other;
381                 if (name.equals(otherModule.name) &&
382                         version.equals(otherModule.version) &&
383                         type.equals(otherModule.type)
384                 ) {
385                     result = true;
386                 }
387             }
388             return result;
389         }
390     }
391 }
392
Popular Tags