diff --git a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/BlobStoreProvider.java b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/BlobStoreProvider.java index ba154d56..38093a3a 100644 --- a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/BlobStoreProvider.java +++ b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/BlobStoreProvider.java @@ -29,6 +29,7 @@ import java.net.URI; import java.net.URL; +import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.domain.Blob; import org.kohsuke.accmod.Restricted; @@ -69,6 +70,12 @@ public enum HttpMethod { @NonNull public abstract BlobStoreContext getContext() throws IOException; + /** Return the jclouds Blobstore for working with blob. */ + @NonNull + public BlobStore getBlobStore() throws IOException { + return getContext().getBlobStore(); + } + /** * Get a provider-specific URI. * diff --git a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsArtifactManager.java b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsArtifactManager.java index f6ee5e13..fb687ed9 100644 --- a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsArtifactManager.java +++ b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsArtifactManager.java @@ -62,7 +62,6 @@ import jenkins.util.VirtualFile; import org.apache.http.client.methods.HttpGet; import org.jclouds.blobstore.BlobStore; -import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStores; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.StorageMetadata; @@ -122,7 +121,7 @@ public void archive(FilePath workspace, Launcher launcher, BuildListener listene Map contentTypes = workspace.act(new ContentTypeGuesser(new ArrayList<>(artifacts.keySet()), listener)); LOGGER.fine(() -> "guessing content types: " + contentTypes); Map artifactUrls = new HashMap<>(); - BlobStore blobStore = getContext().getBlobStore(); + BlobStore blobStore = getBlobStore(); // Map artifacts to urls for upload for (Map.Entry entry : artifacts.entrySet()) { @@ -203,7 +202,7 @@ public boolean delete() throws IOException, InterruptedException { LOGGER.log(Level.FINE, "Ignoring blob deletion: {0}", blobPath); return false; } - return JCloudsVirtualFile.delete(provider, getContext().getBlobStore(), blobPath); + return JCloudsVirtualFile.delete(provider, getBlobStore(), blobPath); } @Override @@ -213,7 +212,7 @@ public VirtualFile root() { @Override public void stash(String name, FilePath workspace, Launcher launcher, EnvVars env, TaskListener listener, String includes, String excludes, boolean useDefaultExcludes, boolean allowEmpty) throws IOException, InterruptedException { - BlobStore blobStore = getContext().getBlobStore(); + BlobStore blobStore = getBlobStore(); // Map stash to url for upload String path = getBlobPath("stashes/" + name + ".tgz"); @@ -278,7 +277,7 @@ public Void invoke(File f, VirtualChannel channel) throws IOException, Interrupt @Override public void unstash(String name, FilePath workspace, Launcher launcher, EnvVars env, TaskListener listener) throws IOException, InterruptedException { - BlobStore blobStore = getContext().getBlobStore(); + BlobStore blobStore = getBlobStore(); // Map stash to url for download String blobPath = getBlobPath("stashes/" + name + ".tgz"); @@ -328,7 +327,7 @@ public void clearAllStashes(TaskListener listener) throws IOException, Interrupt return; } - BlobStore blobStore = getContext().getBlobStore(); + BlobStore blobStore = getBlobStore(); int count = 0; try { for (StorageMetadata sm : BlobStores.listAll(blobStore, provider.getContainer(), ListContainerOptions.Builder.prefix(stashPrefix).recursive())) { @@ -352,7 +351,7 @@ public void copyAllArtifactsAndStashes(Run to, TaskListener listener) thro } JCloudsArtifactManager dest = (JCloudsArtifactManager) am; String allPrefix = getBlobPath(""); - BlobStore blobStore = getContext().getBlobStore(); + BlobStore blobStore = getBlobStore(); int count = 0; try { for (StorageMetadata sm : BlobStores.listAll(blobStore, provider.getContainer(), ListContainerOptions.Builder.prefix(allPrefix).recursive())) { @@ -369,8 +368,8 @@ public void copyAllArtifactsAndStashes(Run to, TaskListener listener) thro listener.getLogger().printf("Copied %d artifact(s)/stash(es) from %s to %s%n", count, provider.toURI(provider.getContainer(), allPrefix), provider.toURI(provider.getContainer(), dest.getBlobPath(""))); } - private BlobStoreContext getContext() throws IOException { - return provider.getContext(); + private BlobStore getBlobStore() throws IOException { + return provider.getBlobStore(); } } diff --git a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsVirtualFile.java b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsVirtualFile.java index 00dce6e2..2fbadd0e 100644 --- a/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsVirtualFile.java +++ b/src/main/java/io/jenkins/plugins/artifact_manager_jclouds/JCloudsVirtualFile.java @@ -44,7 +44,6 @@ import java.util.stream.StreamSupport; import org.jclouds.blobstore.BlobStore; -import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStores; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.MutableBlobMetadata; @@ -80,7 +79,7 @@ public class JCloudsVirtualFile extends VirtualFile { @CheckForNull private transient Blob blob; @CheckForNull - private transient BlobStoreContext context; + private transient BlobStore blobStore; public JCloudsVirtualFile(@NonNull BlobStoreProvider provider, @NonNull String container, @NonNull String key) { this.provider = provider; @@ -93,18 +92,18 @@ public JCloudsVirtualFile(@NonNull BlobStoreProvider provider, @NonNull String c private JCloudsVirtualFile(@NonNull JCloudsVirtualFile related, @NonNull String key) { this(related.provider, related.container, key); - context = related.context; + blobStore = related.blobStore; } /** - * Build jclouds blob context that is the base for all operations + * Build jclouds blobstore that is the base for all operations */ @Restricted(NoExternalUse.class) // testing only - BlobStoreContext getContext() throws IOException { - if (context == null) { - context = provider.getContext(); + BlobStore getBlobStore() throws IOException { + if (blobStore == null) { + blobStore = provider.getBlobStore(); } - return context; + return blobStore; } private String getContainer() { @@ -129,9 +128,9 @@ public String getName() { private Blob getBlob() throws IOException { if (blob == null) { LOGGER.log(Level.FINE, "checking for existence of blob {0} / {1}", new Object[] {container, key}); - blob = getContext().getBlobStore().getBlob(getContainer(), getKey()); + blob = getBlobStore().getBlob(getContainer(), getKey()); if (blob == null) { - blob = getContext().getBlobStore().blobBuilder(getKey()).build(); + blob = getBlobStore().blobBuilder(getKey()).build(); blob.getMetadata().setContainer(getContainer()); } } @@ -164,7 +163,7 @@ public boolean isDirectory() throws IOException { return frame.children.keySet().stream().anyMatch(f -> f.startsWith(relSlash)); } LOGGER.log(Level.FINE, "checking directory status {0} / {1}", new Object[] {container, key}); - return !getContext().getBlobStore().list(getContainer(), prefix(key + "/")).isEmpty(); + return !getBlobStore().list(getContainer(), prefix(key + "/")).isEmpty(); } @Override @@ -196,7 +195,7 @@ private Iterable listStorageMetadata(boolean recursive) throws if (recursive) { options.recursive(); } - return BlobStores.listAll(getContext().getBlobStore(), getContainer(), options); + return BlobStores.listAll(getBlobStore(), getContainer(), options); } @Override diff --git a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/NetworkTest.java b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/NetworkTest.java index a166a635..a38c746c 100644 --- a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/NetworkTest.java +++ b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/NetworkTest.java @@ -85,7 +85,7 @@ public class NetworkTest { @Before public void configureManager() throws Exception { MockBlobStore mockBlobStore = new MockBlobStore(); - mockBlobStore.getContext().getBlobStore().createContainerInLocation(null, mockBlobStore.getContainer()); + mockBlobStore.getBlobStore().createContainerInLocation(null, mockBlobStore.getContainer()); ArtifactManagerConfiguration.get().getArtifactManagerFactories().add(new JCloudsArtifactManagerFactory(mockBlobStore)); } diff --git a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/JCloudsArtifactManagerTest.java b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/JCloudsArtifactManagerTest.java index fcda1d3f..a2fedc0a 100644 --- a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/JCloudsArtifactManagerTest.java +++ b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/JCloudsArtifactManagerTest.java @@ -88,6 +88,7 @@ import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jvnet.hudson.test.Issue; +import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.domain.Blob; import org.jenkinsci.plugins.workflow.flow.FlowCopier; @@ -119,7 +120,55 @@ public static void doPrepareImage() throws Exception { public GitSampleRepoRule sampleRepo = new GitSampleRepoRule(); protected ArtifactManagerFactory getArtifactManagerFactory(Boolean deleteArtifacts, Boolean deleteStashes) { - return new JCloudsArtifactManagerFactory(new CustomBehaviorBlobStoreProvider(provider, deleteArtifacts, deleteStashes)); + return new JCloudsArtifactManagerFactory(new CustomPrefixBlobStoreProvider(provider, getPrefix(), deleteArtifacts, deleteStashes)); + } + + private static final class CustomPrefixBlobStoreProvider extends BlobStoreProvider { + private final BlobStoreProvider delegate; + private final String prefix; + private final Boolean deleteArtifacts, deleteStashes; + CustomPrefixBlobStoreProvider(BlobStoreProvider delegate, String prefix, Boolean deleteArtifacts, Boolean deleteStashes) { + this.delegate = delegate; + this.prefix = prefix; + this.deleteArtifacts = deleteArtifacts; + this.deleteStashes = deleteStashes; + } + @Override + public String getPrefix() { + return prefix; + } + @Override + public String getContainer() { + return delegate.getContainer(); + } + @Override + public boolean isDeleteArtifacts() { + return deleteArtifacts != null ? deleteArtifacts : delegate.isDeleteArtifacts(); + } + @Override + public boolean isDeleteStashes() { + return deleteStashes != null ? deleteStashes : delegate.isDeleteStashes(); + } + @Override + public BlobStoreContext getContext() throws IOException { + return delegate.getContext(); + } + @Override + public BlobStore getBlobStore() throws IOException { + return delegate.getBlobStore(); + } + @Override + public URI toURI(String container, String key) { + return delegate.toURI(container, key); + } + @Override + public URL toExternalURL(Blob blob, HttpMethod httpMethod) throws IOException { + return delegate.toExternalURL(blob, httpMethod); + } + @Override + public BlobStoreProviderDescriptor getDescriptor() { + return delegate.getDescriptor(); + } } @Test diff --git a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3AbstractTest.java b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3AbstractTest.java index bf118130..b8a5d99f 100644 --- a/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3AbstractTest.java +++ b/src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3AbstractTest.java @@ -119,7 +119,7 @@ public void setupContext() throws Exception { context = provider.getContext(); - blobStore = context.getBlobStore(); + blobStore = provider.getBlobStore(); setup(); }