Skip to content

Commit

Permalink
Change location of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed Jul 23, 2016
1 parent eaa5167 commit f77a826
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,42 @@
*/
public class ProblogDownloadManager {

public static final URI DEFAULT_PROBLOG_DOWNLOAD_URI = URI
.create("https://bitbucket.org/problog/problog/get/master.zip");

public static final String DEFAULT_PROBLOG_ZIP_FILE = ResourceConstant.BORN_WORKING_DIRECTORY
+ ResourceConstant.FILE_SEPARATOR + "problog-2.1-SNAPSHOT.zip";

public static final String DEFAULT_VERIFICATION_FILE = DEFAULT_PROBLOG_ZIP_FILE + ".sha";

final String MD5 = "MD5";
final String SHA_1 = "SHA-1";
final String SHA_256 = "SHA-256";
final String VERIFICATION_FILE_EXTENSION = ".sha";

private final URI problogDownloadUri;
private final String problogZipFileName;
private final String verificationFile;

/**
* Creates a new ProbLog download manager.
*/
public ProblogDownloadManager() {
public ProblogDownloadManager(URI problogDownloadUri, String problogZipFileName) {
Objects.nonNull(problogDownloadUri);
Objects.nonNull(problogZipFileName);
this.problogDownloadUri = problogDownloadUri;
this.problogZipFileName = problogZipFileName;
this.verificationFile = problogZipFileName + VERIFICATION_FILE_EXTENSION;
}

/**
* Returns the downloaded ZIP file name.
* Returns the name of the ZIP file that is downloaded.
*
* @return the downloaded ZIP file name
* @return the name of the ZIP file that is downloaded
*/
public String getProblogZipFile() {
return DEFAULT_PROBLOG_ZIP_FILE;
return this.problogZipFileName;
}

/**
* Returns the URI used to download ProbLog.
*
* @return the URI used to download ProbLog
*/
public URI getProblogDownloadUri() {
return this.problogDownloadUri;
}

/**
Expand All @@ -61,11 +72,11 @@ public String getProblogZipFile() {
* if something went wrong with the input/output
*/
public void downloadIfNecessary() throws IOException {
boolean downloadIsNecessary = checkIfDownloadIsNecessary(DEFAULT_PROBLOG_ZIP_FILE);
boolean downloadIsNecessary = checkIfDownloadIsNecessary(problogZipFileName);
if (downloadIsNecessary) {
downloadProblog(DEFAULT_PROBLOG_ZIP_FILE);
downloadProblog(problogZipFileName);
String verificationCode = computeVerificationCode();
storeVerificationCode(verificationCode, DEFAULT_VERIFICATION_FILE);
storeVerificationCode(verificationCode, verificationFile);
}
}

Expand All @@ -79,7 +90,7 @@ public void downloadIfNecessary() throws IOException {
*/
void downloadProblog(String problogZipFile) throws IOException {
Objects.requireNonNull(problogZipFile);
ReadableByteChannel channel = Channels.newChannel(DEFAULT_PROBLOG_DOWNLOAD_URI.toURL().openStream());
ReadableByteChannel channel = Channels.newChannel(problogDownloadUri.toURL().openStream());
FileOutputStream output = new FileOutputStream(Decompressor.ensurePath(problogZipFile));
output.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
output.close();
Expand All @@ -88,7 +99,7 @@ void downloadProblog(String problogZipFile) throws IOException {
String readVerificationCode() {
String ret = "";
try {
BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_VERIFICATION_FILE));
BufferedReader reader = new BufferedReader(new FileReader(verificationFile));
StringBuilder sb = new StringBuilder();
reader.lines().forEach(line -> {
sb.append(line.trim());
Expand Down Expand Up @@ -120,7 +131,7 @@ byte[] getBytes(InputStream inputStream) throws IOException {
String computeVerificationCode() {
String ret = "";
try {
ret = asHexString(computeVerificationCode(getBytes(new FileInputStream(DEFAULT_PROBLOG_ZIP_FILE))));
ret = asHexString(computeVerificationCode(getBytes(new FileInputStream(problogZipFileName))));
} catch (IOException e) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.Objects;
import java.util.function.Function;
import java.util.zip.ZipEntry;
Expand All @@ -23,20 +24,32 @@
*/
public class ProblogProcessor implements Function<String, String> {

public static final String DEFAULT_INPUT_FILE_FOR_PROBLOG = ResourceConstant.BORN_WORKING_DIRECTORY
+ ResourceConstant.FILE_SEPARATOR + "input_for_problog.txt";
public static final URI DEFAULT_PROBLOG_DOWNLOAD_URI = URI
.create("https://bitbucket.org/problog/problog/get/master.zip");

public static final String DEFAULT_OUTPUT_FILE_FROM_PROBLOG = ResourceConstant.BORN_WORKING_DIRECTORY
+ ResourceConstant.FILE_SEPARATOR + "output_from_problog.txt";
public static final String FILE_SEPARATOR = System.getProperty("file.separator");

public static final String DEFAULT_PROBLOG_INSTALLATION_DIRECTORY = ResourceConstant.BORN_WORKING_DIRECTORY;
public static final String USER_HOME_DIRECTORY = System.getProperty("user.home");

public static final String BORN_WORKING_DIRECTORY = USER_HOME_DIRECTORY + FILE_SEPARATOR + ".cache" + FILE_SEPARATOR
+ "born";

public static final String DEFAULT_INPUT_FILE_FOR_PROBLOG = BORN_WORKING_DIRECTORY + FILE_SEPARATOR
+ "input_for_problog.txt";

public static final String DEFAULT_OUTPUT_FILE_FROM_PROBLOG = BORN_WORKING_DIRECTORY + FILE_SEPARATOR
+ "output_from_problog.txt";

public static final String DEFAULT_PROBLOG_INSTALLATION_DIRECTORY = BORN_WORKING_DIRECTORY;

public static final String DEFAULT_PROBLOG_ZIP_FILE = BORN_WORKING_DIRECTORY + FILE_SEPARATOR
+ "problog-2.1-SNAPSHOT.zip";

static final String PROBLOG_CLI = "problog-cli.py";
static final String PROBLOG_INSTALL_COMMAND = "install";
static final String PROBLOG_OUTPUT_OPTION = "-o";
static final String PYTHON = "python";

static final String FILE_SEPARATOR = ResourceConstant.FILE_SEPARATOR;
static final String PROBLOG_EXEC_WINDOWS = "problog" + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "windows"
+ FILE_SEPARATOR + "dsharp.exe";
static final String PROBLOG_EXEC_DARWIN = "problog" + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "darwin"
Expand Down Expand Up @@ -158,7 +171,8 @@ public String getProblogDirectory() {
* if the execution was interrupted
*/
public void install(long start) throws IOException, InterruptedException {
ProblogDownloadManager downloadManager = new ProblogDownloadManager();
ProblogDownloadManager downloadManager = new ProblogDownloadManager(DEFAULT_PROBLOG_DOWNLOAD_URI,
DEFAULT_PROBLOG_ZIP_FILE);
downloadManager.downloadIfNecessary();

String directory = decompressProblog(start, downloadManager.getProblogZipFile(),
Expand Down

This file was deleted.

0 comments on commit f77a826

Please sign in to comment.