Updated Branches: refs/heads/update-cdn-dir-upload ef3ff8e19 -> ea2a630db (forced update)
Updating the UploadDirectoryToCDN example * adding ability to upload to cloudfiles-uk * setting configuration values via system properties * making BlobDetail fully immutable Follow-up to 089bc285 Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/commit/ea2a630d Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/tree/ea2a630d Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/diff/ea2a630d Branch: refs/heads/update-cdn-dir-upload Commit: ea2a630dbc24e8d8deff0850ef962349c8059931 Parents: b9a846b Author: Andrew Phillips <[email protected]> Authored: Sun Jul 28 11:31:40 2013 -0400 Committer: Andrew Phillips <[email protected]> Committed: Mon Jul 29 15:04:55 2013 -0400 ---------------------------------------------------------------------- rackspace/pom.xml | 27 +++++++++++ .../cloudfiles/UploadDirectoryToCDN.java | 49 ++++++++++---------- 2 files changed, 52 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/ea2a630d/rackspace/pom.xml ---------------------------------------------------------------------- diff --git a/rackspace/pom.xml b/rackspace/pom.xml index e3ca9c4..8e6787d 100644 --- a/rackspace/pom.xml +++ b/rackspace/pom.xml @@ -43,6 +43,7 @@ <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> + <!-- US --> <dependency> <groupId>org.apache.jclouds.provider</groupId> <artifactId>rackspace-cloudservers-us</artifactId> @@ -68,5 +69,31 @@ <artifactId>rackspace-cloudloadbalancers-us</artifactId> <version>${jclouds.version}</version> </dependency> + <!-- UK --> + <dependency> + <groupId>org.apache.jclouds.provider</groupId> + <artifactId>rackspace-cloudservers-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> + <dependency> + <groupId>org.apache.jclouds.provider</groupId> + <artifactId>cloudfiles-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> + <dependency> + <groupId>org.apache.jclouds.provider</groupId> + <artifactId>rackspace-cloudblockstorage-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> + <dependency> + <groupId>org.apache.jclouds.provider</groupId> + <artifactId>rackspace-clouddns-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> + <dependency> + <groupId>org.apache.jclouds.provider</groupId> + <artifactId>rackspace-cloudloadbalancers-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/ea2a630d/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java index a0c31c0..6c1d77c 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java @@ -18,8 +18,8 @@ */ package org.jclouds.examples.rackspace.cloudfiles; -import static com.google.common.base.Preconditions.checkArgument; import static java.util.concurrent.Executors.newFixedThreadPool; +import static com.google.common.base.Preconditions.checkArgument; import java.io.Closeable; import java.io.File; @@ -51,16 +51,16 @@ import com.google.common.util.concurrent.MoreExecutors; */ public class UploadDirectoryToCDN implements Closeable { // The provider configures jclouds To use the Rackspace Cloud (US) - // To use the Rackspace Cloud (UK) set the provider to "cloudfiles-uk" - public static final String PROVIDER = "cloudfiles-us"; - public static final int THREADS = 10; - + // To use the Rackspace Cloud (UK) set the system property or default value to "cloudfiles-uk" + private static final String PROVIDER = System.getProperty("cloudfiles.provider", "cloudfiles-us"); + private static final int THREADS = Integer.getInteger("upload.threadpool.size", 10); + private final BlobStore storage; private final CloudFilesClient rackspace; /** * To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/ - * + * * The first argument (args[0]) must be your username * The second argument (args[1]) must be your API key * The third argument (args[2]) must be the path to the local directory @@ -76,17 +76,18 @@ public class UploadDirectoryToCDN implements Closeable { catch (Exception e) { e.printStackTrace(); } - finally { + finally { uploadDirToCDN.close(); } } - + public UploadDirectoryToCDN(String username, String apiKey) { BlobStoreContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildView(BlobStoreContext.class); storage = context.getBlobStore(); - rackspace = context.unwrap(CloudFilesApiMetadata.CONTEXT_TOKEN).getApi(); // can use unwrapApi() in jclouds 1.7.0 + // can use context.unwrapApi(CloudFilesClient.class) in jclouds 1.7 + rackspace = context.unwrap(CloudFilesApiMetadata.CONTEXT_TOKEN).getApi(); } /** @@ -129,33 +130,33 @@ public class UploadDirectoryToCDN implements Closeable { ListeningExecutorService executor = MoreExecutors.listeningDecorator(newFixedThreadPool(THREADS)); List<ListenableFuture<BlobDetail>> blobUploaderFutures = Lists.newArrayList(); BlobUploaderCallback blobUploaderCallback = new BlobUploaderCallback(); - + try { - + for (BlobDetail blobDetail: blobDetails) { BlobUploader blobUploader = new BlobUploader(container, blobDetail); ListenableFuture<BlobDetail> blobDetailFuture = executor.submit(blobUploader); blobUploaderFutures.add(blobDetailFuture); - + Futures.addCallback(blobDetailFuture, blobUploaderCallback); } - + ListenableFuture<List<BlobDetail>> future = Futures.successfulAsList(blobUploaderFutures); List<BlobDetail> uploadedBlobDetails = future.get(); // begin the upload System.out.println(); - + for (int i = 0; i < uploadedBlobDetails.size(); i++) { if (uploadedBlobDetails.get(i) != null) { BlobDetail blobDetail = uploadedBlobDetails.get(i); - System.out.format(" %s (eTag: %s)\n", blobDetail.getRemoteBlobName(), blobDetail.getETag()); + System.out.format(" %s (eTag: %s)%n", blobDetail.getRemoteBlobName(), blobDetail.getETag()); } else { - System.out.format(" %s (ERROR)\n", blobDetails.get(i).getLocalFile().getAbsolutePath()); + System.out.format(" %s (ERROR)%n", blobDetails.get(i).getLocalFile().getAbsolutePath()); } } } - finally { + finally { executor.shutdown(); } } @@ -208,9 +209,9 @@ public class UploadDirectoryToCDN implements Closeable { return uploadedBlobDetail; } } - + /** - * Example of a FutureCallback triggered with an upload has finished. Just prints out a character to inform + * Example of a FutureCallback triggered when an upload has finished. Just prints out a character to inform * the user of upload progress. */ private class BlobUploaderCallback implements FutureCallback<BlobDetail> { @@ -221,7 +222,7 @@ public class UploadDirectoryToCDN implements Closeable { @Override public void onFailure(Throwable t) { - System.out.print("X"); + System.out.print("X " + t); } } @@ -232,15 +233,15 @@ public class UploadDirectoryToCDN implements Closeable { public static class BlobDetail { private final String remoteBlobName; private final File localFile; - private String eTag; + private final String eTag; protected BlobDetail(String remoteBlobName, File localFile) { - this.remoteBlobName = remoteBlobName; - this.localFile = localFile; + this(remoteBlobName, localFile, null); } protected BlobDetail(String remoteBlobName, File localFile, String eTag) { - this(remoteBlobName, localFile); + this.remoteBlobName = remoteBlobName; + this.localFile = localFile; this.eTag = eTag; }
