Loading ...
Sorry, an error occurred while loading the content.

Updated API

Expand Messages
  • Neal Gafter
    I ve updated the API to repond to a number of comments. David Flanagan s comments were typical of those I am responding to, so I ve included his comments with
    Message 1 of 1 , Aug 14 2:22 PM
    • 0 Attachment
      I've updated the API to repond to a number of comments. David
      Flanagan's comments were typical of those I am responding to,
      so I've included his comments with my responses. The updated
      API is enclosed

      The typical client looks like this:

      import java.tools.*;

      try {
      Tool javac = Tool.find("com.sun.tools.javac");
      PrintWriter out = new PrintWriter(System.out);
      String[] ags = {"-source", "1.5", "/tmp/Foo.java"};
      javac.run(null, out, out, args);
      } catch (ToolNotFoundException ex) {
      // appropriate cleanup
      }

      ///////////////////////////////////////////////////////////////////////////////////////////////
      package java.tools;

      import java.io.PrintWriter;
      import java.io.InputStream;

      /** The set of tools available on a platform is defined by the
      * vendor.
      *
      */
      public abstract class Tool {

      /** Run the tool with the given I/O channels and arguments. By
      * convention a tool returns 0 for success and nonzero for
      * errors.
      */
      public abstract int run(InputStream in, PrintWriter out, PrintWriter err, String... args);

      /** Find a tool, specified by its
      * class loader and name (for example,
      * "com.sun.tools.javac").
      */
      public static final Tool find(ClassLoader loader, String pack) throws ToolNotFoundException {
      try {
      String clazz = pack + ".Main";
      return (Tool)((loader == null)
      ? Class.forName(clazz)
      : loader.loadClass(clazz)).newInstance();
      } catch (ClassCastException ex) {
      throw new ToolNotFoundException(pack, ex);
      } catch (ClassNotFoundException ex) {
      throw new ToolNotFoundException(pack, ex);
      } catch (InstantiationException ex) {
      throw new ToolNotFoundException(pack, ex);
      } catch (IllegalAccessException ex) {
      throw new ToolNotFoundException(pack, ex);
      }
      }

      private static final ClassLoader getToolLoader() {
      // implementation omitted
      return null;
      }
      private static final ClassLoader toolLoader = getToolLoader(); // class loader for tools.jar

      /** Find a tool installed on the platform. */
      public static final Tool find(String pack) throws ToolNotFoundException {
      return find(toolLoader, pack);
      }
      }

      public class ToolNotFoundException extends Exception {
      private final String name;

      public ToolNotFoundException(String name, Throwable cause) {
      super("tool \'" + name + "\' not found", cause);
      this.name = name;
      }

      public String name() {
      return this.name;
      }
      }
    Your message has been successfully submitted and would be delivered to recipients shortly.