KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.ant;
7
8 import java.io.File JavaDoc;
9 import java.util.Date JavaDoc;
10
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.taskdefs.CallTarget;
13 import org.apache.tools.ant.taskdefs.Echo;
14 import org.apache.tools.ant.taskdefs.Input;
15 import org.apache.tools.ant.taskdefs.Property;
16
17 import fr.jayasoft.ivy.DefaultPublishingDRResolver;
18 import fr.jayasoft.ivy.Ivy;
19 import fr.jayasoft.ivy.IvyContext;
20 import fr.jayasoft.ivy.ModuleDescriptor;
21 import fr.jayasoft.ivy.ModuleRevisionId;
22 import fr.jayasoft.ivy.PublishingDependencyRevisionResolver;
23 import fr.jayasoft.ivy.status.StatusManager;
24 import fr.jayasoft.ivy.util.Message;
25 import fr.jayasoft.ivy.util.MessageImpl;
26
27 /**
28  * Trigger the delivery of a module, which may consist in a recursive delivery of dependencies
29  * and on the replacement in the ivy file of dynamic revisions (like latest.integration) by static ones.
30  *
31  * @author Xavier Hanin
32  *
33  */

34 public class IvyDeliver extends IvyTask {
35     private final class DeliverDRResolver extends DefaultPublishingDRResolver {
36         public String JavaDoc resolve(ModuleDescriptor published,
37                 String JavaDoc publishedStatus, ModuleRevisionId depMrid,
38                 String JavaDoc depStatus) {
39             if (StatusManager.getCurrent().isIntegration(publishedStatus)) {
40                 // published status is integration one, nothing to ask
41
return super.resolve(published, publishedStatus, depMrid,
42                         depStatus);
43             }
44
45             // we are publishing a delivery (a non integration module)
46

47             if (!StatusManager.getCurrent().isIntegration(depStatus)) {
48                 // dependency is already a delivery, nothing to ask
49
return super.resolve(published, publishedStatus, depMrid,
50                         depStatus);
51             }
52             
53             // the dependency is not a delivery
54

55             String JavaDoc statusProperty = depMrid.getName() + "." + depMrid.getRevision() + ".status";
56             String JavaDoc versionProperty = depMrid.getName() + "." + depMrid.getRevision() + ".version";
57             String JavaDoc deliveredProperty = depMrid.getName() + "." + depMrid.getRevision() + ".delivered";
58             
59             String JavaDoc version = getProject().getProperty(versionProperty);
60             String JavaDoc status = getProject().getProperty(statusProperty);
61             String JavaDoc delivered = getProject().getProperty(deliveredProperty);
62             Message.debug("found version = " + version + " status=" + status+" delivered="+delivered);
63             if (version != null && status != null) {
64                 if ("true".equals(delivered)) {
65                     // delivery has already been done : just return the value
66
return version;
67                 } else {
68                     deliverDependency(depMrid, version, status, depStatus);
69                     loadDeliveryList();
70                     return version;
71                 }
72             }
73
74             /**
75              * By setting these properties: recursive.delivery.status and
76              * recursive.delivery.version, then if the specific status/version
77              * is not found, then we will use the status/version set in these
78              * global properties. This will apply to all artifacts in the
79              * system.
80              *
81              * This patch is meant to be used for recursive deliveries so that
82              * all deliveries will use the global status/version unless a more
83              * specific one is set.
84              */

85             String JavaDoc globalStatusProperty = "recursive.delivery.status";
86             String JavaDoc globalVersionProperty = "recursive.delivery.version";
87             version = getProject().getProperty(globalVersionProperty);
88             status = getProject().getProperty(globalStatusProperty);
89             if (version != null && status != null) {
90                 // found global delivery properties
91
delivered = getProject().getProperty("recursive."+depMrid.getName()+ ".delivered");
92                 Message.debug("found global version = " + version + " and global status=" + status+" - delivered = "+delivered);
93                 if ("true".equals(delivered)) {
94                     // delivery has already been done : just return the value
95
return version;
96                 } else {
97                     getProject().setProperty(statusProperty, status);
98                     deliverDependency(depMrid, version, status, depStatus);
99                     loadDeliveryList();
100                     return version;
101                 }
102             }
103
104             // we must ask the user what version and status he want to have
105
// for the dependency
106
Input input = (Input) getProject().createTask("input");
107             input.setOwningTarget(getOwningTarget());
108             input.init();
109
110             // ask status
111
input.setMessage(depMrid.getName() + " " + depMrid.getRevision()
112                     + ": please enter a status: ");
113             input.setValidargs(StatusManager.getCurrent().getDeliveryStatusListString());
114             input.setAddproperty(statusProperty);
115             input.perform();
116             status = getProject().getProperty(statusProperty);
117             appendDeliveryList(statusProperty + " = " + status);
118
119             // ask version
120
input.setMessage(depMrid.getName() + " " + depMrid.getRevision()
121                     + ": please enter a version: ");
122             input.setValidargs(null);
123             input.setAddproperty(versionProperty);
124             input.perform();
125
126             version = getProject().getProperty(versionProperty);
127             appendDeliveryList(versionProperty + " = " + version);
128             deliverDependency(depMrid, version, status, depStatus);
129
130             loadDeliveryList();
131
132             return version;
133         }
134
135         public void deliverDependency(ModuleRevisionId depMrid, String JavaDoc version, String JavaDoc status, String JavaDoc depStatus) {
136             // call deliver target if any
137
if (_deliverTarget != null && _deliverTarget.trim().length() > 0) {
138
139                 CallTarget ct = (CallTarget) getProject().createTask("antcall");
140                 ct.setOwningTarget(getOwningTarget());
141                 ct.init();
142                 ct.setTarget(_deliverTarget);
143                 ct.setInheritAll(true);
144                 ct.setInheritRefs(true);
145                 Property param = ct.createParam();
146                 param.setName("dependency.name");
147                 param.setValue(depMrid.getName());
148                 param = ct.createParam();
149                 param.setName("dependency.published.status");
150                 param.setValue(status);
151                 param = ct.createParam();
152                 param.setName("dependency.published.version");
153                 param.setValue(version);
154                 param = ct.createParam();
155                 param.setName("dependency.version");
156                 param.setValue(depMrid.getRevision());
157                 param = ct.createParam();
158                 param.setName("dependency.status");
159                 param.setValue(depStatus==null?"null":depStatus);
160
161                 MessageImpl impl = IvyContext.getContext().getMessageImpl();
162                 try {
163                     IvyContext.getContext().setMessageImpl(null);
164                     ct.perform();
165                 } finally {
166                     IvyContext.getContext().setMessageImpl(impl);
167                 }
168                 
169                 String JavaDoc deliveredProperty = depMrid.getName() + "." + depMrid.getRevision() + ".delivered";
170                 getProject().setProperty(deliveredProperty, "true");
171                 appendDeliveryList(deliveredProperty + " = true");
172
173                 getProject().setProperty("recursive."+depMrid.getName() + ".delivered", "true");
174                 appendDeliveryList("recursive."+depMrid.getName() + ".delivered" + " = true");
175             }
176         }
177
178     }
179
180     private String JavaDoc _organisation;
181
182     private String JavaDoc _module;
183
184     private String JavaDoc _revision;
185
186     private String JavaDoc _pubRevision;
187
188     private File JavaDoc _cache;
189
190     private String JavaDoc _deliverpattern;
191
192     private String JavaDoc _status;
193
194     private String JavaDoc _pubdate;
195
196     private String JavaDoc _deliverTarget;
197
198     private File JavaDoc _deliveryList;
199
200     private boolean _replacedynamicrev = true;
201
202     public File JavaDoc getCache() {
203         return _cache;
204     }
205
206     public void setCache(File JavaDoc cache) {
207         _cache = cache;
208     }
209
210     public String JavaDoc getDeliverpattern() {
211         return _deliverpattern;
212     }
213
214     public void setDeliverpattern(String JavaDoc destivypattern) {
215         _deliverpattern = destivypattern;
216     }
217
218     public String JavaDoc getModule() {
219         return _module;
220     }
221
222     public void setModule(String JavaDoc module) {
223         _module = module;
224     }
225
226     public String JavaDoc getOrganisation() {
227         return _organisation;
228     }
229
230     public void setOrganisation(String JavaDoc organisation) {
231         _organisation = organisation;
232     }
233
234     public String JavaDoc getPubdate() {
235         return _pubdate;
236     }
237
238     public void setPubdate(String JavaDoc pubdate) {
239         _pubdate = pubdate;
240     }
241
242     public String JavaDoc getPubrevision() {
243         return _pubRevision;
244     }
245
246     public void setPubrevision(String JavaDoc pubRevision) {
247         _pubRevision = pubRevision;
248     }
249
250     public String JavaDoc getRevision() {
251         return _revision;
252     }
253
254     public void setRevision(String JavaDoc revision) {
255         _revision = revision;
256     }
257
258     public String JavaDoc getStatus() {
259         return _status;
260     }
261
262     public void setStatus(String JavaDoc status) {
263         _status = status;
264     }
265
266     public void setDelivertarget(String JavaDoc deliverTarget) {
267         _deliverTarget = deliverTarget;
268     }
269
270     public void setDeliveryList(File JavaDoc deliveryList) {
271         _deliveryList = deliveryList;
272     }
273
274     public boolean isReplacedynamicrev() {
275         return _replacedynamicrev;
276     }
277
278     public void setReplacedynamicrev(boolean replacedynamicrev) {
279         _replacedynamicrev = replacedynamicrev;
280     }
281
282     public void execute() throws BuildException {
283         Ivy ivy = getIvyInstance();
284         _organisation = getProperty(_organisation, ivy, "ivy.organisation");
285         _module = getProperty(_module, ivy, "ivy.module");
286         _revision = getProperty(_revision, ivy, "ivy.revision");
287         _pubRevision = getProperty(_pubRevision, ivy, "ivy.deliver.revision");
288         if (_cache == null) {
289             _cache = ivy.getDefaultCache();
290         }
291         _deliverpattern = getProperty(_deliverpattern, ivy,
292                 "ivy.deliver.ivy.pattern");
293         _status = getProperty(_status, ivy, "ivy.status");
294         if (_deliveryList == null) {
295             String JavaDoc deliveryListPath = getProperty(ivy, "ivy.delivery.list.file");
296             if (deliveryListPath == null) {
297                 _deliveryList = new File JavaDoc(System.getProperty("java.io.tmpdir")
298                         + "/delivery.properties");
299             } else {
300                 _deliveryList = getProject().resolveFile(
301                         ivy.substitute(deliveryListPath));
302             }
303         }
304         if (_organisation == null) {
305             throw new BuildException(
306                     "no organisation provided for ivy deliver task: It can either be set explicitely via the attribute 'organisation' or via 'ivy.organisation' property or a prior call to <resolve/>");
307         }
308         if (_module == null) {
309             throw new BuildException(
310                     "no module name provided for ivy deliver task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>");
311         }
312         if (_revision == null) {
313             _revision = "working@"+Ivy.getLocalHostName();
314         }
315         Date JavaDoc pubdate = getPubDate(_pubdate, new Date JavaDoc());
316         if (_pubRevision == null) {
317             if (_revision.startsWith("working@")) {
318                 _pubRevision = Ivy.DATE_FORMAT.format(pubdate);
319             } else {
320                 _pubRevision = _revision;
321             }
322         }
323         if (_deliverpattern == null) {
324             throw new BuildException(
325                     "deliver ivy pattern is missing: either provide it as parameters or through ivy.deliver.ivy.pattern properties");
326         }
327         if (_status == null) {
328             throw new BuildException(
329                     "no status provided: either provide it as parameter or through the ivy.status.default property");
330         }
331         ModuleRevisionId mrid = ModuleRevisionId.newInstance(
332                 _organisation, _module, _revision);
333         boolean isLeading = false;
334         try {
335             if (!_deliveryList.exists()) {
336                 isLeading = true;
337             }
338
339             loadDeliveryList();
340
341             PublishingDependencyRevisionResolver drResolver;
342             if (_deliverTarget != null && _deliverTarget.trim().length() > 0) {
343                 drResolver = new DeliverDRResolver();
344             } else {
345                 drResolver = new DefaultPublishingDRResolver();
346             }
347             ivy.deliver(mrid, _pubRevision, _cache, _deliverpattern, _status,
348                     pubdate, drResolver, doValidate(ivy), _replacedynamicrev);
349
350         } catch (Exception JavaDoc e) {
351             throw new BuildException("impossible to deliver " + mrid + ": " + e, e);
352         } finally {
353             if (isLeading) {
354                 if (_deliveryList.exists()) {
355                     _deliveryList.delete();
356                 }
357             }
358         }
359     }
360
361     private void loadDeliveryList() {
362         Property property = (Property) getProject().createTask("property");
363         property.setOwningTarget(getOwningTarget());
364         property.init();
365         property.setFile(_deliveryList);
366         property.perform();
367     }
368
369     private void appendDeliveryList(String JavaDoc msg) {
370         Echo echo = (Echo) getProject().createTask("echo");
371         echo.setOwningTarget(getOwningTarget());
372         echo.init();
373         echo.setFile(_deliveryList);
374         echo.setMessage(msg + "\n");
375         echo.setAppend(true);
376         echo.perform();
377     }
378
379 }
380
Popular Tags