Skip to content

Commit

Permalink
Code review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrianhill committed Nov 8, 2019
1 parent 1b0d5af commit 1a58d04
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,70 @@ public class ChecksumMetadataExpirationStrategy

public Decision decide(final RepositoryPath repositoryPath) throws IOException
{
Decision decision = decideUsingChecksumAlgorithm(repositoryPath, EncryptionAlgorithmsEnum.SHA1);
if (UNDECIDED.equals(decision))
{
decision = decideUsingChecksumAlgorithm(repositoryPath, EncryptionAlgorithmsEnum.MD5);
}
return decision;
}

private RepositoryPath resolveSiblingChecksum(final RepositoryPath repositoryPath,
final EncryptionAlgorithmsEnum checksumAlgorithm)
{
return repositoryPath.resolveSibling(
repositoryPath.getFileName().toString() + checksumAlgorithm.getExtension());
}

private String readChecksum(final RepositoryPath checksumRepositoryPath)
throws IOException
{
if (!Files.exists(checksumRepositoryPath))
{
return null;
}

RepositoryPath checksumRepositoryPath = resolveSiblingChecksum(repositoryPath, EncryptionAlgorithmsEnum.SHA1);
return Files.readAllLines(checksumRepositoryPath).stream().findFirst().orElse(null);
}

private Decision decideUsingChecksumAlgorithm(final RepositoryPath repositoryPath,
final EncryptionAlgorithmsEnum checksumAlgorithm) throws IOException
{
RepositoryPath checksumRepositoryPath = resolveSiblingChecksum(repositoryPath, checksumAlgorithm);
String currentChecksum = readChecksum(checksumRepositoryPath);
if (currentChecksum == null)
{
checksumRepositoryPath = resolveSiblingChecksum(repositoryPath, EncryptionAlgorithmsEnum.MD5);
currentChecksum = readChecksum(checksumRepositoryPath);
if (currentChecksum == null)
{
logger.debug("Unable to read local checksum for {}, will refetch metadata", repositoryPath.normalize());
return UNDECIDED;
}
logger.debug("Unable to read local {} checksum for {}, returning " + UNDECIDED.name(),
checksumAlgorithm,
repositoryPath.normalize());
return UNDECIDED;
}

proxyRepositoryArtifactResolver.fetchRemoteResource(checksumRepositoryPath);
final String newRemoteChecksum = readChecksum(checksumRepositoryPath);

if (newRemoteChecksum == null)
{
logger.debug("Unable to fetch remote checksum for {}, will refetch metadata", repositoryPath.normalize());
logger.debug("Unable to fetch remote {} checksum for {}, returning " + UNDECIDED.name(),
checksumAlgorithm,
repositoryPath.normalize());
return UNDECIDED;
}

if (currentChecksum.equals(newRemoteChecksum))
{
logger.debug("Local and remote checksums match for {}, no need to refetch metadata",
logger.debug("Local and remote {} checksums match for {}, no need to refetch metadata",
checksumAlgorithm,
repositoryPath.normalize());
return USABLE;
}
else
{
logger.debug("Local and remote checksums differ for {}, will refetch metadata",
logger.debug("Local and remote {} checksums differ for {}, will refetch metadata",
checksumAlgorithm,
repositoryPath.normalize());
return EXPIRED;
}
}

private RepositoryPath resolveSiblingChecksum(final RepositoryPath repositoryPath,
final EncryptionAlgorithmsEnum checksumAlgorithm)
{
return repositoryPath.resolveSibling(
repositoryPath.getFileName().toString() + checksumAlgorithm.getExtension());
}

private String readChecksum(final RepositoryPath checksumRepositoryPath)
throws IOException
{
if (!Files.exists(checksumRepositoryPath))
{
return null;
}

return Files.readAllLines(checksumRepositoryPath).stream().findFirst().orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String describe()
public static MetadataExpirationStrategyType ofStrategy(String strategy)
{
return Stream.of(values())
.filter(e -> e.strategy.equalsIgnoreCase(strategy))
.filter(e -> e.strategy.equals(strategy))
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MavenRepositoryConfigurationDto
// defaults to once daily at 2 am
private String cronExpression = "0 0 2 * * ?";

private String metadataExpirationStrategy = MetadataExpirationStrategyType.CHECKSUM.name();
private String metadataExpirationStrategy = MetadataExpirationStrategyType.CHECKSUM.describe();

@Override
public boolean isIndexingEnabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MavenRepositoryConfigurationForm
private String cronExpression;

@DescribableEnumValue(type = MetadataExpirationStrategyType.class,
message = "metadataExpirationStrategy must contain one the following strings as value: checksum, refresh")
message = "metadataExpirationStrategy must be equal to either: checksum, refresh")
private String metadataExpirationStrategy;

public boolean isIndexingEnabled()
Expand Down

0 comments on commit 1a58d04

Please sign in to comment.