Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split off the "normal case" example from the "legacy case" one. #839

Merged
6 changes: 6 additions & 0 deletions gcloud-java-contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Contents
--------

* [gcloud-java-nio](./gcloud-java-nio/): NIO Filesystem Provider for Google Cloud Storage.
* [gcloud-java-nio-example](./gcloud-java-nio-example/): How to add GCS NIO after the fact.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


See also
--------

* [gcloud-java-examples](../gcloud-java-examples) for an example of how to use NIO normally.

Contributing
------------
Expand Down
38 changes: 38 additions & 0 deletions gcloud-java-contrib/gcloud-java-nio-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Example of adding the Google Cloud Storage NIO Provider to a legacy jar
=======================================================================

This project shows how to add GCS capabilities to a jar file for a Java 7 application
that uses Java NIO but that you cannot recompile.

This comment was marked as spam.

This comment was marked as spam.


Note that whenever possible, you instead want to recompile the app and use the normal
dependency mechanism to add a dependency to gcloud-java-nio. You can see examples of
this in the [gcloud-java-examples](../../gcloud-java-examples) project.

To run this example:

1. Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it.

2. Log in using gcloud SDK (`gcloud auth login` in command line)

3. Compile the JAR with:
```
mvn package -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true
```

4. Run the sample with:

```
java -cp gcloud-java-contrib/gcloud-java-nio/target/gcloud-java-nio-0.1.6-SNAPSHOT-shaded.jar:gcloud-java-contrib/gcloud-java-nio-example/target/gcloud-java-nio-example-0.1.6-SNAPSHOT.jar com.google.gcloud.nio.example.Stat --check

This comment was marked as spam.

This comment was marked as spam.

```
Or, if you have a file in `gs://mybucket/myfile.txt`, you can run:
```
java -cp gcloud-java-contrib/gcloud-java-nio/target/gcloud-java-nio-0.1.6-SNAPSHOT-shaded.jar:gcloud-java-contrib/gcloud-java-nio-example/target/gcloud-java-nio-example-0.1.6-SNAPSHOT.jar com.google.gcloud.nio.example.Stat gs://mybucket/myfile.txt
```

The sample doesn't have anything about GCS in it. It gets that ability from the nio jar that

This comment was marked as spam.

This comment was marked as spam.

we're adding to the classpath. You can use the nio "fat shaded" jar for this purpose as it also
includes the dependencies for gcloud-java-nio.

If you have access to a project's source code you can also simply add gcloud-java-nio as
a dependency and let Maven pull in the required dependencies (this is what the nio unit tests do).
This approach is preferable as the fat jar approach may waste memory on multiple copies of dependencies.
92 changes: 92 additions & 0 deletions gcloud-java-contrib/gcloud-java-nio-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0"?>
<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>com.google.gcloud</groupId>
<artifactId>gcloud-java-nio-example</artifactId>
<packaging>jar</packaging>
<name>GCloud Java NIO example</name>
<description>
Demonstrates how to use the gcloud-java-nio jar to add GCS functionality after the fact.
</description>
<parent>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-contrib</artifactId>
<version>0.1.6-SNAPSHOT</version>
</parent>
<properties>
<site.installationModule>nio</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-storage</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0-rc2</version>
<scope>provided</scope> <!-- to leave out of the all-deps jar -->
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.1</version>
<scope>provided</scope> <!-- to leave out of the all-deps jar -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.27</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.google.gcloud.nio.example;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;

/**
* Stat is a super-simple program that just displays the size of the file
* passed as argument.
*
* <p>It's meant to be used to test GCloud's integration with Java NIO.
*
* <p>You can either use the '--check' argument to see whether GCS is enabled,
* or you can directly pass in a GCS file name to use. In that case you have to
* be logged in (using e.g. the gcloud auth command).
*
* <p>See the README for a command line to run this example.
*
* <p>In short, This version (in gcloud-java-nio-example) is in a package that does not list
* gcloud-java-nio as a dependency, so you have to add the gcloud-java-nio jar to the classpath.

*/
public class Stat {

/**
* See the class documentation.
*/
public static void main(String[] args) throws IOException {
if (args.length == 0 || args[0].equals("--help")) {
help();
return;
}
if (args[0].equals("--list")) {
listFilesystems();
return;
}
if (args[0].equals("--check")) {
checkGcs();
return;
}
for (String a : args) {
statFile(a);
}
}

/**
* Print the length of the indicated file.
*
* <p>This uses the normal Java NIO Api, so it can take advantage of any installed
* NIO Filesystem provider without any extra effort.
*/
private static void statFile(String fname) {
try {
Path path = Paths.get(new URI(fname));
long size = Files.size(path);
System.out.println(fname + ": " + size + " bytes.");
} catch (Exception ex) {
System.out.println(fname + ": " + ex.toString());
}
}

private static void help() {
String[] help =
{"The arguments can be one of:",
" * <path>",
" to display the length of that file.",
"",
" * --list",
" to list the filesystem providers.",
"",
" * --check",
" to double-check the GCS provider is installed.",
"",
"The purpose of this tool is to demonstrate that the gcloud NIO filesystem provider",
"can add Google Cloud Storage support to programs not explicitly designed for it.",
"",
"This tool normally knows nothing of Google Cloud Storage. If you pass it --check",
"or a GCS file name (e.g. gs://mybucket/myfile), it will show an error.",
"However, by just adding the gcloud-nio jar in your classpath, this tool is made",
"aware of gs:// paths and can access files on the cloud.",
"",
"The gcloud NIO filesystem provider can similarly enable existing Java 7 programs",
"to read and write cloud files, even if they have no special built-in cloud support."
};
for (String s : help) {
System.out.println(s);
}
}

private static void listFilesystems() {
System.out.println("Installed filesystem providers:");
for (FileSystemProvider p : FileSystemProvider.installedProviders()) {
System.out.println(" " + p.getScheme());
}
}

private static void checkGcs() {
FileSystem fs = FileSystems.getFileSystem(URI.create("gs://domain-registry-alpha"));
System.out.println("Success! We can instantiate a gs:// filesystem.");
System.out.println("isOpen: " + fs.isOpen());
System.out.println("isReadOnly: " + fs.isReadOnly());
}
}
1 change: 1 addition & 0 deletions gcloud-java-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</properties>
<modules>
<module>gcloud-java-nio</module>
<module>gcloud-java-nio-example</module>
</modules>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

/**
* A Google Cloud Datastore query.
* For usage examples see {@link GqlQuery} and {@link StructuredQuery}.
* For usage example see {@link GqlQuery} and {@link StructuredQuery}.

This comment was marked as spam.

This comment was marked as spam.

*
* Note that queries require proper indexing. See
* <a href="https://cloud.google.com/datastore/docs/tools/indexconfig">
Expand Down
24 changes: 9 additions & 15 deletions gcloud-java-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If you are using Maven, add this to your pom.xml file
```
If you are using Gradle, add this to your dependencies
```Groovy
compile 'com.google.gcloud:gcloud-java-examples:0.1.5'
compile example
```
If you are using SBT, add this to your dependencies
```Scala
Expand Down Expand Up @@ -89,28 +89,22 @@ To run examples from your command line:
mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.storage.StorageExample" -Dexec.args="download <bucket_name> test.txt"
mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.storage.StorageExample" -Dexec.args="delete <bucket_name> test.txt"
```

* Here's an example run of `Stat`, illustrating the use of the gcloud-java-nio jar.

Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it.
Compile the JAR with:
```
mvn package -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true
Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it.
Run the sample with:
```
Then run the sample with:
```
java -cp gcloud-java-contrib/gcloud-java-nio/target/gcloud-java-nio-0.1.6-SNAPSHOT-shaded.jar:gcloud-java-examples/target/gcloud-java-examples-0.1.6-SNAPSHOT.jar com.google.gcloud.examples.nio.Stat --check
mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.nio.Stat" -Dexec.args="--check"

This comment was marked as spam.

This comment was marked as spam.


```
Or, if you have a file in `gs://mybucket/myfile.txt`, you can run:
```
java -cp gcloud-java-contrib/gcloud-java-nio/target/gcloud-java-nio-0.1.6-SNAPSHOT-shaded.jar:gcloud-java-examples/target/gcloud-java-examples-0.1.6-SNAPSHOT.jar com.google.gcloud.examples.nio.Stat gs://mybucket/myfile.txt
mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.nio.Stat" -Dexec.args="gs://mybucket/myfile.txt"
```
The sample doesn't have anything about GCS in it. It gets that ability from the nio jar that
we're adding to the classpath. You can use the nio "fat shaded" jar for this purpose as it also
includes the dependencies for gcloud-java-nio.

If you have access to a project's source code you can also simply add gcloud-java-nio as
a dependency and let Maven pull in the required dependencies (this is what the nio unit tests do).
This approach is preferable as the fat jar approach may waste memory on multiple copies of dependencies.
The sample doesn't have anything special about GCS in it, it just opens files via the NIO API.
It lists gcloud-java-nio as a dependency, and that enables it to interpret `gs://` paths.

Troubleshooting
---------------
Expand Down
5 changes: 5 additions & 0 deletions gcloud-java-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<artifactId>gcloud-java</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-nio</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
* <li>compile using maven - {@code mvn compile}</li>
* <li>run using maven -
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.bigquery.BigQueryExample"
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.example.bigquery.BigQueryExample"

This comment was marked as spam.

This comment was marked as spam.

* -Dexec.args="[<project_id>]
* list datasets |
* list tables <dataset> |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
* <li>compile using maven - {@code mvn compile}</li>
* <li>run using maven - {@code mvn exec:java
* -Dexec.mainClass="com.google.gcloud.examples.datastore.DatastoreExample"
* -Dexec.mainClass="com.google.gcloud.example.datastore.DatastoreExample"
* -Dexec.args="[projectId] [user] [delete|display|add comment]"}</li>
* </ol>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* be logged in (using e.g. the gcloud auth command).
*
* <p>See the README for a command line to run this example.
*

This comment was marked as spam.

This comment was marked as spam.

* <p>In short, this version (in gcloud-java-examples) is in a package that lists gcloud-java-nio
* as a dependency, so it will work directly without having to do any special work.
*/
public class Stat {

Expand Down Expand Up @@ -77,8 +80,8 @@ private static void help() {
"",
"This tool normally knows nothing of Google Cloud Storage. If you pass it --check",
"or a GCS file name (e.g. gs://mybucket/myfile), it will show an error.",
"However, by just adding the gcloud-nio jar in your classpath, this tool is made",
"aware of gs:// paths and can access files on the cloud.",
"However, by just adding the gcloud-nio jar as a dependency and recompiling, this tool is",
"made aware of gs:// paths and can access files on the cloud.",
"",
"The gcloud NIO filesystem provider can similarly enable existing Java 7 programs",
"to read and write cloud files, even if they have no special built-in cloud support."
Expand All @@ -97,7 +100,7 @@ private static void listFilesystems() {

private static void checkGcs() {
FileSystem fs = FileSystems.getFileSystem(URI.create("gs://domain-registry-alpha"));
System.out.println("We seem to be able to instantiate a gs:// filesystem.");
System.out.println("Success! We can instantiate a gs:// filesystem.");
System.out.println("isOpen: " + fs.isOpen());
System.out.println("isReadOnly: " + fs.isReadOnly());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
* <li>compile using maven - {@code mvn compile}</li>
* <li>run using maven - {@code mvn exec:java
* -Dexec.mainClass="com.google.gcloud.examples.resourcemanager.ResourceManagerExample"
* -Dexec.mainClass="com.google.gcloud.example.resourcemanager.ResourceManagerExample"
* -Dexec.args="[list | [create | delete | get] projectId]"}</li>
* </ol>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
* <li>compile using maven - {@code mvn compile}</li>
* <li>run using maven -
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.storage.StorageExample"
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.example.storage.StorageExample"
* -Dexec.args="[<project_id>]
* list [<bucket>] |
* info [<bucket> [<file>]] |
Expand Down
Loading