KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DateFormat JavaDoc;
9 import java.text.SimpleDateFormat JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.apache.tools.ant.BuildException;
17 import org.apache.tools.ant.Project;
18 import org.apache.tools.ant.Task;
19
20 import fr.jayasoft.ivy.Ivy;
21 import fr.jayasoft.ivy.IvyContext;
22 import fr.jayasoft.ivy.ModuleDescriptor;
23 import fr.jayasoft.ivy.report.ResolveReport;
24 import fr.jayasoft.ivy.util.Message;
25 import fr.jayasoft.ivy.util.StringUtils;
26
27 /**
28  * Base class for all ivy ant tasks, deal particularly with ivy instance storage in ant project.
29  *
30  * @author Xavier Hanin
31  *
32  */

33 public class IvyTask extends Task {
34     public static final String JavaDoc ANT_PROJECT_CONTEXT_KEY = "ant-project";
35     private Boolean JavaDoc _validate = null;
36
37     protected boolean doValidate(Ivy ivy) {
38         if (_validate != null) {
39             return _validate.booleanValue();
40         }
41         return ivy.doValidate();
42     }
43     public boolean isValidate() {
44         return _validate == null ? true : _validate.booleanValue();
45     }
46     public void setValidate(boolean validate) {
47         _validate = Boolean.valueOf(validate);
48     }
49     
50     protected Ivy getIvyInstance() {
51         ensureMessageInitialised();
52         Object JavaDoc ref = getProject().getReference("ivy.instances");
53         if (ref != null && !(ref instanceof Map JavaDoc)) {
54             throw new BuildException("ivy problem with ant: ivy.instances reference is not a Map. Please do not sett ivy.instances reference in your ant project. current reference: "+ref+" class="+ref.getClass()+" classloader="+ref.getClass().getClassLoader());
55         }
56         Map JavaDoc instances = (Map JavaDoc) ref;
57         if (instances == null || !instances.containsKey(Ivy.class)) {
58             Message.verbose("no ivy instance found: auto configuring ivy");
59             IvyConfigure configure = new IvyConfigure();
60             configure.setProject(getProject());
61             configure.execute();
62             instances = (Map JavaDoc) getProject().getReference("ivy.instances");
63             if (instances == null || !instances.containsKey(Ivy.class)) {
64                 throw new BuildException("ivy internal problem: impossible to get ivy instance after configure... maybe a classloader problem");
65             }
66         }
67         return (Ivy)instances.get(Ivy.class);
68     }
69
70     /**
71      * Every task MUST call ensureMessageInitialised when the execution method
72      * starts (at least before doing any log in order to set the correct task
73      * in the log.
74      */

75     protected void ensureMessageInitialised() {
76         if (!Message.isInitialised()) {
77             Message.init(new AntMessageImpl(this));
78         }
79
80     }
81     protected void setIvyInstance(Ivy ivy) {
82         // this reference is not used anymore, what is used is the instances map below
83
getProject().addReference("ivy.instance", ivy);
84         
85         if (ivy != null) {
86             Message.debug("setting ivy.instance on "+getProject()+": "+ivy+" class="+ivy.getClass().getName()+" classloader="+ivy.getClass().getClassLoader());
87             // we keep a map of ivy instances per Ivy class, in case of multiple classloaders
88
Map JavaDoc instances = (Map JavaDoc) getProject().getReference("ivy.instances");
89             if (instances == null) {
90                 instances = new HashMap JavaDoc();
91                 getProject().addReference("ivy.instances", instances);
92             }
93             instances.put(ivy.getClass(), ivy);
94         }
95     }
96     
97     protected void setResolved(ResolveReport report, boolean keep) {
98         ModuleDescriptor md = report.getModuleDescriptor();
99         String JavaDoc[] confs = report.getConfigurations();
100         if (keep) {
101             getProject().addReference("ivy.resolved.report", report);
102             getProject().addReference("ivy.resolved.configurations.ref", confs);
103             getProject().addReference("ivy.resolved.descriptor", md);
104         }
105         String JavaDoc suffix = md.getModuleRevisionId().getModuleId().getOrganisation()+"."+md.getModuleRevisionId().getModuleId().getName();
106         getProject().addReference("ivy.resolved.report."+suffix, report);
107         getProject().addReference("ivy.resolved.descriptor."+suffix, md);
108         getProject().addReference("ivy.resolved.configurations.ref."+suffix, confs);
109     }
110     
111     protected void ensureResolved(boolean haltOnFailure, boolean useOrigin, String JavaDoc org, String JavaDoc module) {
112         ensureResolved(haltOnFailure, useOrigin, true, org, module, null);
113     }
114     protected void ensureResolved(boolean haltOnFailure, boolean useOrigin, boolean transitive, String JavaDoc org, String JavaDoc module, String JavaDoc conf) {
115         ensureMessageInitialised();
116 // if (org != null && module != null) {
117
// return;
118
// }
119
String JavaDoc[] confs = getConfsToResolve(org, module, conf, false);
120         
121         if (confs.length > 0) {
122             IvyResolve resolve = createResolve(haltOnFailure, useOrigin);
123             resolve.setTransitive(transitive);
124             resolve.setConf(StringUtils.join(confs, ", "));
125             resolve.execute();
126         }
127     }
128     
129     protected String JavaDoc[] getConfsToResolve(String JavaDoc org, String JavaDoc module, String JavaDoc conf, boolean strict) {
130         ModuleDescriptor reference = (ModuleDescriptor) getResolvedDescriptor(org, module, strict);
131         Message.debug("calculating configurations to resolve");
132         
133         if (reference == null) {
134             Message.debug("module not yet resolved, all confs still need to be resolved");
135             if (conf == null) {
136                 return new String JavaDoc[] {"*"};
137             } else {
138                 return splitConfs(conf);
139             }
140         } else if (conf != null) {
141             String JavaDoc[] rconfs = getResolvedConfigurations(org, module, strict);
142             String JavaDoc[] confs;
143             if ("*".equals(conf)) {
144                 confs = reference.getConfigurationsNames();
145             } else {
146                 confs = splitConfs(conf);
147             }
148             HashSet JavaDoc rconfsSet = new HashSet JavaDoc(Arrays.asList(rconfs));
149             HashSet JavaDoc confsSet = new HashSet JavaDoc(Arrays.asList(confs));
150             Message.debug("resolved configurations: "+rconfsSet);
151             Message.debug("asked configurations: "+confsSet);
152             confsSet.removeAll(rconfsSet);
153             Message.debug("to resolve configurations: "+confsSet);
154             return (String JavaDoc[]) confsSet.toArray(new String JavaDoc[confsSet.size()]);
155         } else {
156             Message.debug("module already resolved, no configuration to resolve");
157             return new String JavaDoc[0];
158         }
159         
160     }
161     
162     protected String JavaDoc[] getResolvedConfigurations(String JavaDoc org, String JavaDoc module, boolean strict) {
163         return (String JavaDoc[]) getReference("ivy.resolved.configurations.ref", org, module, strict);
164     }
165     
166     protected Object JavaDoc getResolvedDescriptor(String JavaDoc org, String JavaDoc module) {
167         return getResolvedDescriptor(org, module, false);
168     }
169     
170     protected Object JavaDoc getResolvedDescriptor(String JavaDoc org, String JavaDoc module, boolean strict) {
171         return getReference("ivy.resolved.descriptor", org, module, strict);
172     }
173     private Object JavaDoc getReference(String JavaDoc prefix, String JavaDoc org, String JavaDoc module, boolean strict) {
174         Object JavaDoc reference = null;
175         if (org != null && module != null) {
176             reference = getProject().getReference(prefix+"."+org+"."+module);
177         }
178         if (!strict && reference == null) {
179             reference = getProject().getReference(prefix);
180         }
181         return reference;
182     }
183     
184     protected ResolveReport getResolvedReport(String JavaDoc org, String JavaDoc module) {
185         return getResolvedReport(org, module, false);
186     }
187     protected ResolveReport getResolvedReport(String JavaDoc org, String JavaDoc module, boolean strict) {
188         return (ResolveReport) getReference("ivy.resolved.report", org, module, strict);
189     }
190     
191     protected IvyResolve createResolve(boolean haltOnFailure, boolean useOrigin) {
192         Message.verbose("no resolved descriptor found: launching default resolve");
193         IvyResolve resolve = new IvyResolve();
194         resolve.setProject(getProject());
195         resolve.setHaltonfailure(haltOnFailure);
196         resolve.setUseOrigin(useOrigin);
197         if (_validate != null) {
198             resolve.setValidate(_validate.booleanValue());
199         }
200         return resolve;
201     }
202
203     protected boolean shouldResolve(String JavaDoc org, String JavaDoc module) {
204         ensureMessageInitialised();
205         if (org != null && module != null) {
206             return false;
207         }
208         Object JavaDoc reference = getResolvedDescriptor(org, module);
209         return (reference == null);
210     }
211
212     protected String JavaDoc[] splitConfs(String JavaDoc conf) {
213         String JavaDoc[] confs = conf.split(",");
214         for (int i = 0; i < confs.length; i++) {
215             confs[i] = confs[i].trim();
216         }
217         return confs;
218     }
219
220     protected String JavaDoc mergeConfs(String JavaDoc[] conf) {
221         return StringUtils.join(conf, ", ");
222     }
223
224     private static final DateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
225
226     protected Date JavaDoc getPubDate(String JavaDoc date, Date JavaDoc def) {
227         if (date != null) {
228             if ("now".equalsIgnoreCase(date)) {
229                 return new Date JavaDoc();
230             }
231             try {
232                 return DATE_FORMAT.parse(date);
233             } catch (Exception JavaDoc ex) {
234                 throw new BuildException("publication date provided in bad format. should be yyyyMMddHHmmss and not "+date);
235             }
236         } else {
237             return def;
238         }
239     }
240
241     protected String JavaDoc getProperty(String JavaDoc value, Ivy ivy, String JavaDoc name) {
242         if (value == null) {
243             return getProperty(ivy, name);
244         } else {
245             value = ivy.substitute(value);
246             Message.debug("parameter found as attribute value: "+name+"="+value);
247             return value;
248         }
249     }
250     
251     protected String JavaDoc getProperty(Ivy ivy, String JavaDoc name) {
252         String JavaDoc val = ivy.getVariable(name);
253         if (val == null) {
254             val = ivy.substitute(getProject().getProperty(name));
255             if (val != null) {
256                 Message.debug("parameter found as ant project property: "+name+"="+val);
257             } else {
258                 Message.debug("parameter not found: "+name);
259             }
260         } else {
261             val = ivy.substitute(val);
262             Message.debug("parameter found as ivy variable: "+name+"="+val);
263         }
264         return val;
265     }
266     
267     public void setProject(Project project) {
268         super.setProject(project);
269         IvyContext.getContext().set(ANT_PROJECT_CONTEXT_KEY, project);
270     }
271     
272 }
273
Popular Tags