db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r530281 - /db/ddlutils/trunk/src/java/org/apache/ddlutils/task/
Date Thu, 19 Apr 2007 05:52:50 GMT
Author: tomdz
Date: Wed Apr 18 22:52:46 2007
New Revision: 530281

URL: http://svn.apache.org/viewvc?view=rev&rev=530281
Log:
Fix for DDLUTILS-167 and general clean up of the error handling in the tasks

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/Command.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDtdToFileCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/Command.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/Command.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/Command.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/Command.java Wed Apr 18 22:52:46 2007
@@ -19,24 +19,81 @@
  * under the License.
  */
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.ddlutils.model.Database;
 import org.apache.tools.ant.BuildException;
 
 /**
- * Base interface for commands that work with a model.
+ * Base class for commands that work with a model.
  * 
  * @version $Revision: 289996 $
  * @ant.type ignore="true"
  */
-public interface Command
+public abstract class Command
 {
+    /** The log. */
+    protected final Log _log = LogFactory.getLog(getClass());
+
+    /** Whether to stop execution upon an error. */
+    private boolean _failOnError = true;
+
+    /**
+     * Determines whether the command execution will be stopped upon an error.
+     * Default value is <code>true</code>.
+     *
+     * @return <code>true</code> if the execution stops in case of an error
+     */
+    public boolean isFailOnError()
+    {
+        return _failOnError;
+    }
+
+    /**
+     * Specifies whether the execution shall stop if an error has occurred during the task
runs.
+     *
+     * @param failOnError <code>true</code> if the execution shall stop in case
of an error
+     * @ant.not-required By default execution will be stopped when an error is encountered.
+     */
+    public void setFailOnError(boolean failOnError)
+    {
+        _failOnError = failOnError;
+    }
+
+    /**
+     * Handles the given exception according to the fail-on-error setting by either
+     * re-throwing it (wrapped in a build exception) or only logging it.
+     * 
+     * @param ex  The exception
+     * @param msg The message to use unless this the exception is rethrown and it is
+     *            already a build exception 
+     */
+    protected void handleException(Exception ex, String msg) throws BuildException
+    {
+        if (isFailOnError())
+        {
+            if (ex instanceof BuildException)
+            {
+                throw (BuildException)ex;
+            }
+            else
+            {
+                throw new BuildException(msg, ex);
+            }
+        }
+        else
+        {
+            _log.error(msg, ex);
+        }
+    }
+    
     /**
      * Specifies whether this command requires a model, i.e. whether the second
      * argument in {@link #execute(DatabaseTaskBase, Database)} cannot be <code>null</code>.
      * 
      * @return <code>true</code> if this command requires a model 
      */
-    public boolean isRequiringModel();
+    public abstract boolean isRequiringModel();
 
     /**
      * Executes this command.
@@ -44,5 +101,5 @@
      * @param task  The executing task
      * @param model The database model
      */
-    public void execute(DatabaseTaskBase task, Database model) throws BuildException;
+    public abstract void execute(DatabaseTaskBase task, Database model) throws BuildException;
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java Wed Apr
18 22:52:46 2007
@@ -93,14 +93,7 @@
         }
         catch (Exception ex)
         {
-            if (isFailOnError())
-            {
-                throw new BuildException(ex);
-            }
-            else
-            {
-                _log.error(ex);
-            }
+            handleException(ex, ex.getMessage());
         }
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java Wed Apr 18 22:52:46
2007
@@ -20,8 +20,6 @@
  */
 
 import org.apache.commons.dbcp.BasicDataSource;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.ddlutils.Platform;
 import org.apache.tools.ant.BuildException;
 
@@ -31,15 +29,10 @@
  * @version $Revision: 289996 $
  * @ant.type ignore="true"
  */
-public abstract class DatabaseCommand implements Command
+public abstract class DatabaseCommand extends Command
 {
-    /** The log. */
-    protected final Log _log = LogFactory.getLog(getClass());
-
     /** The platform configuration. */
     private PlatformConfiguration _platformConf = new PlatformConfiguration();
-    /** Whether to stop execution upon an error. */
-    private boolean _failOnError = true;
 
     /**
      * Returns the database type.
@@ -89,28 +82,6 @@
     protected void setPlatformConfiguration(PlatformConfiguration platformConf)
     {
         _platformConf = platformConf;
-    }
-
-    /**
-     * Determines whether the command execution will be stopped upon an error.
-     * Default value is <code>true</code>.
-     *
-     * @return <code>true</code> if the execution stops in case of an error
-     */
-    public boolean isFailOnError()
-    {
-        return _failOnError;
-    }
-
-    /**
-     * Specifies whether the execution shall stop if an error has occurred during the task
runs.
-     *
-     * @param failOnError <code>true</code> if the execution shall stop in case
of an error
-     * @ant.not-required By default execution will be stopped when an error is encountered.
-     */
-    public void setFailOnError(boolean failOnError)
-    {
-        _failOnError = failOnError;
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java Wed Apr 18
22:52:46 2007
@@ -68,19 +68,12 @@
         }
         catch (UnsupportedOperationException ex)
         {
-            _log.info("Database platform " + platform.getName() + " does not support database
dropping via JDBC",
-                      ex);
+            _log.error("Database platform " + platform.getName() + " does not support database
dropping via JDBC",
+                       ex);
         }
         catch (Exception ex)
         {
-            if (isFailOnError())
-            {
-                throw new BuildException(ex);
-            }
-            else
-            {
-                _log.error(ex);
-            }
+            handleException(ex, ex.getMessage());
         }
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java Wed
Apr 18 22:52:46 2007
@@ -178,14 +178,7 @@
         }
         catch (Exception ex)
         {
-            if (ex instanceof BuildException)
-            {
-                throw (BuildException)ex;
-            }
-            else
-            {
-                throw new BuildException(ex);
-            }
+            handleException(ex, ex.getMessage());
         }
         finally
         {
@@ -203,7 +196,7 @@
      * @param reader   The data reader
      * @param dataFile The schema file
      */
-    private void readSingleDataFile(Task task, DataReader reader, File dataFile)
+    private void readSingleDataFile(Task task, DataReader reader, File dataFile) throws BuildException
     {
         if (!dataFile.exists())
         {
@@ -226,15 +219,7 @@
             }
             catch (Exception ex)
             {
-                if (isFailOnError())
-                {
-                    throw new BuildException("Could not parse or write data file " + dataFile.getAbsolutePath(),
ex);
-                }
-                else
-                {
-                    _log.error("Could not parse or write data file " + dataFile.getAbsolutePath(),
-                               ex);
-                }
+                handleException(ex, "Could not parse or write data file " + dataFile.getAbsolutePath());
             }
         }
     }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java Wed Apr
18 22:52:46 2007
@@ -91,7 +91,7 @@
         }
         catch (Exception ex)
         {
-            throw new BuildException(ex);
+            handleException(ex, ex.getMessage());
         }
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDtdToFileCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDtdToFileCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDtdToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDtdToFileCommand.java Wed Apr
18 22:52:46 2007
@@ -22,8 +22,6 @@
 import java.io.File;
 import java.io.FileWriter;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.ddlutils.io.DataDtdWriter;
 import org.apache.ddlutils.model.Database;
 import org.apache.tools.ant.BuildException;
@@ -36,11 +34,8 @@
  * @version $Revision: 289996 $
  * @ant.task name="writeDtdToFile"
  */
-public class WriteDtdToFileCommand implements Command
+public class WriteDtdToFileCommand extends Command
 {
-    /** The log. */
-    private final Log _log = LogFactory.getLog(getClass());
-
     /** The file to output the DTD to. */
     private File _outputFile;
 
@@ -88,7 +83,7 @@
         }
         catch (Exception ex)
         {
-            throw new BuildException("Failed to write to output file " + _outputFile.getAbsolutePath(),
ex);
+            handleException(ex, "Failed to write to output file " + _outputFile.getAbsolutePath());
         }
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java Wed
Apr 18 22:52:46 2007
@@ -178,14 +178,7 @@
         }
         catch (Exception ex)
         {
-            if (isFailOnError())
-            {
-                throw new BuildException(ex);
-            }
-            else
-            {
-                _log.error(ex);
-            }
+            handleException(ex, ex.getMessage());
         }
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
Wed Apr 18 22:52:46 2007
@@ -61,7 +61,8 @@
     }
 
     /**
-     * Determines whether to drop tables and the associated constraints if necessary.
+     * Determines whether to drop tables and the associated constraints before re-creating
them
+     * (this implies <code>alterDatabase</code> is <code>false</code>).
      * 
      * @return <code>true</code> if drops shall be performed
      */
@@ -131,14 +132,7 @@
         }
         catch (Exception ex)
         {
-            if (isFailOnError())
-            {
-                throw new BuildException(ex);
-            }
-            else
-            {
-                _log.error(ex);
-            }
+            handleException(ex, ex.getMessage());
         }
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java?view=diff&rev=530281&r1=530280&r2=530281
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java Wed
Apr 18 22:52:46 2007
@@ -22,8 +22,6 @@
 import java.io.File;
 import java.io.FileWriter;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.ddlutils.io.DatabaseIO;
 import org.apache.ddlutils.model.Database;
 import org.apache.tools.ant.BuildException;
@@ -35,11 +33,8 @@
  * @version $Revision: 289996 $
  * @ant.task name="writeSchemaToFile"
  */
-public class WriteSchemaToFileCommand implements Command
+public class WriteSchemaToFileCommand extends Command
 {
-    /** The log. */
-    private final Log _log = LogFactory.getLog(getClass());
-
     /** The file to output the schema to. */
     private File _outputFile;
 
@@ -87,7 +82,7 @@
         }
         catch (Exception ex)
         {
-            throw new BuildException("Failed to write to output file " + _outputFile.getAbsolutePath(),
ex);
+            handleException(ex, "Failed to write to output file " + _outputFile.getAbsolutePath());
         }
     }
 }



Mime
View raw message