use of org.haiku.driversettings.Parameter in project haikudepotserver by haiku.
the class RepositoryHpkrIngressJobRunner method runImportInfoForRepositorySource.
/**
* <p>Each repository has a little "repo.info" file that resides next to the HPKR data.
* This method will pull this in and process the data into the repository source.</p>
*/
private void runImportInfoForRepositorySource(ObjectContext mainContext, RepositorySource repositorySource) throws RepositoryHpkrIngressException {
URL url = repositorySource.tryGetInternalFacingDownloadRepoInfoURL().orElseThrow(() -> new RepositoryHpkrIngressException("unable to download for [" + repositorySource.getCode() + "] as no download repo info url was available"));
// now shift the URL's data into a temporary file and then process it.
File temporaryFile = null;
try {
temporaryFile = File.createTempFile(repositorySource.getCode() + "__import", ".repo-info");
LOGGER.info("will copy data for repository info [{}] ({}) to temporary file", repositorySource, url.toString());
FileHelper.streamUrlDataToFile(url, temporaryFile, TIMEOUT_REPOSITORY_SOURCE_FETCH);
try (InputStream inputStream = new FileInputStream(temporaryFile);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
List<Parameter> parameters = DriverSettings.parse(reader);
String identifierParameterValue = ObjectUtils.firstNonNull(tryGetParameterValue(parameters, PARAMETER_NAME_IDENTIFIER).orElse(null), tryGetParameterValue(parameters, PARAMETER_NAME_URL).orElse(null));
if (StringUtils.isEmpty(identifierParameterValue)) {
throw new DriverSettingsException("expected to find the parameter [" + PARAMETER_NAME_IDENTIFIER + "] or [" + PARAMETER_NAME_URL + "]");
}
if (!Objects.equals(identifierParameterValue, repositorySource.getIdentifier())) {
LOGGER.info("updated the repo info identifier to [{}] for repository source [{}]", identifierParameterValue, repositorySource.getCode());
repositorySource.setIdentifier(identifierParameterValue);
repositorySource.getRepository().setModifyTimestamp();
mainContext.commitChanges();
}
Optional<String> architectureCodeOptional = tryGetParameterValue(parameters, PARAMETER_ARCHITECTURE);
if (architectureCodeOptional.isEmpty()) {
throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has no architecture code");
}
Optional<Architecture> architectureOptional = Architecture.tryGetByCode(mainContext, architectureCodeOptional.get());
if (architectureOptional.isEmpty()) {
throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has unknown architecture code [" + architectureCodeOptional.get() + "]");
}
repositorySource.setArchitecture(architectureOptional.get());
}
} catch (IOException | DriverSettingsException e) {
throw new RepositoryHpkrIngressException("a problem has arisen parsing or dealing with the 'repo.info' file", e);
} finally {
if (null != temporaryFile && temporaryFile.exists()) {
if (!temporaryFile.delete()) {
LOGGER.error("unable to delete the file; {}" + temporaryFile.getAbsolutePath());
}
}
}
}
Aggregations