-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 4 commits
d36c958
03d6c4d
4d09b7e
18b7c3d
88b25ac
e2ab031
6cc5abc
d48ec9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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 without the need to recompile. | ||
|
||
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. | ||
|
||
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-examples/target/gcloud-java-nio-examples-0.1.6-SNAPSHOT.jar com.google.gcloud.nio.examples.ListFilesystems | ||
``` | ||
|
||
Notice that it lists gcs, which it wouldn't if you ran it without the nio jar: | ||
``` | ||
java -cp gcloud-java-contrib/gcloud-java-nio-examples/target/gcloud-java-nio-examples-0.1.6-SNAPSHOT.jar com.google.gcloud.nio.examples.ListFilesystems | ||
``` | ||
|
||
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. | ||
The underlying mechanism is Java's standard [ServiceLoader](https://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html) | ||
facility, the [standard way](http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html) to plug in NIO providers like this one. | ||
|
||
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. |
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-examples</artifactId> | ||
<packaging>jar</packaging> | ||
<name>GCloud Java NIO example</name> | ||
<description> | ||
Demonstrates how to use the gcloud-java-nio jar to add GCS functionality to legacy code. | ||
</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> | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>2.5.4</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
<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,32 @@ | ||
package com.google.gcloud.nio.examples; | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
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; | ||
|
||
/** | ||
* ListFilesystems is a super-simple program that lists the available NIO filesystems. | ||
|
||
*/ | ||
public class ListFilesystems { | ||
|
||
/** | ||
* See the class documentation. | ||
*/ | ||
public static void main(String[] args) throws IOException { | ||
listFilesystems(); | ||
} | ||
|
||
private static void listFilesystems() { | ||
System.out.println("Installed filesystem providers:"); | ||
for (FileSystemProvider p : FileSystemProvider.installedProviders()) { | ||
System.out.println(" " + p.getScheme()); | ||
} | ||
} | ||
|
||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
} |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.