sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1491986 - in /sis/branches/JDK7/application/sis-console: ./ src/main/java/org/apache/sis/console/ src/main/resources/org/apache/sis/console/
Date Tue, 11 Jun 2013 21:58:18 GMT
Author: desruisseaux
Date: Tue Jun 11 21:58:17 2013
New Revision: 1491986

URL: http://svn.apache.org/r1491986
Log:
Initial draft of a metadata sub-command.

Modified:
    sis/branches/JDK7/application/sis-console/pom.xml
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
    sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
    sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties

Modified: sis/branches/JDK7/application/sis-console/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/pom.xml?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/pom.xml (original)
+++ sis/branches/JDK7/application/sis-console/pom.xml Tue Jun 11 21:58:17 2013
@@ -98,4 +98,17 @@ Console application.
     </plugins>
   </build>
 
+
+  <!-- ===========================================================
+           Dependencies
+       =========================================================== -->
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.sis.storage</groupId>
+      <artifactId>sis-netcdf</artifactId>
+      <version>${project.version}</version>
+      <!-- TODO: use test or runtime scope -->
+    </dependency>
+  </dependencies>
+
 </project>

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] Tue Jun 11 21:58:17 2013
@@ -20,6 +20,11 @@ import java.util.Locale;
 import java.io.Console;
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.resources.Errors;
 
 
@@ -41,7 +46,7 @@ import org.apache.sis.util.resources.Err
  * @version 0.3
  * @module
  */
-public final class Command implements Runnable {
+public final class Command {
     /**
      * The code given to {@link System#exit(int)} when the program failed because of a unknown
sub-command.
      */
@@ -68,6 +73,12 @@ public final class Command implements Ru
     public static final int SQL_EXCEPTION_EXIT_CODE = 101;
 
     /**
+     * The code given to {@link System#exit(int)} when the program failed for a reason
+     * other than the ones enumerated in the above constants.
+     */
+    public static final int FAILURE_EXIT_CODE = 199;
+
+    /**
      * The sub-command name.
      */
     private final String commandName;
@@ -113,8 +124,9 @@ public final class Command implements Ru
         } else {
             commandName = commandName.toLowerCase(Locale.US);
             switch (commandName) {
-                case "about": command = new AboutSC(commandIndex, args); break;
-                case "help":  command = new HelpSC (commandIndex, args); break;
+                case "help":     command = new HelpSC    (commandIndex, args); break;
+                case "about":    command = new AboutSC   (commandIndex, args); break;
+                case "metadata": command = new MetadataSC(commandIndex, args); break;
                 default: throw new InvalidCommandException(Errors.format(
                             Errors.Keys.UnknownCommand_1, commandName), commandName);
             }
@@ -123,18 +135,47 @@ public final class Command implements Ru
     }
 
     /**
-     * Runs the command.
+     * Runs the command. If an exception occurs, then the exception message is sent to the
error output
+     * stream before to be thrown. Callers can map the exception to a system exit code by
the
+     * {@link #exitCodeFor(Throwable)} method.
+     *
+     * @throws Exception If an error occurred during the command execution. This is typically,
but not limited, to
+     *         {@link IOException}, {@link SQLException}, {@link DataStoreException} or {@link
TransformException}.
      */
-    @Override
-    public void run() {
+    public void run() throws Exception {
         if (command.options.containsKey(Option.HELP)) {
             command.help(commandName);
-        } else {
+        } else try {
             command.run();
+        } catch (Exception e) {
+            command.out.flush();
+            command.err.println(Exceptions.formatChainedMessages(command.locale, null, e));
+            throw e;
         }
     }
 
     /**
+     * Returns the exit code for the given exception, or 0 if unknown. This method iterates
through the
+     * {@linkplain Throwable#getCause() causes} until an exception matching a {@code *_EXIT_CODE}
+     * constant is found.
+     *
+     * @param  cause The exception for which to get the exit code.
+     * @return The exit code as one of the {@code *_EXIT_CODE} constant, or {@link #FAILURE_EXIT_CODE}
if unknown.
+     */
+    public static int exitCodeFor(Throwable cause) {
+        while (cause != null) {
+            if (cause instanceof IOException) {
+                return IO_EXCEPTION_EXIT_CODE;
+            }
+            if (cause instanceof SQLException) {
+                return SQL_EXCEPTION_EXIT_CODE;
+            }
+            cause = cause.getCause();
+        }
+        return FAILURE_EXIT_CODE;
+    }
+
+    /**
      * Prints the message of the given exception. This method is invoked only when the error
occurred before
      * the {@link SubCommand} has been built, otherwise the {@link SubCommand#err} printer
shall be used.
      */
@@ -169,6 +210,10 @@ public final class Command implements Ru
             System.exit(INVALID_OPTION_EXIT_CODE);
             return;
         }
-        c.run();
+        try {
+            c.run();
+        } catch (Exception e) {
+            System.exit(exitCodeFor(e));
+        }
     }
 }

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] Tue Jun 11 21:58:17 2013
@@ -38,7 +38,8 @@ final class HelpSC extends SubCommand {
      */
     private static final String[] COMMANDS = {
         "help",
-        "about"
+        "about",
+        "metadata"
     };
 
     /**

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] Tue Jun 11 21:58:17 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.console;
 
+import java.util.List;
+import java.util.ArrayList;
 import java.util.Locale;
 import java.util.EnumSet;
 import java.util.EnumMap;
@@ -42,7 +44,7 @@ import org.apache.sis.internal.util.X364
  * @version 0.3
  * @module
  */
-abstract class SubCommand implements Runnable {
+abstract class SubCommand {
     /**
      * Special value for {@code arguments[commandIndex]} meaning that this sub-command is
created
      * for JUnit test purpose.
@@ -109,6 +111,12 @@ abstract class SubCommand implements Run
     final StringBuffer outputBuffer;
 
     /**
+     * Any remaining parameters that are not command name or option.
+     * They are typically file names, but can occasionally be other type like URL.
+     */
+    protected final List<String> files;
+
+    /**
      * Creates a new sub-command with the given command-line arguments.
      * The {@code arguments} array is the same array than the one given to the {@code main(String[])}
method.
      * The argument at index {@code commandIndex} is the name of this command, and will be
ignored except for
@@ -125,6 +133,7 @@ abstract class SubCommand implements Run
         boolean isTest = false;
         this.validOptions = validOptions;
         options = new EnumMap<>(Option.class);
+        files = new ArrayList<>(arguments.length);
         for (int i=0; i<arguments.length; i++) {
             final String arg = arguments[i];
             if (i == commandIndex) {
@@ -153,6 +162,8 @@ abstract class SubCommand implements Run
                     throw new InvalidOptionException(Errors.format(Errors.Keys.DuplicatedOption_1,
name), name);
                 }
                 options.put(option, value);
+            } else {
+                files.add(arg);
             }
         }
         /*
@@ -238,7 +249,8 @@ abstract class SubCommand implements Run
 
     /**
      * Executes the sub-command.
+     *
+     * @throws Exception If an error occurred while executing the sub-command.
      */
-    @Override
-    public abstract void run();
+    public abstract void run() throws Exception;
 }

Modified: sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
[ISO-8859-1] Tue Jun 11 21:58:17 2013
@@ -2,5 +2,6 @@
 SIS=Spatial Information System
 Usage=Usage: sis <command> [options] [files]
 
-about=Show information about Apache SIS and system configuration.
 help=Show a help overview.
+about=Show information about Apache SIS and system configuration.
+metadata=Show metadata information for the given file.

Modified: sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties?rev=1491986&r1=1491985&r2=1491986&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
[ISO-8859-1] Tue Jun 11 21:58:17 2013
@@ -2,5 +2,6 @@
 SIS=Syst\u00e8me d\u2019informations spatiales
 Usage=Usage: sis <commande> [options] [fichiers]
 
-about=Affiche des informations \u00e0 propos de Apache SIS et de la configuration du syst\u00e8me.
 help=Affiche un \u00e9cran d\u2019aide.
+about=Affiche des informations \u00e0 propos de Apache SIS et de la configuration du syst\u00e8me.
+metadata=Affiche les m\u00e9ta-donn\u00e9es du fichier sp\u00e9cifi\u00e9.



Mime
View raw message