diff options
author | Damien Martin-Guillerez <dmarting@google.com> | 2015-03-24 09:05:08 +0000 |
---|---|---|
committer | Han-Wen Nienhuys <hanwen@google.com> | 2015-03-24 16:41:48 +0000 |
commit | adeef73a97cf191301a7b563883a075caed733b7 (patch) | |
tree | c06734cbffa4160b156779da7c87dbf5cc2b6f94 /examples/java-skylark/src/main/java/com/example/myproject/Greeter.java | |
parent | 5247ff53465afc6ad9db889d1ec3f106ec09a3d6 (diff) |
Move all examples into Bazel workspace
Those examples will be used for Bazel integration tests and their
sources should be available directly to the Bazel workspace itself.
--
MOS_MIGRATED_REVID=89380736
Diffstat (limited to 'examples/java-skylark/src/main/java/com/example/myproject/Greeter.java')
-rw-r--r-- | examples/java-skylark/src/main/java/com/example/myproject/Greeter.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java b/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java new file mode 100644 index 0000000000..b4d8516dc9 --- /dev/null +++ b/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java @@ -0,0 +1,45 @@ +package com.example.myproject; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +/** + * Prints a greeting which can be customized by building with data and/or passing in command- + * line arguments. + */ +public class Greeter { + static PrintStream out = System.out; + + public static String getRunfiles() { + String path = System.getenv("JAVA_RUNFILES"); + if (path == null) { + path = System.getenv("TEST_SRCDIR"); + } + return path; + } + + public static String convertStreamToString(InputStream is) throws Exception { + Scanner s = new Scanner(is).useDelimiter("\n"); + return s.hasNext() ? s.next() : ""; + } + + public void hello(String obj) throws Exception { + String greeting = "Hello"; + try { + String greetFile = getRunfiles() + "/examples/java-skylark/src/main/resources/greeting.txt"; + greeting = convertStreamToString(new FileInputStream(greetFile)); + } catch (FileNotFoundException e) { + // use default. + } + out.println(greeting + " " + obj); + } + + public static void main(String... args) throws Exception { + Greeter g = new Greeter(); + String obj = args.length > 0 ? args[0] : "world"; + g.hello(obj); + } +}; |