KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyBuildNumber


1 package fr.jayasoft.ivy.ant;
2
3 import org.apache.tools.ant.BuildException;
4
5 import fr.jayasoft.ivy.Ivy;
6 import fr.jayasoft.ivy.ModuleId;
7 import fr.jayasoft.ivy.ModuleRevisionId;
8 import fr.jayasoft.ivy.ResolvedModuleRevision;
9
10 /**
11  * Look for the latest module in the repository matching the given criteria,
12  * and sets a set of properties according to what was found.
13  *
14  * @author Xavier Hanin
15  */

16 public class IvyBuildNumber extends IvyTask {
17     private String JavaDoc _organisation;
18     private String JavaDoc _module;
19     private String JavaDoc _branch;
20     private String JavaDoc _revision;
21     
22     private String JavaDoc _revSep = ".";
23     private String JavaDoc _prefix = "ivy.";
24     private String JavaDoc _default = "0";
25     private String JavaDoc _defaultBuildNumber = "0";
26     
27     public String JavaDoc getModule() {
28         return _module;
29     }
30
31     public void setModule(String JavaDoc module) {
32         _module = module;
33     }
34
35     public String JavaDoc getOrganisation() {
36         return _organisation;
37     }
38
39     public void setOrganisation(String JavaDoc organisation) {
40         _organisation = organisation;
41     }
42
43     public String JavaDoc getRevision() {
44         return _revision;
45     }
46
47     public void setRevision(String JavaDoc revision) {
48         _revision = revision;
49     }
50
51
52     public String JavaDoc getBranch() {
53         return _branch;
54     }
55
56     public void setBranch(String JavaDoc branch) {
57         _branch = branch;
58     }
59
60     public String JavaDoc getDefault() {
61         return _default;
62     }
63
64     public void setDefault(String JavaDoc default1) {
65         _default = default1;
66     }
67
68     public String JavaDoc getPrefix() {
69         return _prefix;
70     }
71
72     public void setPrefix(String JavaDoc prefix) {
73         _prefix = prefix;
74     }
75
76     public void execute() throws BuildException {
77         if (_organisation == null) {
78             throw new BuildException("no organisation provided for ivy findmodules");
79         }
80         if (_module == null) {
81             throw new BuildException("no module name provided for ivy findmodules");
82         }
83         if (_prefix == null) {
84             throw new BuildException("null prefix not allowed");
85         }
86         
87         Ivy ivy = getIvyInstance();
88         if (_branch == null) {
89             ivy.getDefaultBranch(new ModuleId(_organisation, _module));
90         }
91         if (_revision == null || _revision.length() == 0) {
92             _revision = "latest.integration";
93         } else if (!_revision.endsWith("+")) {
94             _revision = _revision+"+";
95         }
96         if (!_prefix.endsWith(".") && _prefix.length() > 0) {
97             _prefix = _prefix+".";
98         }
99         ResolvedModuleRevision rmr = ivy.findModule(ModuleRevisionId.newInstance(_organisation, _module, _branch, _revision));
100         String JavaDoc revision = rmr == null ? null : rmr.getId().getRevision();
101         NewRevision newRevision = computeNewRevision(revision);
102         setProperty("revision", newRevision.revision);
103         setProperty("new.revision", newRevision.newRevision);
104         setProperty("build.number", newRevision.buildNumber);
105         setProperty("new.build.number", newRevision.newBuildNumber);
106     }
107
108     private void setProperty(String JavaDoc propertyName, String JavaDoc value) {
109         if (value != null) {
110             getProject().setProperty(_prefix+propertyName, value);
111         }
112     }
113
114     private NewRevision computeNewRevision(String JavaDoc revision) {
115         String JavaDoc revPrefix = "latest.integration".equals(_revision)?"":_revision.substring(0, _revision.length() - 1);
116         if (revision != null && !revision.startsWith(revPrefix)) {
117             throw new BuildException("invalid exception found in repository: '"+revision+"' for '"+revPrefix+"'");
118         }
119         if (revision == null) {
120             if (revPrefix.length() > 0) {
121                 return new NewRevision(revision, revPrefix+(revPrefix.endsWith(_revSep)?_defaultBuildNumber:_revSep+_defaultBuildNumber), null, _defaultBuildNumber);
122             } else {
123                 Range r = findLastNumber(_default);
124                 if (r == null) { // no number found
125
return new NewRevision(revision, _default, null, null);
126                 } else {
127                     long n = Long.parseLong(_default.substring(r.startIndex, r.endIndex));
128                     return new NewRevision(revision, _default, null, String.valueOf(n));
129                 }
130             }
131         }
132         Range r;
133         if (revPrefix.length() == 0) {
134             r = findLastNumber(revision);
135             if (r == null) {
136                  return new NewRevision(revision, revision+(revision.endsWith(_revSep)?"1":_revSep+"1"), null, "1");
137             }
138         } else {
139             r = findFirstNumber(revision, revPrefix.length());
140             if (r == null) {
141                  return new NewRevision(revision, revPrefix+(revPrefix.endsWith(_revSep)?"1":_revSep+"1"), null, "1");
142             }
143         }
144         long n = Long.parseLong(revision.substring(r.startIndex, r.endIndex)) + 1;
145         return new NewRevision(
146                 revision,
147                 revision.substring(0, r.startIndex)+n,
148                 String.valueOf(n-1),
149                 String.valueOf(n));
150     }
151     
152     private Range findFirstNumber(String JavaDoc str, int startIndex) {
153         // let's find the first digit in the string
154
int startNumberIndex = startIndex;
155         while (startNumberIndex<str.length() && !Character.isDigit(str.charAt(startNumberIndex))) {
156             startNumberIndex++;
157         }
158         if (startNumberIndex == str.length()) {
159             return null;
160         }
161         // let's find the end of the number
162
int endNumberIndex = startNumberIndex+1;
163         while (endNumberIndex<str.length() && Character.isDigit(str.charAt(endNumberIndex))) {
164             endNumberIndex++;
165         }
166         return new Range(startNumberIndex, endNumberIndex);
167     }
168     
169     private Range findLastNumber(String JavaDoc str) {
170         int endNumberIndex = str.length() - 1;
171         while (endNumberIndex >= 0 && !Character.isDigit(str.charAt(endNumberIndex))) {
172             endNumberIndex--;
173         }
174         int startNumberIndex = endNumberIndex == -1?-1:endNumberIndex - 1;
175         while (startNumberIndex >= 0 && Character.isDigit(str.charAt(startNumberIndex))) {
176             startNumberIndex--;
177         }
178         endNumberIndex ++;
179         startNumberIndex ++;
180         if (startNumberIndex == endNumberIndex) { // no number found
181
return null;
182         } else {
183             return new Range(startNumberIndex, endNumberIndex);
184         }
185     }
186
187     private static class Range {
188         private int startIndex;
189         private int endIndex;
190         public Range(int startIndex, int endIndex) {
191             this.startIndex = startIndex;
192             this.endIndex = endIndex;
193         }
194     }
195
196     private static class NewRevision {
197         private String JavaDoc revision;
198         private String JavaDoc newRevision;
199         private String JavaDoc buildNumber;
200         private String JavaDoc newBuildNumber;
201         public NewRevision(String JavaDoc revision, String JavaDoc newRevision, String JavaDoc buildNumber, String JavaDoc newBuildNumber) {
202             this.revision = revision;
203             this.newRevision = newRevision;
204             this.buildNumber = buildNumber;
205             this.newBuildNumber = newBuildNumber;
206         }
207     }
208
209     public String JavaDoc getRevSep() {
210         return _revSep;
211     }
212
213     public void setRevSep(String JavaDoc revSep) {
214         _revSep = revSep;
215     }
216
217     public String JavaDoc getDefaultBuildNumber() {
218         return _defaultBuildNumber;
219     }
220
221     public void setDefaultBuildNumber(String JavaDoc defaultBuildNumber) {
222         _defaultBuildNumber = defaultBuildNumber;
223     }
224 }
225
Popular Tags