You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by an...@apache.org on 2013/07/28 18:16:08 UTC
[2/2] git commit: Updating the UploadDirectoryToCDN example
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/70f20434
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/tree/70f20434
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/diff/70f20434
Branch: refs/heads/update-cdn-dir-upload
Commit: 70f20434488fb51158b45f33c09f8aa74eee298b
Parents: b9a846b
Author: Andrew Phillips <an...@apache.org>
Authored: Sun Jul 28 11:31:40 2013 -0400
Committer: Andrew Phillips <an...@apache.org>
Committed: Sun Jul 28 11:46:08 2013 -0400
----------------------------------------------------------------------
rackspace/pom.xml | 5 ++
.../cloudfiles/UploadDirectoryToCDN.java | 49 +++++++++++---------
2 files changed, 31 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/70f20434/rackspace/pom.xml
----------------------------------------------------------------------
diff --git a/rackspace/pom.xml b/rackspace/pom.xml
index e3ca9c4..286f203 100644
--- a/rackspace/pom.xml
+++ b/rackspace/pom.xml
@@ -55,6 +55,11 @@
</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-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/70f20434/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..063b14d 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,10 @@
*/
package org.jclouds.examples.rackspace.cloudfiles;
-import static com.google.common.base.Preconditions.checkArgument;
import static java.util.concurrent.Executors.newFixedThreadPool;
+import static org.jclouds.Constants.PROPERTY_PROVIDER;
+import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
+import static com.google.common.base.Preconditions.checkArgument;
import java.io.Closeable;
import java.io.File;
@@ -51,16 +53,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"
+ public static final String PROVIDER = System.getProperty(PROPERTY_PROVIDER, "cloudfiles-us");
+ public static final int THREADS = Integer.getInteger(PROPERTY_IO_WORKER_THREADS, 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 +78,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 +132,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,7 +211,7 @@ public class UploadDirectoryToCDN implements Closeable {
return uploadedBlobDetail;
}
}
-
+
/**
* Example of a FutureCallback triggered with an upload has finished. Just prints out a character to inform
* the user of upload progress.
@@ -221,7 +224,7 @@ public class UploadDirectoryToCDN implements Closeable {
@Override
public void onFailure(Throwable t) {
- System.out.print("X");
+ System.out.print("X " + t);
}
}
@@ -232,15 +235,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;
}