diff options
author | Amadornes <amadornes@gmail.com> | 2018-08-19 00:15:34 +0200 |
---|---|---|
committer | Amadornes <amadornes@gmail.com> | 2018-08-19 00:15:34 +0200 |
commit | 6fd0997b5ddcea041d488c7e02f165cf06b2a375 (patch) | |
tree | 38d57d22f846d87118e8c03a284cc0e81d78439d | |
parent | 5bdc425b044246521849e902290ec8e65812f7be (diff) | |
download | Artifactural-6fd0997b5ddcea041d488c7e02f165cf06b2a375.tar.gz Artifactural-6fd0997b5ddcea041d488c7e02f165cf06b2a375.tar.bz2 Artifactural-6fd0997b5ddcea041d488c7e02f165cf06b2a375.zip |
Added API-level support for cached artifacts
6 files changed, 110 insertions, 17 deletions
diff --git a/src/api/java/com/amadornes/artifactural/api/artifact/Artifact.java b/src/api/java/com/amadornes/artifactural/api/artifact/Artifact.java index c82bbd9..7e797fb 100644 --- a/src/api/java/com/amadornes/artifactural/api/artifact/Artifact.java +++ b/src/api/java/com/amadornes/artifactural/api/artifact/Artifact.java @@ -4,11 +4,8 @@ import com.amadornes.artifactural.api.cache.ArtifactCache; import com.amadornes.artifactural.api.transform.ArtifactTransformer; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; -import java.util.function.UnaryOperator; public interface Artifact { @@ -26,10 +23,22 @@ public interface Artifact { Artifact apply(ArtifactTransformer transformer); - Artifact cache(ArtifactCache cache, String specifier); + Artifact.Cached cache(ArtifactCache cache, String specifier); + + default Artifact.Cached optionallyCache(ArtifactCache cache, String specifier) { + return this instanceof Artifact.Cached ? (Artifact.Cached) this : cache(cache, specifier); + } boolean isPresent(); InputStream openStream() throws IOException, MissingArtifactException; + interface Cached extends Artifact { + + File asFile() throws IOException, MissingArtifactException; + + File getFileLocation() throws IOException, MissingArtifactException; + + } + } diff --git a/src/api/java/com/amadornes/artifactural/api/artifact/Internal.java b/src/api/java/com/amadornes/artifactural/api/artifact/Internal.java index 41201df..9401b00 100644 --- a/src/api/java/com/amadornes/artifactural/api/artifact/Internal.java +++ b/src/api/java/com/amadornes/artifactural/api/artifact/Internal.java @@ -3,10 +3,10 @@ package com.amadornes.artifactural.api.artifact; import com.amadornes.artifactural.api.cache.ArtifactCache; import com.amadornes.artifactural.api.transform.ArtifactTransformer; -import java.io.IOException; +import java.io.File; import java.io.InputStream; -class Internal { +final class Internal { static final ArtifactIdentifier NO_IDENTIFIER = new ArtifactIdentifier() { @@ -37,7 +37,7 @@ class Internal { }; - static final Artifact NO_ARTIFACT = new Artifact() { + static final Artifact NO_ARTIFACT = new Artifact.Cached() { @Override public ArtifactIdentifier getIdentifier() { @@ -65,7 +65,7 @@ class Internal { } @Override - public Artifact cache(ArtifactCache cache, String specifier) { + public Artifact.Cached cache(ArtifactCache cache, String specifier) { return this; } @@ -79,6 +79,16 @@ class Internal { throw new MissingArtifactException(getIdentifier()); } + @Override + public File asFile() throws MissingArtifactException { + throw new MissingArtifactException(getIdentifier()); + } + + @Override + public File getFileLocation() throws MissingArtifactException { + throw new MissingArtifactException(getIdentifier()); + } + }; } diff --git a/src/api/java/com/amadornes/artifactural/api/cache/ArtifactCache.java b/src/api/java/com/amadornes/artifactural/api/cache/ArtifactCache.java index 4f86798..3209656 100644 --- a/src/api/java/com/amadornes/artifactural/api/cache/ArtifactCache.java +++ b/src/api/java/com/amadornes/artifactural/api/cache/ArtifactCache.java @@ -4,6 +4,6 @@ import com.amadornes.artifactural.api.artifact.Artifact; public interface ArtifactCache { - Artifact store(Artifact artifact, String specifier); + Artifact.Cached store(Artifact artifact, String specifier); } diff --git a/src/shared/java/com/amadornes/artifactural/base/artifact/ArtifactBase.java b/src/shared/java/com/amadornes/artifactural/base/artifact/ArtifactBase.java index 4656285..e7dd0c2 100644 --- a/src/shared/java/com/amadornes/artifactural/base/artifact/ArtifactBase.java +++ b/src/shared/java/com/amadornes/artifactural/base/artifact/ArtifactBase.java @@ -13,7 +13,7 @@ public abstract class ArtifactBase implements Artifact { private final ArtifactType type; private final ArtifactMetadata metadata; - ArtifactBase(ArtifactIdentifier identifier, ArtifactType type, ArtifactMetadata metadata) { + public ArtifactBase(ArtifactIdentifier identifier, ArtifactType type, ArtifactMetadata metadata) { this.identifier = identifier; this.type = type; this.metadata = metadata; @@ -41,7 +41,7 @@ public abstract class ArtifactBase implements Artifact { } @Override - public Artifact cache(ArtifactCache cache, String specifier) { + public Artifact.Cached cache(ArtifactCache cache, String specifier) { return cache.store(this, specifier); } diff --git a/src/shared/java/com/amadornes/artifactural/base/cache/ArtifactCacheBase.java b/src/shared/java/com/amadornes/artifactural/base/cache/ArtifactCacheBase.java index 5f1c240..93ca358 100644 --- a/src/shared/java/com/amadornes/artifactural/base/cache/ArtifactCacheBase.java +++ b/src/shared/java/com/amadornes/artifactural/base/cache/ArtifactCacheBase.java @@ -1,16 +1,31 @@ package com.amadornes.artifactural.base.cache; import com.amadornes.artifactural.api.artifact.Artifact; +import com.amadornes.artifactural.api.artifact.ArtifactIdentifier; +import com.amadornes.artifactural.api.artifact.ArtifactMetadata; +import com.amadornes.artifactural.api.artifact.ArtifactType; +import com.amadornes.artifactural.api.artifact.MissingArtifactException; import com.amadornes.artifactural.api.cache.ArtifactCache; +import com.amadornes.artifactural.api.transform.ArtifactTransformer; import com.amadornes.artifactural.base.artifact.StreamableArtifact; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; -abstract class ArtifactCacheBase implements ArtifactCache { +public abstract class ArtifactCacheBase implements ArtifactCache { - Artifact doStore(File path, Artifact artifact) { - return StreamableArtifact.ofStreamable(artifact.getIdentifier(), artifact.getType(), () -> stream(path, artifact)) - .withMetadata(artifact.getMetadata()); + Artifact.Cached doStore(File path, Artifact artifact) { + return wrap( + StreamableArtifact.ofStreamable( + artifact.getIdentifier(), + artifact.getType(), + () -> stream(path, artifact) + ).withMetadata(artifact.getMetadata()), + path + ); } private InputStream stream(File path, Artifact artifact) throws IOException { @@ -30,4 +45,63 @@ abstract class ArtifactCacheBase implements ArtifactCache { return new FileInputStream(path); } + public static Artifact.Cached wrap(Artifact artifact, File file) { + return new Artifact.Cached() { + + @Override + public ArtifactIdentifier getIdentifier() { + return artifact.getIdentifier(); + } + + @Override + public ArtifactMetadata getMetadata() { + return artifact.getMetadata(); + } + + @Override + public ArtifactType getType() { + return artifact.getType(); + } + + @Override + public Artifact withMetadata(ArtifactMetadata metadata) { + return artifact.withMetadata(metadata); + } + + @Override + public Artifact apply(ArtifactTransformer transformer) { + return artifact.apply(transformer); + } + + @Override + public Artifact.Cached cache(ArtifactCache cache, String specifier) { + return artifact.cache(cache, specifier); + } + + @Override + public boolean isPresent() { + return artifact.isPresent(); + } + + @Override + public InputStream openStream() throws IOException, MissingArtifactException { + return artifact.openStream(); + } + + @Override + public File asFile() throws IOException, MissingArtifactException { + if(!file.exists()) { + artifact.openStream().close(); + } + return file; + } + + @Override + public File getFileLocation() throws MissingArtifactException { + return file; + } + + }; + } + } diff --git a/src/shared/java/com/amadornes/artifactural/base/cache/LocatedArtifactCache.java b/src/shared/java/com/amadornes/artifactural/base/cache/LocatedArtifactCache.java index caa5de7..2afbff8 100644 --- a/src/shared/java/com/amadornes/artifactural/base/cache/LocatedArtifactCache.java +++ b/src/shared/java/com/amadornes/artifactural/base/cache/LocatedArtifactCache.java @@ -14,7 +14,7 @@ public class LocatedArtifactCache extends ArtifactCacheBase { } @Override - public Artifact store(Artifact artifact, String specifier) { + public Artifact.Cached store(Artifact artifact, String specifier) { ArtifactIdentifier identifier = artifact.getIdentifier(); File cachePath = new File(path.getAbsolutePath() .replace("${GROUP}", identifier.getGroup()) |