PageRenderTime 37ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/main/scala/com/atlassian/labs/opup/Report.scala

https://bitbucket.org/jwalton/opup
Scala | 188 lines | 149 code | 38 blank | 1 comment | 11 complexity | caaaf093cc835cedc0c02628854eb8a9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.{File, FileNotFoundException, PrintWriter, Writer}
  3. import java.lang.Override
  4. import java.util.Map
  5. import java.util.ArrayList
  6. import java.util.Collection
  7. import java.util.Collections
  8. import java.util.HashMap
  9. import java.util.Properties
  10. import scala.collection.JavaConverters._
  11. import org.apache.commons.io.FileUtils
  12. import org.apache.maven.artifact.versioning.ArtifactVersion
  13. import org.apache.velocity.app.VelocityEngine
  14. import org.apache.velocity.VelocityContext
  15. import scala.collection.mutable.Buffer
  16. import scala.collection.mutable.ListBuffer
  17. class Report(groupId: String, artifactId: String, version: String) extends ReportTarget {
  18. private val thingVersionWarnings = new ArrayList[MultipleThingVersionWarning]();
  19. private val miscWarnings = new ArrayList[String]();
  20. private var currentVersions: Map[String, ArtifactVersion] = _;
  21. private var schedule: UpgradeSchedule = _;
  22. private var strategy: String = _;
  23. def setUpgradeStrategy(name: String)
  24. {
  25. this.strategy = name;
  26. }
  27. def writeReport(w: Writer) =
  28. {
  29. val ve = new VelocityEngine();
  30. val props = new Properties();
  31. props.setProperty("resource.loader", "class");
  32. props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  33. props.setProperty("eventhandler.referenceinsertion.class", "org.apache.velocity.app.event.implement.EscapeHtmlReference");
  34. ve.init(props);
  35. val ctxt = new VelocityContext();
  36. ctxt.put("groupId", groupId);
  37. ctxt.put("artifactId", artifactId);
  38. ctxt.put("version", version);
  39. ctxt.put("upgradeStrategy", strategy);
  40. ctxt.put("warnings", thingVersionWarnings);
  41. ctxt.put("miscWarnings", miscWarnings);
  42. ctxt.put("currentVersions", currentVersions);
  43. ctxt.put("upgradeSchedule", schedule);
  44. val template = ve.getTemplate("/com/atlassian/labs/opup/report.vm");
  45. template.merge(ctxt, w);
  46. w.flush();
  47. }
  48. def copyStylesheet(base: File) =
  49. {
  50. val in = getClass().getResourceAsStream("style.css");
  51. if (in == null)
  52. {
  53. throw new FileNotFoundException("Resource style.css not found");
  54. }
  55. FileUtils.copyInputStreamToFile(in, new File(base, "style.css"));
  56. }
  57. def writeJunitReport(w: Writer)
  58. {
  59. val items = new ListBuffer[(String, ArtifactVersion, ArtifactVersion)]
  60. for ((k, v) <- currentVersions.asScala)
  61. {
  62. val x = List(schedule.getNewVersions().asScala.get(k).map(_.asScala),
  63. schedule.getNewProperties().asScala.get(k).map(_.asScala)).flatten.flatten
  64. val i = (k, v, if (x.isEmpty) v else x.max)
  65. items += i
  66. }
  67. w.write(JUnitReport.toJunitReport(items.toList).toString)
  68. }
  69. def writeOpUpUpgradeReport(w: PrintWriter): Unit =
  70. {
  71. val items = new ListBuffer[(String, ArtifactVersion, ArtifactVersion)]
  72. for ((k, currentVersion) <- currentVersions.asScala)
  73. {
  74. val x = List(schedule.getNewVersions().asScala.get(k).map(_.asScala),
  75. schedule.getNewProperties().asScala.get(k).map(_.asScala)).flatten.flatten
  76. val availableVersion = if (x.isEmpty) currentVersion else x.max
  77. if (currentVersion.compareTo(availableVersion) < 0)
  78. {
  79. w.println(s"$k,$currentVersion,$availableVersion")
  80. }
  81. }
  82. }
  83. @Override
  84. def multipleVersions(artifact: String, versions: Collection[ArtifactVersion])
  85. {
  86. addWarning(new MultipleThingVersionWarning("artifact", artifact, versions.asScala));
  87. }
  88. @Override
  89. def multipleValues(propname: String, values: Collection[ArtifactVersion])
  90. {
  91. addWarning(new MultipleThingVersionWarning("property", propname, values.asScala));
  92. }
  93. @Override
  94. def obsolete(artifact: String, advice: String)
  95. {
  96. miscWarnings.add(artifact + " is obsolete. " + advice);
  97. }
  98. @Override
  99. def undefinedProperty(artifact: String, propname: String)
  100. {
  101. miscWarnings.add("Undefined property " + propname + " used in as version for " + artifact)
  102. }
  103. def addWarning(w: MultipleThingVersionWarning)
  104. {
  105. thingVersionWarnings.add(w);
  106. println(w);
  107. }
  108. @Override
  109. def packagingChange(coords: String, latestVersionWithDifferentPackaging: ArtifactVersion,
  110. differentPackaging: String, requiredPackaging: String, artifactVersion: ArtifactVersion)
  111. {
  112. val reportPackaging = if (differentPackaging.isEmpty) "of undefined packaging" else "a " + differentPackaging
  113. val diag = "Latest " + coords + " (" +
  114. latestVersionWithDifferentPackaging + ") is " + reportPackaging + ", not a " + requiredPackaging +
  115. ". Sticking with " + artifactVersion + ".";
  116. miscWarnings.add(diag);
  117. }
  118. def implementationLimitation(note: String)
  119. {
  120. miscWarnings.add(note)
  121. }
  122. // This is written like a bean so Velocity can use it
  123. class MultipleThingVersionWarning(thingType: String, thingName: String, versions: Iterable[ArtifactVersion])
  124. {
  125. def getType(): String =
  126. {
  127. return thingType;
  128. }
  129. def getName(): String =
  130. {
  131. return thingName;
  132. }
  133. def getVersions(): Collection[ArtifactVersion] =
  134. {
  135. return versions.toSeq.sorted.asJava
  136. }
  137. override def toString(): String =
  138. {
  139. return "Multiple versions for " + thingType + " '" + thingName + "': " + versions;
  140. }
  141. }
  142. def schedule(currentVersions: Map[String, ArtifactVersion], currentProperties: Map[String, ArtifactVersion], upgradeSchedule: UpgradeSchedule)
  143. {
  144. val current = new HashMap[String, ArtifactVersion]();
  145. current.putAll(currentVersions);
  146. current.putAll(currentProperties);
  147. this.currentVersions = current ;
  148. this.schedule = upgradeSchedule;
  149. }
  150. }