Skip to content

Commit

Permalink
JBEAP-24602: handle badly formatted artifact cache
Browse files Browse the repository at this point in the history
  • Loading branch information
michpetrov committed Jan 22, 2024
1 parent 7a9881c commit 71a6628
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wildfly.prospero.api.exceptions.NoChannelException;
import org.wildfly.prospero.api.exceptions.ProvisioningRuntimeException;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
Expand Down Expand Up @@ -362,4 +363,7 @@ public interface ProsperoLogger extends BasicLogger {
@Message(id = 263, value = "Unable to read the candidate properties file %s.")
@LogMessage(level = Logger.Level.ERROR)
void unableToReadChannelNames(String fileName, @Cause Exception e);

@Message(id = 264, value = "Bad artifact record format in the cache descriptor, line %d: '%s'")
IOException unableToReadArtifactCache(int row, String line, @Cause Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.galleon.util.IoUtils;
import org.jboss.logging.Logger;
import org.wildfly.channel.MavenArtifact;
import org.wildfly.prospero.ProsperoLogger;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;

import java.io.BufferedWriter;
Expand Down Expand Up @@ -201,10 +202,14 @@ private void init() throws IOException {
Path artifactLog = cacheDir.resolve(ArtifactCache.CACHE_FILENAME);

if (Files.exists(artifactLog)) {
int row = 0;
final List<String> lines = Files.readAllLines(artifactLog);
try {
final List<String> lines = Files.readAllLines(artifactLog);
for (String line : lines) {
final String[] splitLine = line.split(ArtifactCache.CACHE_LINE_SEPARATOR);
for ( ; row < lines.size(); row++) {
final String[] splitLine = lines.get(row).split(ArtifactCache.CACHE_LINE_SEPARATOR);
if (splitLine.length < 3) {
throw new IOException("Not enough segments, expected format is <GAV>::<hash>::<path>");
}
String gav = splitLine[0];
String hash = splitLine[1];
Path path = Paths.get(splitLine[2]);
Expand All @@ -213,8 +218,8 @@ private void init() throws IOException {
paths.put(key, installationDir.resolve(path));
hashes.put(key, hash);
}
} catch (MavenUniverseException e) {
throw new IOException("Unable to read cached items.", e);
} catch (MavenUniverseException | IOException e) {
throw ProsperoLogger.ROOT_LOGGER.unableToReadArtifactCache(row + 1, lines.get(row), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.wildfly.channel.MavenArtifact;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -63,6 +65,18 @@ public void testNoCacheFolderReturnsNoArtifacts() throws Exception {
assertEquals(Optional.empty(),cache.getArtifact(GROUP_ID, ARTIFACT_ID, EXTENSION, CLASSIFIER, VERSION));
}

@Test
public void testReadBadlyFormattedFile() throws Exception {
Path newFolder = temp.newFolder().toPath();
Files.createDirectories(newFolder.resolve(ArtifactCache.CACHE_FOLDER));
Files.writeString(newFolder.resolve(ArtifactCache.CACHE_FOLDER).resolve(ArtifactCache.CACHE_FILENAME),"badformat");

assertThatThrownBy(() -> ArtifactCache.getInstance(newFolder))
.isInstanceOf(IOException.class)
.hasMessageContaining("PRSP000264")
.hasStackTraceContaining("Not enough segments");
}

@Test
public void emptyCacheListReturnsNoArtifacts() throws Exception {
assertEquals(Optional.empty(),cache.getArtifact(GROUP_ID, ARTIFACT_ID, EXTENSION, CLASSIFIER, VERSION));
Expand Down

0 comments on commit 71a6628

Please sign in to comment.