KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package fr.jayasoft.ivy.ant;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import javax.xml.transform.OutputKeys JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
21 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
22 import javax.xml.transform.sax.TransformerHandler JavaDoc;
23 import javax.xml.transform.stream.StreamResult JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.helpers.AttributesImpl JavaDoc;
29
30 import fr.jayasoft.ivy.Artifact;
31 import fr.jayasoft.ivy.ArtifactOrigin;
32 import fr.jayasoft.ivy.Ivy;
33 import fr.jayasoft.ivy.IvyNode;
34 import fr.jayasoft.ivy.ModuleDescriptor;
35 import fr.jayasoft.ivy.ModuleId;
36
37 /**
38  * Generates a report of all artifacts involved during the last resolve.
39  */

40 public class IvyArtifactReport extends IvyTask {
41     private File JavaDoc _tofile;
42     private String JavaDoc _conf;
43     private String JavaDoc _pattern;
44     private boolean _haltOnFailure = true;
45     private File JavaDoc _cache;
46
47     public File JavaDoc getTofile() {
48         return _tofile;
49     }
50     public void setTofile(File JavaDoc tofile) {
51         _tofile = tofile;
52     }
53     public String JavaDoc getConf() {
54         return _conf;
55     }
56     public void setConf(String JavaDoc conf) {
57         _conf = conf;
58     }
59     public String JavaDoc getPattern() {
60         return _pattern;
61     }
62     public void setPattern(String JavaDoc pattern) {
63         _pattern = pattern;
64     }
65     public boolean isHaltonfailure() {
66         return _haltOnFailure;
67     }
68     public void setHaltonfailure(boolean haltOnFailure) {
69         _haltOnFailure = haltOnFailure;
70     }
71     public File JavaDoc getCache() {
72         return _cache;
73     }
74     public void setCache(File JavaDoc cache) {
75         _cache = cache;
76     }
77
78     public void execute() throws BuildException {
79         if (_tofile == null) {
80             throw new BuildException("no destination file name: please provide it through parameter 'tofile'");
81         }
82
83         Ivy ivy = getIvyInstance();
84
85         ensureResolved(isHaltonfailure(), false, null, null);
86
87         String JavaDoc _organisation = getProperty(null, ivy, "ivy.organisation");
88         String JavaDoc _module = getProperty(null, ivy, "ivy.module");
89
90         if (_cache == null) {
91             _cache = ivy.getDefaultCache();
92         }
93         _pattern = getProperty(_pattern, ivy, "ivy.retrieve.pattern");
94         _conf = getProperty(_conf, ivy, "ivy.resolved.configurations");
95         if ("*".equals(_conf)) {
96             _conf = getProperty(ivy, "ivy.resolved.configurations");
97             if (_conf == null) {
98                 throw new BuildException("bad provided for ivy artifact report task: * can only be used with a prior call to <resolve/>");
99             }
100         }
101
102         if (_organisation == null) {
103             throw new BuildException("no organisation provided for ivy artifact report task: It can be set via a prior call to <resolve/>");
104         }
105         if (_module == null) {
106             throw new BuildException("no module name provided for ivy artifact report task: It can be set via a prior call to <resolve/>");
107         }
108         if (_conf == null) {
109             throw new BuildException("no conf provided for ivy artifact report task: It can either be set explicitely via the attribute 'conf' or via 'ivy.resolved.configurations' property or a prior call to <resolve/>");
110         }
111         try {
112             String JavaDoc[] confs = splitConfs(_conf);
113             IvyNode[] dependencies = ivy.getDependencies((ModuleDescriptor) getProject().getReference("ivy.resolved.descriptor"), confs, _cache, new Date JavaDoc(), null, doValidate(ivy));
114
115             Map JavaDoc artifactsToCopy = ivy.determineArtifactsToCopy(new ModuleId(_organisation, _module), confs, _cache, _pattern, null);
116             Map JavaDoc moduleRevToArtifactsMap = new HashMap JavaDoc();
117             for (Iterator JavaDoc iter = artifactsToCopy.keySet().iterator(); iter.hasNext();) {
118                 Artifact artifact = (Artifact) iter.next();
119                 Set JavaDoc moduleRevArtifacts = (Set JavaDoc) moduleRevToArtifactsMap.get(artifact.getModuleRevisionId());
120                 if (moduleRevArtifacts == null) {
121                     moduleRevArtifacts = new HashSet JavaDoc();
122                     moduleRevToArtifactsMap.put(artifact.getModuleRevisionId(), moduleRevArtifacts);
123                 }
124                 moduleRevArtifacts.add(artifact);
125             }
126
127             generateXml(ivy, dependencies, moduleRevToArtifactsMap, artifactsToCopy);
128         } catch (ParseException JavaDoc e) {
129             log(e.getMessage(), Project.MSG_ERR);
130             throw new BuildException("syntax errors in ivy file: "+e, e);
131         } catch (IOException JavaDoc e) {
132             throw new BuildException("impossible to generate report: "+e, e);
133         }
134     }
135
136     private void generateXml(Ivy ivy, IvyNode[] dependencies, Map JavaDoc moduleRevToArtifactsMap, Map JavaDoc artifactsToCopy) {
137         try {
138             FileOutputStream JavaDoc fileOuputStream = new FileOutputStream JavaDoc(_tofile);
139             try {
140                 TransformerHandler JavaDoc saxHandler = createTransformerHandler(fileOuputStream);
141                 
142                 saxHandler.startDocument();
143                 saxHandler.startElement(null, "modules", "modules", new AttributesImpl JavaDoc());
144
145                 for (int i = 0; i < dependencies.length; i++) {
146                     IvyNode dependency = dependencies[i];
147                     if (dependency.getModuleRevision() == null || dependency.isCompletelyEvicted()) {
148                         continue;
149                     }
150
151                     startModule(saxHandler, dependency);
152
153                     Set JavaDoc artifactsOfModuleRev = (Set JavaDoc) moduleRevToArtifactsMap.get(dependency.getModuleRevision().getId());
154                     if (artifactsOfModuleRev != null) {
155                         for (Iterator JavaDoc iter = artifactsOfModuleRev.iterator(); iter.hasNext();) {
156                             Artifact artifact = (Artifact) iter.next();
157
158                             startArtifact(saxHandler, artifact);
159
160                             writeOriginLocationIfPresent(ivy, saxHandler, artifact);
161
162                             writeCacheLocation(ivy, saxHandler, artifact);
163
164                             Set JavaDoc artifactDestPaths = (Set JavaDoc) artifactsToCopy.get(artifact);
165                             for (Iterator JavaDoc iterator = artifactDestPaths.iterator(); iterator.hasNext();) {
166                                 String JavaDoc artifactDestPath = (String JavaDoc) iterator.next();
167                                 writeRetrieveLocation(saxHandler, artifactDestPath);
168                             }
169                             saxHandler.endElement(null, "artifact", "artifact");
170                         }
171                     }
172                     saxHandler.endElement(null, "module", "module");
173                 }
174                 saxHandler.endElement(null, "modules", "modules");
175                 saxHandler.endDocument();
176             } finally {
177                 fileOuputStream.close();
178             }
179         } catch (SAXException JavaDoc e) {
180             throw new BuildException("impossible to generate report", e);
181         } catch (TransformerConfigurationException JavaDoc e) {
182             throw new BuildException("impossible to generate report", e);
183         } catch (IOException JavaDoc e) {
184             throw new BuildException("impossible to generate report", e);
185         }
186     }
187
188     private TransformerHandler JavaDoc createTransformerHandler(FileOutputStream JavaDoc fileOuputStream) throws TransformerFactoryConfigurationError JavaDoc, TransformerConfigurationException JavaDoc, SAXException JavaDoc {
189         SAXTransformerFactory JavaDoc transformerFact = (SAXTransformerFactory JavaDoc) SAXTransformerFactory.newInstance();
190         TransformerHandler JavaDoc saxHandler = transformerFact.newTransformerHandler();
191         saxHandler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
192         saxHandler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
193         saxHandler.setResult(new StreamResult JavaDoc(fileOuputStream));
194         return saxHandler;
195     }
196
197     private void startModule(TransformerHandler JavaDoc saxHandler, IvyNode dependency) throws SAXException JavaDoc {
198         AttributesImpl JavaDoc moduleAttrs = new AttributesImpl JavaDoc();
199         moduleAttrs.addAttribute(null, "organisation", "organisation", "CDATA", dependency.getModuleId().getOrganisation());
200         moduleAttrs.addAttribute(null, "name", "name", "CDATA", dependency.getModuleId().getName());
201         moduleAttrs.addAttribute(null, "rev", "rev", "CDATA", dependency.getModuleRevision().getId().getRevision());
202         moduleAttrs.addAttribute(null, "status", "status", "CDATA", dependency.getModuleRevision().getDescriptor().getStatus());
203         saxHandler.startElement(null, "module", "module", moduleAttrs);
204     }
205
206     private void startArtifact(TransformerHandler JavaDoc saxHandler, Artifact artifact) throws SAXException JavaDoc {
207         AttributesImpl JavaDoc artifactAttrs = new AttributesImpl JavaDoc();
208         artifactAttrs.addAttribute(null, "name", "name", "CDATA", artifact.getName());
209         artifactAttrs.addAttribute(null, "ext", "ext", "CDATA", artifact.getExt());
210         artifactAttrs.addAttribute(null, "type", "type", "CDATA", artifact.getType());
211         saxHandler.startElement(null, "artifact", "artifact", artifactAttrs);
212     }
213
214     private void writeOriginLocationIfPresent(Ivy ivy, TransformerHandler JavaDoc saxHandler, Artifact artifact) throws IOException JavaDoc, SAXException JavaDoc {
215         ArtifactOrigin origin = ivy.getSavedArtifactOrigin(_cache, artifact);
216         if (origin != null) {
217             String JavaDoc originName = origin.getLocation();
218             boolean isOriginLocal = origin.isLocal();
219
220             String JavaDoc originLocation;
221             AttributesImpl JavaDoc originLocationAttrs = new AttributesImpl JavaDoc();
222             if (isOriginLocal) {
223                 originLocationAttrs.addAttribute(null, "is-local", "is-local", "CDATA", "true");
224                 File JavaDoc originNameFile = new File JavaDoc(originName);
225                 StringBuffer JavaDoc originNameWithSlashes = new StringBuffer JavaDoc(1000);
226                 replaceFileSeparatorWithSlash(originNameFile, originNameWithSlashes);
227                 originLocation = originNameWithSlashes.toString();
228             } else {
229                 originLocationAttrs.addAttribute(null, "is-local", "is-local", "CDATA", "false");
230                 originLocation = originName;
231             }
232             saxHandler.startElement(null, "origin-location", "origin-location", originLocationAttrs);
233             char[] originLocationAsChars = originLocation.toCharArray();
234             saxHandler.characters(originLocationAsChars, 0, originLocationAsChars.length);
235             saxHandler.endElement(null, "origin-location", "origin-location");
236         }
237     }
238
239     private void writeCacheLocation(Ivy ivy, TransformerHandler JavaDoc saxHandler, Artifact artifact) throws SAXException JavaDoc {
240         ArtifactOrigin origin = ivy.getSavedArtifactOrigin(_cache, artifact);
241         File JavaDoc archiveInCacheFile = ivy.getArchiveFileInCache(_cache, artifact, origin, false);
242         StringBuffer JavaDoc archiveInCachePathWithSlashes = new StringBuffer JavaDoc(1000);
243         replaceFileSeparatorWithSlash(archiveInCacheFile, archiveInCachePathWithSlashes);
244
245         saxHandler.startElement(null, "cache-location", "cache-location", new AttributesImpl JavaDoc());
246         char[] archiveInCachePathAsChars = archiveInCachePathWithSlashes.toString().toCharArray();
247         saxHandler.characters(archiveInCachePathAsChars, 0, archiveInCachePathAsChars.length);
248         saxHandler.endElement(null, "cache-location", "cache-location");
249     }
250
251     private void writeRetrieveLocation(TransformerHandler JavaDoc saxHandler, String JavaDoc artifactDestPath) throws SAXException JavaDoc {
252         artifactDestPath = removeLeadingPath(getProject().getBaseDir(), new File JavaDoc(artifactDestPath));
253         StringBuffer JavaDoc artifactDestPathWithSlashes = new StringBuffer JavaDoc(1000);
254         replaceFileSeparatorWithSlash(new File JavaDoc(artifactDestPath), artifactDestPathWithSlashes);
255
256         saxHandler.startElement(null, "retrieve-location", "retrieve-location", new AttributesImpl JavaDoc());
257         char[] artifactDestPathAsChars = artifactDestPathWithSlashes.toString().toCharArray();
258         saxHandler.characters(artifactDestPathAsChars, 0, artifactDestPathAsChars.length);
259         saxHandler.endElement(null, "retrieve-location", "retrieve-location");
260     }
261     
262     private void replaceFileSeparatorWithSlash(File JavaDoc file, StringBuffer JavaDoc resultPath) {
263         if (file.getParentFile() != null) {
264             replaceFileSeparatorWithSlash(file.getParentFile(), resultPath);
265             resultPath.append('/');
266         }
267
268         if (file.getName().equals("")) {
269             String JavaDoc fileSeparator = System.getProperty("file.separator");
270             String JavaDoc path = file.getPath();
271             while (path.endsWith(fileSeparator)) {
272                 path = path.substring(0, path.length() - fileSeparator.length());
273             }
274             resultPath.append(path);
275         } else {
276             resultPath.append(file.getName());
277         }
278     }
279     
280     // method largely inspired by ant 1.6.5 FileUtils method
281
public String JavaDoc removeLeadingPath(File JavaDoc leading, File JavaDoc path) {
282         String JavaDoc l = leading.getAbsolutePath();
283         String JavaDoc p = path.getAbsolutePath();
284         if (l.equals(p)) {
285             return "";
286         }
287
288         // ensure that l ends with a /
289
// so we never think /foo was a parent directory of /foobar
290
if (!l.endsWith(File.separator)) {
291             l += File.separator;
292         }
293         return (p.startsWith(l)) ? p.substring(l.length()) : p;
294     }
295
296 }
297
Popular Tags