KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > tasks > ModuleInfo


1 package org.antmod.tasks;
2
3 import java.io.File JavaDoc;
4
5 import org.antmod.conf.AntmodProperties;
6 import org.antmod.descriptor.DescriptorStoreFactory;
7 import org.antmod.descriptor.ReleaseDescriptor;
8 import org.antmod.scm.ScmSystem;
9 import org.antmod.scm.ScmSystemFactory;
10 import org.antmod.scm.ScmVersion;
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.Task;
13 import org.apache.tools.ant.types.EnumeratedAttribute;
14
15 /**
16  * Task for checking and displaying module information.
17  *
18  * @author Klaas Waslander
19  * @version Sep 15, 2003 6:57:57 PM
20  */

21 public final class ModuleInfo extends Task {
22     private String JavaDoc addProperty;
23     private String JavaDoc descriptorName;
24     private String JavaDoc moduleName;
25     private String JavaDoc moduleVersion;
26     private Info info;
27
28     /**
29      * Constructor.
30      */

31     public ModuleInfo() {
32     }
33
34     public void setAddProperty(String JavaDoc addProperty) {
35         this.addProperty = addProperty;
36     }
37     
38     public void setDescriptor(String JavaDoc descriptorName) {
39         this.descriptorName = descriptorName;
40     }
41
42     public void setModuleName(String JavaDoc moduleName) {
43         if (moduleName != null) {
44             moduleName = moduleName.trim();
45         }
46         this.moduleName = moduleName;
47     }
48
49     public void setModuleVersion(String JavaDoc moduleVersion) {
50         if (moduleVersion != null) {
51             moduleVersion = moduleVersion.trim();
52         }
53         this.moduleVersion = moduleVersion;
54     }
55
56     public void setInfo(Info info) {
57         this.info = info;
58     }
59
60     public void execute() throws BuildException {
61         if (this.addProperty == null) {
62             throw new BuildException("FAIL: 'addProperty' attribute of moduleinfo task has to be set.");
63         }
64         if (this.moduleName == null) {
65             throw new BuildException("FAIL: 'modulename' attribute of moduleinfo task has to be set.");
66         }
67         if (this.descriptorName == null) {
68             throw new BuildException("FAIL: 'descriptor' attribute of moduleinfo task has to be set.");
69         }
70         if (this.info == null) {
71             throw new BuildException("FAIL: 'info' attribute of moduleinfo task has to be set.");
72         }
73
74         // get release descriptor, module and scm system
75
ReleaseDescriptor descriptor = DescriptorStoreFactory.getConfiguredDescriptorStore().getReleaseDescriptor(this.descriptorName);
76         ReleaseDescriptor.Module module = descriptor.getModuleByName(this.moduleName);
77         ScmSystem scm = ScmSystemFactory.getScmSystemByName(module.getRepos());
78
79         // return the appropriate 'info'
80
String JavaDoc str = this.info.getValue();
81         if (str.equals(Info.LOCALVERSION)) {
82             getProject().setProperty(this.addProperty, scm.getLocalVersion(getModuleDir()).toString());
83
84         } else if (str.equals(Info.REVISIONAVAILABLE)) {
85             if (this.moduleVersion == null) {
86                 throw new BuildException("FAIL: 'moduleversion' attribute of moduleinfo task has to be set for 'revisionavailable' info.");
87             }
88
89             // get the parent branch of the desired version
90
ScmVersion ver = new ScmVersion(this.moduleName, this.moduleVersion);
91             boolean hasVersion = ver.isTrunk();
92             if (!hasVersion) {
93                 ScmVersion parentVer = ver.getParentBranch();
94
95                 // check whether revision is available within its parent branch
96
/*
97                 AbsCvsRunner.CvsRevision[] revs = new AbsCvsRunner().getCvsRevisions(new File(getModuleDir(), "module.xml"), parentVer.toCvsRevisionString());
98                 for (int i = 0; i < revs.length; i++) {
99                     AbsCvsRunner.CvsRevision rev = revs[i];
100                     if (rev.getVersion().equals(ver)) {
101                         hasVersion = true;
102                         break;
103                     }
104                 }
105                 */

106                 ScmVersion[] revs = scm.getVersionsInBranch(new File JavaDoc(getModuleDir(), "module.xml"), parentVer);
107                 for (int i = 0; i < revs.length; i++) {
108                     ScmVersion verInBranch = revs[i];
109                     if (verInBranch.equals(ver)) {
110                         hasVersion = true;
111                         break;
112                     }
113                 }
114             }
115
116             if (hasVersion) {
117                 getProject().setProperty(this.addProperty, "true");
118             }
119
120         } else if (str.equals(Info.LISTREVISIONS)) {
121             // determine current branch
122
ScmVersion localVersion = scm.getLocalVersion(getModuleDir());
123             //Version localVersion = new Version(this.moduleName, AbsCvsRunner.getLocalVersion(getModuleDir()).toString());
124
ScmVersion branchToList = localVersion;
125             if (branchToList.isTag()) {
126                 branchToList = branchToList.getParentBranch();
127             }
128
129             // retrieve revisions in current branch
130
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
131             ScmVersion[] revs = scm.getVersionsInBranch(new File JavaDoc(getModuleDir(), "module.xml"), branchToList);
132             //AbsCvsRunner.CvsRevision[] revs = new AbsCvsRunner().getCvsRevisions(new File(getModuleDir(), "module.xml"), branchToList.toCvsRevisionString());
133
for (int i = 0; i < revs.length; i++) {
134                 if (i > 0) {
135                     result.append(", ");
136                 }
137                 result.append(revs[i].toString());
138             }
139             getProject().setProperty(this.addProperty, result.toString());
140
141         } else if (str.equals(Info.SCMREPOSITORY)) {
142             if (module.getRepos() != null) {
143                 getProject().setProperty(this.addProperty, module.getRepos());
144             } else {
145                 getProject().setProperty(this.addProperty, "");
146             }
147
148         } else if (str.equals(Info.ISUPTODATE)) {
149             getProject().setProperty(this.addProperty, String.valueOf(scm.isUpToDate(getModuleDir())));
150
151         } else if (str.equals(Info.LATESTVERSION)) {
152             getProject().setProperty(this.addProperty, scm.getLatestVersion(getModuleDir()).toString());
153
154         }
155     }
156
157     /**
158      * Returns the directory of the currently set modulename.
159      */

160     private File JavaDoc getModuleDir() {
161         if (this.moduleName == null) {
162             throw new RuntimeException JavaDoc("Cannot return valid moduledir because 'moduleName' attribute is not set.");
163         }
164         if (new File JavaDoc(getProject().getBaseDir(), AntmodProperties.getProperty("antmod.release.metadata.file")).exists()) {
165             return new File JavaDoc(getProject().getBaseDir(), this.moduleName);
166         } else {
167             return new File JavaDoc(getProject().getBaseDir().getParentFile(), this.moduleName);
168         }
169     }
170
171     /**
172      * Lists all the kinds of information that the {@link ModuleInfo}
173      * Ant task can handle.
174      *
175      * @author Klaas Waslander
176      */

177     public static class Info extends EnumeratedAttribute {
178
179         public static String JavaDoc LOCALVERSION = "localversion";
180         public static String JavaDoc REVISIONAVAILABLE = "revisionavailable";
181         public static String JavaDoc LISTREVISIONS = "listrevisions";
182         public static String JavaDoc SCMREPOSITORY = "scmrepository";
183         public static String JavaDoc ISUPTODATE = "isuptodate";
184         public static String JavaDoc LATESTVERSION = "latestversion";
185
186         private static String JavaDoc[] values =
187                 new String JavaDoc[]{
188                     LOCALVERSION,
189                     REVISIONAVAILABLE,
190                     LISTREVISIONS,
191                     SCMREPOSITORY,
192                     ISUPTODATE,
193                     LATESTVERSION
194                 };
195
196         public String JavaDoc[] getValues() {
197             return Info.values;
198         }
199
200     }
201 }
202
Popular Tags