diff --git a/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java b/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java index 65d620c82..2cc0efcf1 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java @@ -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; @@ -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); } diff --git a/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java b/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java index 5a215afd6..fafefdb4c 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java @@ -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; @@ -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 lines = Files.readAllLines(artifactLog); try { - final List 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 ::::"); + } String gav = splitLine[0]; String hash = splitLine[1]; Path path = Paths.get(splitLine[2]); @@ -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); } } } diff --git a/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java b/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java index 50aef9569..0aaf7384e 100644 --- a/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java +++ b/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java @@ -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; @@ -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));