-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from chains-project/download_jars
Adding download jar feature
- Loading branch information
Showing
3 changed files
with
120 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,92 @@ | ||
package se.kth.breaking_changes; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
||
@lombok.Setter | ||
public class ApiMetadata { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ApiMetadata.class); | ||
|
||
String name; | ||
|
||
Path file; | ||
|
||
private static final Map<String, String> repositoryUrls = Map.of( | ||
"mavenCentral", "https://repo1.maven.org/maven2/", | ||
"jenkins","https://repo.jenkins-ci.org/artifactory/releases/" | ||
); | ||
|
||
|
||
public ApiMetadata(String name, Path file) { | ||
this.name = name; | ||
this.file = file; | ||
|
||
} | ||
|
||
public String getName() { | ||
return name.contains(".jar") ? name.substring(0, name.indexOf(".jar")) : name; | ||
} | ||
|
||
/** | ||
* Downloads the version of the dependency from the maven repository | ||
* | ||
* @param artifactId the name of the dependency | ||
* @param groupId the group id of the dependency | ||
* @param version the version of the dependency | ||
* @return true if the download was successful, false otherwise | ||
*/ | ||
public static boolean download(String groupId, String artifactId, String version) { | ||
|
||
|
||
|
||
/* | ||
* Maven repository link for the previous version of the dependency | ||
*/ | ||
final OkHttpClient httpConnector | ||
= new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS) | ||
.writeTimeout(120, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build(); | ||
|
||
String jarName = artifactId + "-" + version + ".jar"; | ||
|
||
for (String repository : repositoryUrls.values()) { | ||
String artifactUrl = getArtifactUrl(groupId, artifactId, version, repository); | ||
try (Response response = httpConnector.newCall(new Request.Builder().url(artifactUrl).build()).execute()) { | ||
if (response.code() == 200) { | ||
downloadAndConvert(response, "jars" + File.separator + jarName); | ||
return true; | ||
} | ||
} catch (IOException e) { | ||
log.error("Maven source links could not be found for the updated dependency {}.", artifactId, e); | ||
return false; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static String getArtifactUrl(String groupId, String artifactId, String version, String repositoryUrl) { | ||
groupId = groupId.replace('.', '/'); | ||
return repositoryUrl + groupId + "/" + artifactId + "/" + version + "/"; | ||
} | ||
|
||
private static void downloadAndConvert(Response response, String fileName) throws IOException { | ||
assert response.body() != null; | ||
byte[] bytes = response.body().bytes(); | ||
try (FileOutputStream fos = new FileOutputStream(fileName)) { | ||
fos.write(bytes); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,57 @@ | ||
package se.kth.breaking_changes; | ||
|
||
import se.kth.data.BreakingUpdateMetadata; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Set; | ||
import java.util.List; | ||
|
||
import static se.kth.data.Main.getBreakingCommit; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
|
||
JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze( | ||
Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.18.1.jar"), | ||
Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.19.1.jar") | ||
); | ||
// JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze( | ||
// Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.18.1.jar"), | ||
// Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.19.1.jar") | ||
// ); | ||
// | ||
// Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp(); | ||
// | ||
// for (ApiChange apiChange : apiChanges) { | ||
// System.out.println(apiChange); | ||
// } | ||
|
||
|
||
List<BreakingUpdateMetadata> list = | ||
getBreakingCommit(Path.of("/Users/frank/Documents/Work/PHD/chains-project/paper/bump/data/benchmark")); | ||
|
||
|
||
Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp(); | ||
for (BreakingUpdateMetadata breakingUpdate : list) { | ||
boolean result = ApiMetadata.download(breakingUpdate.updatedDependency().dependencyGroupID(), | ||
breakingUpdate.updatedDependency().dependencyArtifactID(), | ||
breakingUpdate.updatedDependency().newVersion()); | ||
|
||
if (result) { | ||
System.out.println("Downloaded " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion()); | ||
} else { | ||
System.out.println("Failed to download " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion()); | ||
} | ||
|
||
result = ApiMetadata.download(breakingUpdate.updatedDependency().dependencyGroupID(), | ||
breakingUpdate.updatedDependency().dependencyArtifactID(), | ||
breakingUpdate.updatedDependency().previousVersion()); | ||
|
||
if (result) { | ||
System.out.println("Downloaded " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion()); | ||
} else { | ||
System.out.println("Failed to download " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion()); | ||
} | ||
|
||
for (ApiChange apiChange : apiChanges) { | ||
System.out.println(apiChange); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |