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

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

https://bitbucket.org/jwalton/opup
Scala | 113 lines | 88 code | 23 blank | 2 comment | 11 complexity | ffdc633fd7afe131bbb8493f7be131d9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import javax.xml.xpath.XPathExpressionException;
  11. import org.apache.maven.artifact.versioning.ArtifactVersion;
  12. import org.xml.sax.SAXException;
  13. import scala.collection.JavaConverters._
  14. class UpgradeSchedule(val newVersions: Map[String, List[ArtifactVersion]], val newProperties: Map[String, List[ArtifactVersion]])
  15. {
  16. def withUpgrades(): UpgradeSchedule =
  17. {
  18. val versions = UpgradeSchedule.notEmptyLists(newVersions);
  19. val properties = UpgradeSchedule.notEmptyLists(newProperties);
  20. return new UpgradeSchedule(versions, properties);
  21. }
  22. def getNewVersions(): Map[String, List[ArtifactVersion]] = new TreeMap[String, List[ArtifactVersion]](newVersions);
  23. def getNewProperties(): Map[String, List[ArtifactVersion]] = new TreeMap[String, List[ArtifactVersion]](newProperties);
  24. }
  25. object UpgradeSchedule
  26. {
  27. // XXX Should take more general data structures
  28. def from(currentVersions: Map[String, ArtifactVersion], ps: PomScanner,
  29. vc: VersionClient, obsolete: ObsoleteAdvice, report: ReportTarget,
  30. upgradeTarget: (String, ArtifactVersion) => Boolean): UpgradeSchedule =
  31. {
  32. val newVersions = new HashMap[String, List[ArtifactVersion]]();
  33. val newProperties = new HashMap[String, List[ArtifactVersion]]();
  34. for (e <- currentVersions.entrySet.asScala)
  35. {
  36. val coords = e.getKey();
  37. val version = e.getValue();
  38. // System.out.println(coords + " " + version);
  39. val sa = coords.split(":");
  40. if (sa.length < 2 || sa.length > 3)
  41. {
  42. throw new RuntimeException("Bad coord: " + coords);
  43. }
  44. val packaging =
  45. if (sa.length == 3)
  46. {
  47. sa(2);
  48. }
  49. else
  50. {
  51. "jar";
  52. }
  53. for (advice <- obsolete.getAdvice(sa(0) + ':' + sa(1)))
  54. {
  55. report.obsolete(sa(0) + ':' + sa(1), advice)
  56. }
  57. val knownVersions = vc.getVersions(sa(0), sa(1), packaging, report);
  58. val aims = aimedFor(sa(0) + ':' + sa(1), knownVersions, upgradeTarget);
  59. var moreRecent = VersionClientUtil.since(aims, version).asJava
  60. val prop = ps.getPropertyForArtifactVersion(coords);
  61. if (prop != null)
  62. {
  63. if (newProperties.containsKey(prop))
  64. {
  65. val existingNewVersions = newProperties.get(prop);
  66. if (!moreRecent.equals(existingNewVersions))
  67. {
  68. moreRecent = new ArrayList[ArtifactVersion](moreRecent);
  69. moreRecent.retainAll(existingNewVersions);
  70. }
  71. }
  72. newProperties.put(prop, moreRecent);
  73. }
  74. else
  75. {
  76. newVersions.put(coords, moreRecent);
  77. }
  78. }
  79. return new UpgradeSchedule(newVersions, newProperties);
  80. }
  81. def aimedFor(artifact: String, knownVersions: scala.List[ArtifactVersion],
  82. isUpgradeTarget: ((String, ArtifactVersion) => Boolean)): scala.List[ArtifactVersion] =
  83. {
  84. return knownVersions.filter(isUpgradeTarget(artifact, _))
  85. }
  86. def notEmptyLists(m: Map[String, List[ArtifactVersion]]): Map[String, List[ArtifactVersion]] =
  87. {
  88. (for ((k, v) <- m.asScala if !v.isEmpty()) yield (k, v)).toMap.asJava
  89. }
  90. }