1 5 package fr.jayasoft.ivy.ant; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.text.ParseException ; 11 import java.util.Date ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import javax.xml.transform.OutputKeys ; 19 import javax.xml.transform.TransformerConfigurationException ; 20 import javax.xml.transform.TransformerFactoryConfigurationError ; 21 import javax.xml.transform.sax.SAXTransformerFactory ; 22 import javax.xml.transform.sax.TransformerHandler ; 23 import javax.xml.transform.stream.StreamResult ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.helpers.AttributesImpl ; 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 40 public class IvyArtifactReport extends IvyTask { 41 private File _tofile; 42 private String _conf; 43 private String _pattern; 44 private boolean _haltOnFailure = true; 45 private File _cache; 46 47 public File getTofile() { 48 return _tofile; 49 } 50 public void setTofile(File tofile) { 51 _tofile = tofile; 52 } 53 public String getConf() { 54 return _conf; 55 } 56 public void setConf(String conf) { 57 _conf = conf; 58 } 59 public String getPattern() { 60 return _pattern; 61 } 62 public void setPattern(String 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 getCache() { 72 return _cache; 73 } 74 public void setCache(File 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 _organisation = getProperty(null, ivy, "ivy.organisation"); 88 String _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 [] confs = splitConfs(_conf); 113 IvyNode[] dependencies = ivy.getDependencies((ModuleDescriptor) getProject().getReference("ivy.resolved.descriptor"), confs, _cache, new Date (), null, doValidate(ivy)); 114 115 Map artifactsToCopy = ivy.determineArtifactsToCopy(new ModuleId(_organisation, _module), confs, _cache, _pattern, null); 116 Map moduleRevToArtifactsMap = new HashMap (); 117 for (Iterator iter = artifactsToCopy.keySet().iterator(); iter.hasNext();) { 118 Artifact artifact = (Artifact) iter.next(); 119 Set moduleRevArtifacts = (Set ) moduleRevToArtifactsMap.get(artifact.getModuleRevisionId()); 120 if (moduleRevArtifacts == null) { 121 moduleRevArtifacts = new HashSet (); 122 moduleRevToArtifactsMap.put(artifact.getModuleRevisionId(), moduleRevArtifacts); 123 } 124 moduleRevArtifacts.add(artifact); 125 } 126 127 generateXml(ivy, dependencies, moduleRevToArtifactsMap, artifactsToCopy); 128 } catch (ParseException e) { 129 log(e.getMessage(), Project.MSG_ERR); 130 throw new BuildException("syntax errors in ivy file: "+e, e); 131 } catch (IOException e) { 132 throw new BuildException("impossible to generate report: "+e, e); 133 } 134 } 135 136 private void generateXml(Ivy ivy, IvyNode[] dependencies, Map moduleRevToArtifactsMap, Map artifactsToCopy) { 137 try { 138 FileOutputStream fileOuputStream = new FileOutputStream (_tofile); 139 try { 140 TransformerHandler saxHandler = createTransformerHandler(fileOuputStream); 141 142 saxHandler.startDocument(); 143 saxHandler.startElement(null, "modules", "modules", new AttributesImpl ()); 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 artifactsOfModuleRev = (Set ) moduleRevToArtifactsMap.get(dependency.getModuleRevision().getId()); 154 if (artifactsOfModuleRev != null) { 155 for (Iterator 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 artifactDestPaths = (Set ) artifactsToCopy.get(artifact); 165 for (Iterator iterator = artifactDestPaths.iterator(); iterator.hasNext();) { 166 String artifactDestPath = (String ) 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 e) { 180 throw new BuildException("impossible to generate report", e); 181 } catch (TransformerConfigurationException e) { 182 throw new BuildException("impossible to generate report", e); 183 } catch (IOException e) { 184 throw new BuildException("impossible to generate report", e); 185 } 186 } 187 188 private TransformerHandler createTransformerHandler(FileOutputStream fileOuputStream) throws TransformerFactoryConfigurationError , TransformerConfigurationException , SAXException { 189 SAXTransformerFactory transformerFact = (SAXTransformerFactory ) SAXTransformerFactory.newInstance(); 190 TransformerHandler saxHandler = transformerFact.newTransformerHandler(); 191 saxHandler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 192 saxHandler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); 193 saxHandler.setResult(new StreamResult (fileOuputStream)); 194 return saxHandler; 195 } 196 197 private void startModule(TransformerHandler saxHandler, IvyNode dependency) throws SAXException { 198 AttributesImpl moduleAttrs = new AttributesImpl (); 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 saxHandler, Artifact artifact) throws SAXException { 207 AttributesImpl artifactAttrs = new AttributesImpl (); 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 saxHandler, Artifact artifact) throws IOException , SAXException { 215 ArtifactOrigin origin = ivy.getSavedArtifactOrigin(_cache, artifact); 216 if (origin != null) { 217 String originName = origin.getLocation(); 218 boolean isOriginLocal = origin.isLocal(); 219 220 String originLocation; 221 AttributesImpl originLocationAttrs = new AttributesImpl (); 222 if (isOriginLocal) { 223 originLocationAttrs.addAttribute(null, "is-local", "is-local", "CDATA", "true"); 224 File originNameFile = new File (originName); 225 StringBuffer originNameWithSlashes = new StringBuffer (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 saxHandler, Artifact artifact) throws SAXException { 240 ArtifactOrigin origin = ivy.getSavedArtifactOrigin(_cache, artifact); 241 File archiveInCacheFile = ivy.getArchiveFileInCache(_cache, artifact, origin, false); 242 StringBuffer archiveInCachePathWithSlashes = new StringBuffer (1000); 243 replaceFileSeparatorWithSlash(archiveInCacheFile, archiveInCachePathWithSlashes); 244 245 saxHandler.startElement(null, "cache-location", "cache-location", new AttributesImpl ()); 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 saxHandler, String artifactDestPath) throws SAXException { 252 artifactDestPath = removeLeadingPath(getProject().getBaseDir(), new File (artifactDestPath)); 253 StringBuffer artifactDestPathWithSlashes = new StringBuffer (1000); 254 replaceFileSeparatorWithSlash(new File (artifactDestPath), artifactDestPathWithSlashes); 255 256 saxHandler.startElement(null, "retrieve-location", "retrieve-location", new AttributesImpl ()); 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 file, StringBuffer resultPath) { 263 if (file.getParentFile() != null) { 264 replaceFileSeparatorWithSlash(file.getParentFile(), resultPath); 265 resultPath.append('/'); 266 } 267 268 if (file.getName().equals("")) { 269 String fileSeparator = System.getProperty("file.separator"); 270 String 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 public String removeLeadingPath(File leading, File path) { 282 String l = leading.getAbsolutePath(); 283 String p = path.getAbsolutePath(); 284 if (l.equals(p)) { 285 return ""; 286 } 287 288 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 |