Author: cutting
Date: Tue Nov 24 19:13:30 2009
New Revision: 883819

URL: http://svn.apache.org/viewvc?rev=883819&view=rev
Log:
AVRO-233.  Elaborate Java tool API.  Contributed by Philip Zeyliger.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java
    hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
    hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java
    hadoop/avro/trunk/src/java/org/apache/avro/tool/Tool.java
    hadoop/avro/trunk/src/test/bin/test_avroj.sh

Modified: hadoop/avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Nov 24 19:13:30 2009
@@ -101,6 +101,8 @@
 
     AVRO-213. Add Apache RAT to tests, to validate licenses.  (cutting)
 
+    AVRO-233. Elaborate Java tool API. (Philip Zeyliger via cutting)
+
   OPTIMIZATIONS
 
     AVRO-172. More efficient schema processing (massie)

Modified: 
hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java 
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/InduceSchemaTool.java 
Tue Nov 24 19:13:30 2009
@@ -18,6 +18,8 @@
 package org.apache.avro.reflect;
 
 import java.io.File;
+import java.io.InputStream;
+import java.io.PrintStream;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.List;
@@ -31,7 +33,8 @@
 public class InduceSchemaTool implements Tool {
 
   @Override
-  public void run(List<String> args) throws Exception {
+  public void run(InputStream in, PrintStream out, PrintStream err,
+      List<String> args) throws Exception {
     if (args.size() == 0 || args.size() > 2) {
       System.err.println("Usage: [colon-delimited-classpath] classname");
       return;
@@ -56,4 +59,14 @@
     Class<?> klass = classLoader.loadClass(className);
     System.out.println(ReflectData.get().getSchema(klass).toString());
   }
+
+  @Override
+  public String getName() {
+    return "induce";
+  }
+
+  @Override
+  public String getShortDescription() {
+    return "Uses reflection to induce schema for a given class.";
+  }
 }

Modified: 
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java 
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java 
Tue Nov 24 19:13:30 2009
@@ -20,6 +20,8 @@
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -388,7 +390,8 @@
    */
   public static class SpecificCompilerTool implements Tool {
     @Override
-    public void run(List<String> args) throws IOException {
+    public void run(InputStream in, PrintStream out, PrintStream err,
+        List<String> args) throws Exception {
       if (args.size() != 3) {
         System.err.println("Expected 3 arguments: (schema|protocol) inputfile 
outputdir");
         return;
@@ -405,6 +408,16 @@
         return;
       }
     }
+
+    @Override
+    public String getName() {
+      return "compile";
+    }
+
+    @Override
+    public String getShortDescription() {
+      return "Generates Java code for the given schema.";
+    }
   }
 
 }

Modified: hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/tool/Main.java Tue Nov 24 
19:13:30 2009
@@ -31,10 +31,21 @@
    */
   private final Map<String, Tool> tools;
 
+  int maxLen = 0;
+
   private Main() {
     tools = new TreeMap<String, Tool>();
-    tools.put("compile", new SpecificCompilerTool());
-    tools.put("induce", new InduceSchemaTool());
+    for (Tool tool : new Tool[] {
+        new SpecificCompilerTool(),
+        new InduceSchemaTool()
+        }) {
+      Tool prev = tools.put(tool.getName(), tool);
+      if (prev != null) {
+        throw new AssertionError(
+            "Two tools with identical names: " + tool + ", " + prev);
+      }
+      maxLen = Math.max(tool.getName().length(), maxLen);
+    }
   }
 
   public static void main(String[] args) throws Exception {
@@ -48,13 +59,13 @@
     if (args.length != 0) {
       Tool tool = tools.get(args[0]);
       if (tool != null) {
-        tool.run(Arrays.asList(args).subList(1, args.length));
+        tool.run(System.in, System.out, System.err, 
Arrays.asList(args).subList(1, args.length));
         return;
       }
     }
-    System.err.println("Expected one of the following arguments:");
-    for (String k : tools.keySet()) {
-      System.err.println("\t" + k);
+    System.err.println("Available tools:");
+    for (Tool k : tools.values()) {
+      System.err.printf("%" + maxLen + "s  %s\n", k.getName(), 
k.getShortDescription());
     }
   }
 }

Modified: hadoop/avro/trunk/src/java/org/apache/avro/tool/Tool.java
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/tool/Tool.java?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/tool/Tool.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/tool/Tool.java Tue Nov 24 
19:13:30 2009
@@ -17,6 +17,9 @@
  */
 package org.apache.avro.tool;
 
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.util.List;
 
 /**
@@ -25,10 +28,24 @@
  */
 public interface Tool {
   /**
-   * Runs the tool with supplied arguments.
+   * Runs the tool with supplied arguments.  Input and output streams
+   * are customizable for easier testing.
    *
+   * @param in Input stream to read data (typically System.in).
+   * @param out Output of tool (typically System.out).
+   * @param err Error stream (typically System.err).
    * @param args Non-null list of arguments.
    * @throws Exception Just like main(), tools may throw Exception.
    */
-  void run(List<String> args) throws Exception;
-}
+  void run(InputStream in, PrintStream out, PrintStream err, List<String> 
args) throws Exception;
+
+  /**
+   * Name of tool, to be used in listings.
+   */
+  String getName();
+
+  /**
+   * 1-line description to be used in command listings.
+   */
+  String getShortDescription();
+}
\ No newline at end of file

Modified: hadoop/avro/trunk/src/test/bin/test_avroj.sh
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/bin/test_avroj.sh?rev=883819&r1=883818&r2=883819&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/bin/test_avroj.sh (original)
+++ hadoop/avro/trunk/src/test/bin/test_avroj.sh Tue Nov 24 19:13:30 2009
@@ -55,8 +55,8 @@
     | awk -F "/" '{ print $NF }' | sort | tr '\n' ' ')" ]
 ######################################################################
 echo "Testing induce schema..."
-$AVROJ induce build/test/classes org.apache.avro.BarRecord | grep -q -F \
+$CMD induce build/test/classes org.apache.avro.BarRecord | grep -q -F \
   
'{"type":"record","name":"BarRecord","namespace":"org.apache.avro","fields":[{"name":"beerMsg","type":"string"}]}'
 ######################################################################
-$CMD 2>&1 | grep -q "Expected one of the following"
-$CMD doesnotexist 2>&1 | grep -q "Expected one of the following"
+$CMD 2>&1 | grep -q "Available tools:"
+$CMD doesnotexist 2>&1 | grep -q "Available tools:"


Reply via email to