@@ -3,13 +3,14 @@ package de.upb.cs.swt.delphi.crawler.processing
33import akka .actor .{Actor , ActorLogging , Props }
44import de .upb .cs .swt .delphi .crawler .Configuration
55import de .upb .cs .swt .delphi .crawler .discovery .maven .MavenIdentifier
6- import de .upb .cs .swt .delphi .crawler .preprocessing .{JarFile , MavenDownloader , PomFile }
6+ import de .upb .cs .swt .delphi .crawler .preprocessing .{MavenDownloader , PomFile }
77import org .apache .maven .artifact .repository .metadata .io .xpp3 .MetadataXpp3Reader
88import org .apache .maven .artifact .versioning .ComparableVersion
9- import org .apache .maven .model .Dependency
9+ import org .apache .maven .model .{ Dependency , Model }
1010import org .apache .maven .model .io .xpp3 .MavenXpp3Reader
1111
1212import scala .collection .JavaConverters ._
13+ import scala .util .Try
1314
1415/*
1516 * This class extracts the dependencies for a project from a POM file.
@@ -26,79 +27,75 @@ class MavenDependencyActor(configuration: Configuration) extends Actor with Acto
2627
2728 override def receive : Receive = {
2829 case pf : PomFile =>
29- try {
30- sender() ! getDependencies(pf)
31- } catch {
32- case e : Exception => {
30+ Try (getDependencies(pf)) match {
31+ case util.Success (dx) => sender() ! dx
32+ case util.Failure (e) => {
3333 log.warning(" Dependency mapper threw exception " + e)
3434 sender() ! akka.actor.Status .Failure (e)
3535 }
3636 }
3737 }
3838
39- def getDependencies (pomFile : PomFile ): Set [MavenIdentifier ] = {
40- val pomObj = pomReader.read(pomFile.is)
41- pomFile.is.close()
42-
43- def resolveProperty (str : String ) = {
44- if (str == null || ! str.startsWith(" $" )) {
45- str
46- } else {
47- val extractedVar = str.drop(2 ).dropRight(1 )
48- val splitVar = extractedVar.split(" \\ ." , 2 )
49- if (splitVar(0 ) == " project" || splitVar(0 ) == " pom" ){
50- val evaluatedVar = splitVar(1 ) match {
51- case " groupId" => pomObj.getGroupId
52- case " artifactId" => pomObj.getArtifactId
53- case " version" => pomObj.getVersion
54- case _ => null
55- }
56- evaluatedVar
57- } else {
58- val evaluatedVar = pomObj.getProperties.getProperty(extractedVar)
59- evaluatedVar
39+ def resolveProperty (str : String , pomObj : Model ): Option [String ] = {
40+ if (str == null || ! str.startsWith(" $" )){
41+ Option (str)
42+ } else {
43+ val extractedVar = str.drop(2 ).dropRight(1 )
44+ val splitVar = extractedVar.split(" \\ ." , 2 )
45+ if (splitVar(0 ) == " project" || splitVar(0 ) == " pom" ) {
46+ val evaluatedVar = splitVar(1 ) match {
47+ case " groupId" => Some (pomObj.getGroupId)
48+ case " artifactId" => Some (pomObj.getArtifactId)
49+ case " version" => Some (pomObj.getVersion)
50+ case _ => None
6051 }
52+ evaluatedVar
53+ } else {
54+ val evaluatedVar = pomObj.getProperties.getProperty(extractedVar)
55+ Option (evaluatedVar)
6156 }
6257 }
58+ }
6359
64- def resolveIdentifier (d : Dependency ): MavenIdentifier = {
65- val groupId = resolveProperty(d.getGroupId)
66- val artifactId = resolveProperty(d.getArtifactId)
60+ def resolveIdentifier (d : Dependency , pomObj : Model ): Try [MavenIdentifier ] = {
61+ Try {
62+ val groupId = resolveProperty(d.getGroupId, pomObj).getOrElse(throw new Exception )
63+ val artifactId = resolveProperty(d.getArtifactId, pomObj).getOrElse(throw new Exception )
64+ val versionSpec = resolveProperty(d.getVersion, pomObj).getOrElse(throw new Exception )
6765
68- if (groupId == null || artifactId == null ) {
69- MavenIdentifier (null , null , null , null )
70- } else {
71- val versionSpec = resolveProperty(d.getVersion)
72- val tempId = MavenIdentifier (configuration.mavenRepoBase.toString, groupId, artifactId, versionSpec)
66+ val tempId = MavenIdentifier (configuration.mavenRepoBase.toString, groupId, artifactId, versionSpec)
7367
74- try {
75- val downloader = new MavenDownloader (tempId)
76- val metaFile = downloader.downloadMeta
77- val metaObj = metaReader.read(metaFile.is)
78- metaFile.is.close
79- val versionList = metaObj.getVersioning.getVersions.asScala
80- if (versionList.contains(versionSpec) || versionSpec == null ) {
81- tempId
82- } else {
83- val versionComp = new ComparableVersion (versionSpec)
84- val versionNum = versionList.indexWhere(v => new ComparableVersion (v).compareTo(versionComp) >= 0 )
85- val version = if (versionNum == - 1 ) versionList.last else if (versionNum == 0 ) versionList.head else versionList(versionNum - 1 )
86- MavenIdentifier (configuration.mavenRepoBase.toString, groupId, artifactId, version)
87- }
88- } catch {
89- case e : java.io.FileNotFoundException => {
90- log.warning(" Dependency {}:{} does not exist" , groupId, artifactId)
91- MavenIdentifier (null , null , null , null )
92- }
68+ val downloader = new MavenDownloader (tempId)
69+ val metaFile = downloader.downloadMeta()
70+ val metaObj = metaReader.read(metaFile.is)
71+ metaFile.is.close()
72+ val versionList = metaObj.getVersioning.getVersions.asScala
73+ if (versionList.contains(versionSpec) || versionSpec == null ) {
74+ tempId
75+ } else {
76+ val versionComp = new ComparableVersion (versionSpec)
77+ val versionNum = versionList.indexWhere(v => new ComparableVersion (v).compareTo(versionComp) >= 0 )
78+ val version = versionNum match {
79+ case - 1 => versionList.lastOption
80+ case 0 => versionList.headOption
81+ case _ => Option (versionList(versionNum - 1 ))
82+ }
83+ version match {
84+ case Some (v) =>
85+ MavenIdentifier (configuration.mavenRepoBase.toString, groupId, artifactId, v)
86+ case None =>
87+ throw new Exception
9388 }
9489 }
9590 }
91+ }
92+
93+ def getDependencies (pomFile : PomFile ): Set [MavenIdentifier ] = {
94+ val pomObj = pomReader.read(pomFile.is)
95+ pomFile.is.close()
9696
97- val pomSet = pomObj.getDependencies()
98- .asScala.toSet[Dependency ].map(resolveIdentifier).filter(
99- m => ! (m.version == null || m.groupId == null || m.artifactId == null )
100- )
101- pomSet
97+ val pomSet = pomObj.getDependencies.asScala.toSet[Dependency ].map(resolveIdentifier(_, pomObj))
98+ for (util.Success (id) <- pomSet) yield id
10299 }
103100}
104101
0 commit comments