You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2014/08/11 08:27:31 UTC

git commit: Add glacier example

Repository: jclouds-examples
Updated Branches:
  refs/heads/master 276f6caa0 -> cae84da83


Add glacier example


Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/cae84da8
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/cae84da8
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/cae84da8

Branch: refs/heads/master
Commit: cae84da833968ba930859201b00e6e97b78ef27b
Parents: 276f6ca
Author: Roman Coedo <ro...@gmail.com>
Authored: Sun Aug 3 18:55:23 2014 +0200
Committer: Andrew Gaul <ga...@apache.org>
Committed: Sun Aug 10 23:26:47 2014 -0700

----------------------------------------------------------------------
 glacier/README.md                               |  17 ++
 glacier/pom.xml                                 |  79 ++++++
 .../src/main/assembly/jar-with-dependencies.xml |  42 ++++
 .../org/jclouds/examples/glacier/MainApp.java   | 238 +++++++++++++++++++
 4 files changed, 376 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/cae84da8/glacier/README.md
----------------------------------------------------------------------
diff --git a/glacier/README.md b/glacier/README.md
new file mode 100755
index 0000000..823f4f1
--- /dev/null
+++ b/glacier/README.md
@@ -0,0 +1,17 @@
+# Glacier
+
+This is a simple example using the Glacier provider and BlobStore.
+
+## Run
+
+Invoke the jar, passing the identity and the credential.
+
+Example:
+
+java -jar target/glacier-jar-with-dependencies.jar accesskey secretkey
+
+## License
+
+Copyright (C) 2009-2014 The Apache Software Foundation
+
+Licensed under the Apache License, Version 2.0

http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/cae84da8/glacier/pom.xml
----------------------------------------------------------------------
diff --git a/glacier/pom.xml b/glacier/pom.xml
new file mode 100644
index 0000000..f0e6782
--- /dev/null
+++ b/glacier/pom.xml
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.jclouds.examples</groupId>
+  <artifactId>glacier</artifactId>
+  <version>1.8.0</version>
+  <name>glacier</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.jclouds.labs</groupId>
+      <artifactId>glacier</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <finalName>${project.artifactId}</finalName>
+    <plugins>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>2.4</version>
+        <configuration>
+          <archive>
+            <manifest>
+              <mainClass>org.jclouds.examples.glacier.MainApp</mainClass>
+            </manifest>
+          </archive>
+        </configuration>
+      </plugin>
+
+      <plugin>
+        <artifactId>maven-assembly-plugin</artifactId>
+        <version>2.2.1</version>
+        <configuration>
+          <descriptors>
+            <descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
+          </descriptors>
+          <archive>
+            <manifest>
+              <mainClass>org.jclouds.examples.glacier.MainApp</mainClass>
+            </manifest>
+          </archive>
+        </configuration>
+        <executions>
+          <execution>
+            <id>make-assembly</id>
+            <phase>package</phase>
+            <goals>
+              <goal>single</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+
+  </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/cae84da8/glacier/src/main/assembly/jar-with-dependencies.xml
----------------------------------------------------------------------
diff --git a/glacier/src/main/assembly/jar-with-dependencies.xml b/glacier/src/main/assembly/jar-with-dependencies.xml
new file mode 100644
index 0000000..f0ea9bc
--- /dev/null
+++ b/glacier/src/main/assembly/jar-with-dependencies.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
+  <!-- copied from jar-with-dependencies (http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies) -->
+  <id>jar-with-dependencies</id>
+  <formats>
+    <format>jar</format>
+  </formats>
+  <includeBaseDirectory>false</includeBaseDirectory>
+  <containerDescriptorHandlers>
+    <containerDescriptorHandler>
+      <handlerName>metaInf-services</handlerName>
+    </containerDescriptorHandler>
+  </containerDescriptorHandlers>
+  <dependencySets>
+    <dependencySet>
+      <outputDirectory>/</outputDirectory>
+      <useProjectArtifact>true</useProjectArtifact>
+      <unpack>true</unpack>
+      <scope>runtime</scope>
+    </dependencySet>
+  </dependencySets>
+</assembly>

http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/cae84da8/glacier/src/main/java/org/jclouds/examples/glacier/MainApp.java
----------------------------------------------------------------------
diff --git a/glacier/src/main/java/org/jclouds/examples/glacier/MainApp.java b/glacier/src/main/java/org/jclouds/examples/glacier/MainApp.java
new file mode 100755
index 0000000..d694312
--- /dev/null
+++ b/glacier/src/main/java/org/jclouds/examples/glacier/MainApp.java
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jclouds.examples.glacier;
+
+import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.InputMismatchException;
+import java.util.Scanner;
+import java.util.UUID;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.blobstore.domain.Blob;
+import org.jclouds.blobstore.options.PutOptions;
+import org.jclouds.glacier.GlacierClient;
+import org.jclouds.glacier.blobstore.strategy.internal.BasePollingStrategy;
+import org.jclouds.glacier.domain.ArchiveRetrievalJobRequest;
+import org.jclouds.glacier.domain.JobRequest;
+import org.jclouds.io.ByteSources;
+import org.jclouds.io.Payload;
+import org.jclouds.io.payloads.ByteSourcePayload;
+import org.jclouds.util.Strings2;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Throwables;
+import com.google.common.io.ByteSource;
+import com.google.common.io.CharStreams;
+
+/**
+ * Demonstrates the use of Glacier provider and BlobStore.
+ *
+ * Usage is: java MainApp "identity" "credential"
+ */
+public class MainApp {
+   private static final long MiB = 1L << 20;
+
+   public static void main(String[] args) throws IOException {
+      if (args.length < 2) {
+         throw new IllegalArgumentException("Invalid number of parameters. Syntax is: \"identity\" \"credential\"");
+      }
+
+      String identity = args[0];
+      String credentials = args[1];
+
+      // Init
+      BlobStoreContext context = ContextBuilder.newBuilder("glacier")
+            .credentials(identity, credentials)
+            .buildView(BlobStoreContext.class);
+
+      try {
+         while (chooseOption(context));
+      } finally {
+         context.close();
+      }
+   }
+
+   private static void putAndRetrieveBlobExample(BlobStore blobstore) throws IOException {
+      // Create a container
+      String containerName = "jclouds_putAndRetrieveBlobExample_" + UUID.randomUUID().toString();
+      blobstore.createContainerInLocation(null, containerName); // Create a vault
+
+      // Create a blob
+      ByteSource payload = ByteSource.wrap("data".getBytes(Charsets.UTF_8));
+      Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
+            .payload(payload)
+            .contentLength(payload.size())
+            .build();
+
+      // Put the blob in the container
+      String blobId = blobstore.putBlob(containerName, blob);
+
+      // Retrieve the blob
+      Blob result = blobstore.getBlob(containerName, blobId);
+
+      // Print the result
+      InputStream is = result.getPayload().openStream();
+      try {
+         String data = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
+         System.out.println("The retrieved payload is: " + data);
+      } finally {
+         is.close();
+      }
+   }
+
+   private static void multipartUploadExample(BlobStore blobstore) throws IOException {
+      // Create a container
+      String containerName = "jclouds_multipartUploadExample_" + UUID.randomUUID().toString();
+      blobstore.createContainerInLocation(null, containerName); // Create a vault
+
+      // Create a blob
+      ByteSource payload = buildData(16 * MiB);
+      Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
+            .payload(payload)
+            .contentLength(payload.size())
+            .build();
+
+      // Create the PutOptions
+      PutOptions options = PutOptions.Builder.multipart(true);
+
+      // Put the blob in the container
+      blobstore.putBlob(containerName, blob, options);
+      System.out.println("The blob has been uploaded");
+   }
+
+   private static void interruptionExample(final BlobStore blobstore) throws IOException {
+      // Create a container
+      final String containerName =  "jclouds_interruptionExample_" + UUID.randomUUID().toString();
+      blobstore.createContainerInLocation(null, containerName); // Create a vault
+
+      // Create a blob
+      ByteSource payload = ByteSource.wrap("data".getBytes(Charsets.UTF_8));
+      Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
+            .payload(payload)
+            .contentLength(payload.size())
+            .build();
+
+      // Put the blob in the container
+      final String blobId = blobstore.putBlob(containerName, blob);
+
+      // New thread
+      Thread thread = new Thread() {
+         public void run() {
+            try {
+               blobstore.getBlob(containerName, blobId);
+            } catch (RuntimeException e) {
+               System.out.println("The request was aborted");
+            }
+         }
+      };
+
+      // Start and interrupt the thread
+      thread.start();
+      thread.interrupt();
+      try {
+         thread.join();
+      } catch (InterruptedException e) {
+         Throwables.propagate(e);
+      }
+   }
+
+   private static void providerExample(BlobStoreContext context) throws IOException {
+      // Get the provider API
+      GlacierClient client = context.unwrapApi(GlacierClient.class);
+
+      // Create a vault
+      final String vaultName =  "jclouds_providerExample_" + UUID.randomUUID().toString();
+      client.createVault(vaultName);
+
+      // Upload an archive
+      Payload payload = new ByteSourcePayload(buildData(16));
+      payload.getContentMetadata().setContentType(PLAIN_TEXT_UTF_8.toString());
+      payload.getContentMetadata().setContentLength(16L);
+      String archiveId = client.uploadArchive(vaultName, payload);
+
+      // Create an archive retrieval job request
+      JobRequest archiveRetrievalJobRequest = ArchiveRetrievalJobRequest.builder()
+            .archiveId(archiveId)
+            .description("retrieval job")
+            .build();
+
+      // Initiate job
+      String jobId = client.initiateJob(vaultName, archiveRetrievalJobRequest);
+      try {
+         // Poll until the job is done
+         new BasePollingStrategy(client).waitForSuccess(vaultName, jobId);
+
+         // Get the job output
+         Payload result = client.getJobOutput(vaultName, jobId);
+
+         // Print the result
+         System.out.println("The retrieved payload is: " + Strings2.toStringAndClose(result.openStream()));
+      } catch (InterruptedException e) {
+         Throwables.propagate(e);
+      }
+   }
+
+   public static boolean chooseOption(BlobStoreContext context) throws IOException {
+      Scanner scan = new Scanner(System.in);
+      System.out.println("");
+      System.out.println("Glacier examples");
+      System.out.println("1. Put and retrieve blob (~4-5 hours)");
+      System.out.println("2. Multipart upload (~1-5 minutes)");
+      System.out.println("3. Call interruption (~0-2 minutes)");
+      System.out.println("4. Provider API (~4-5 hours)");
+      System.out.println("5. Exit");
+      System.out.print("Choose an option: ");
+      try{
+         switch(scan.nextInt()){
+         case 1:
+            putAndRetrieveBlobExample(context.getBlobStore());
+            break;
+         case 2:
+            multipartUploadExample(context.getBlobStore());
+            break;
+         case 3:
+            interruptionExample(context.getBlobStore());
+            break;
+         case 4:
+            providerExample(context);
+            break;
+         case 5:
+            return false;
+         default:
+            System.out.println("Not a valid option");
+            break;
+         }
+      }
+      catch(InputMismatchException e) {
+         System.out.println("Not a valid option");
+      }
+      return true;
+   }
+
+   private static ByteSource buildData(long size) {
+      byte[] array = new byte[1024];
+      Arrays.fill(array, (byte) 'a');
+      return ByteSources.repeatingArrayByteSource(array).slice(0, size);
+   }
+}