PageRenderTime 596ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/old/java/com/atlassian/labs/opup/Report.java

https://bitbucket.org/jwalton/opup
Java | 167 lines | 133 code | 34 blank | 0 comment | 2 complexity | ba7506eb6d30e63fbc1f7e4c8e67f248 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.Writer;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Collections;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. import org.apache.commons.io.FileUtils;
  15. import org.apache.maven.artifact.versioning.ArtifactVersion;
  16. import org.apache.velocity.Template;
  17. import org.apache.velocity.VelocityContext;
  18. import org.apache.velocity.app.VelocityEngine;
  19. public class Report implements ReportTarget
  20. {
  21. private final String groupId, artifactId;
  22. private final Collection<MultipleThingVersionWarning> thingVersionWarnings = new ArrayList<MultipleThingVersionWarning>();
  23. private final Collection<String> miscWarnings = new ArrayList<String>();
  24. private Map<String, ArtifactVersion> currentVersions;
  25. private UpgradeSchedule schedule;
  26. private String strategy;
  27. public Report(String groupId, String artifactId)
  28. {
  29. this.groupId = groupId;
  30. this.artifactId = artifactId;
  31. }
  32. public void setUpgradeStrategy(String name)
  33. {
  34. this.strategy = name;
  35. }
  36. public void writeReport(Writer w) throws IOException
  37. {
  38. VelocityEngine ve = new VelocityEngine();
  39. Properties props = new Properties();
  40. props.setProperty("resource.loader", "class");
  41. props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  42. props.setProperty("eventhandler.referenceinsertion.class", "org.apache.velocity.app.event.implement.EscapeHtmlReference");
  43. ve.init(props);
  44. VelocityContext ctxt = new VelocityContext();
  45. ctxt.put("groupId", groupId);
  46. ctxt.put("artifactId", artifactId);
  47. ctxt.put("upgradeStrategy", strategy);
  48. ctxt.put("warnings", thingVersionWarnings);
  49. ctxt.put("miscWarnings", miscWarnings);
  50. ctxt.put("currentVersions", currentVersions);
  51. ctxt.put("upgradeSchedule", schedule);
  52. Template template = ve.getTemplate("/com/atlassian/labs/opup/report.vm");
  53. template.merge(ctxt, w);
  54. w.flush();
  55. }
  56. public void copyStylesheet(File base) throws IOException
  57. {
  58. InputStream in = getClass().getResourceAsStream("style.css");
  59. if (in == null)
  60. {
  61. throw new FileNotFoundException("Resource style.css not found");
  62. }
  63. FileUtils.copyInputStreamToFile(in, new File(base, "style.css"));
  64. }
  65. @Override
  66. public void multipleVersions(String artifact, Collection<ArtifactVersion> versions)
  67. {
  68. addWarning(new MultipleThingVersionWarning("artifact", artifact, versions));
  69. }
  70. @Override
  71. public void multipleValues(String propname, Collection<ArtifactVersion> values)
  72. {
  73. addWarning(new MultipleThingVersionWarning("property", propname, values));
  74. }
  75. @Override
  76. public void obsolete(String artifact, String advice)
  77. {
  78. miscWarnings.add(artifact + " is obsolete. " + advice);
  79. }
  80. private void addWarning(MultipleThingVersionWarning w)
  81. {
  82. thingVersionWarnings.add(w);
  83. System.out.println(w);
  84. }
  85. @Override
  86. public void packagingChange(String coords, ArtifactVersion latestVersionWithDifferentPackaging,
  87. String differentPackaging, String requiredPackaging, ArtifactVersion artifactVersion)
  88. {
  89. String diag = "Latest " + coords + " ("
  90. + latestVersionWithDifferentPackaging + ") is a " + differentPackaging + ", not a " + requiredPackaging
  91. + ". Sticking with " + artifactVersion + ".";
  92. System.out.println(diag);
  93. miscWarnings.add(diag);
  94. }
  95. public static class MultipleThingVersionWarning
  96. {
  97. private final String thingType;
  98. private final String thingName;
  99. private final Collection<ArtifactVersion> versions;
  100. public MultipleThingVersionWarning(String thingType, String thingName, Collection<ArtifactVersion> versions)
  101. {
  102. this.thingType = thingType;
  103. this.thingName = thingName;
  104. this.versions = versions;
  105. }
  106. public String getType()
  107. {
  108. return thingType;
  109. }
  110. public String getName()
  111. {
  112. return thingName;
  113. }
  114. public Collection<ArtifactVersion> getVersions()
  115. {
  116. List<ArtifactVersion> inOrder = new ArrayList<ArtifactVersion>(versions);
  117. Collections.sort(inOrder);
  118. return inOrder;
  119. }
  120. public String toString()
  121. {
  122. return "Multiple versions for " + thingType + " '" + thingName + "': " + versions;
  123. }
  124. }
  125. public void schedule(Map<String, ArtifactVersion> currentVersions, Map<String, ArtifactVersion> currentProperties, UpgradeSchedule upgradeSchedule)
  126. {
  127. Map<String, ArtifactVersion> current = new HashMap<String, ArtifactVersion>();
  128. current.putAll(currentVersions);
  129. current.putAll(currentProperties);
  130. this.currentVersions = current ;
  131. this.schedule = upgradeSchedule;
  132. }
  133. }