WIP
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / AbstractSchemaMojo.java
index 71e1250..5f6b9bf 100644 (file)
@@ -4,13 +4,10 @@ package de.juplo.plugins.hibernate;
 import com.pyx4j.log4j.MavenLogAppender;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.security.NoSuchAlgorithmException;
-import java.time.ZonedDateTime;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -113,7 +110,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
    * @required
    * @readonly
    */
-  private String buildDirectory;
+  String buildDirectory;
 
 
   /** Parameters to configure the genaration of the SQL *********************/
@@ -428,7 +425,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
 
 
 
-  public final void execute(String filename)
+  public final void execute(ModificationTracker tracker)
     throws
       MojoFailureException,
       MojoExecutionException
@@ -440,16 +437,6 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       return;
     }
 
-    ModificationTracker tracker;
-    try
-    {
-      tracker = new ModificationTracker(buildDirectory, filename, getLog());
-    }
-    catch (NoSuchAlgorithmException e)
-    {
-      throw new MojoFailureException("Digest-Algorithm MD5 is missing!", e);
-    }
-
     final SimpleConnectionProvider connectionProvider =
         new SimpleConnectionProvider(getLog());
 
@@ -494,10 +481,18 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       else
         getLog().debug("Configuration unchanged.");
 
-      /** Check, that the outputfile is writable */
-      final File output = getOutputFile(filename);
       /** Check, if the outputfile is missing or was changed */
-      checkOutputFile(output, tracker);
+      try
+      {
+        tracker.track();
+      }
+      catch (IOException e)
+      {
+        String error =
+            "Error while checking the generated script: " + e.getMessage();
+        getLog().error(error);
+        throw new MojoExecutionException(error);
+      }
 
       /** Configure Hibernate */
       final StandardServiceRegistry serviceRegistry =
@@ -637,12 +632,12 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       /** Truncate output file */
       try
       {
-        new FileOutputStream(output).getChannel().truncate(0).close();
+        tracker.turncat();
       }
       catch (IOException e)
       {
         String error =
-            "Error while truncating " + output.getAbsolutePath() + ": "
+            "Error while truncating output file: " // TODO: Filename unknown here!
             + e.getMessage();
         getLog().warn(error);
         throw new MojoExecutionException(error);
@@ -687,7 +682,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       {
         thread.setContextClassLoader(classLoader);
         ExceptionHandlerCollectingImpl handler =
-            build((MetadataImplementor)metadataBuilder.build(), output);
+            build((MetadataImplementor)metadataBuilder.build());
         if (handler.getExceptions().size() > 0)
         {
           StringBuilder builder = new StringBuilder();
@@ -706,7 +701,17 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       {
         thread.setContextClassLoader(contextClassLoader);
         /** Track, the content of the generated script */
-        checkOutputFile(output, tracker);
+        try
+        {
+          tracker.track();
+        }
+        catch (IOException e)
+        {
+          String error =
+              "Error while checking the generated script: " + e.getMessage();
+          getLog().error(error);
+          throw new MojoExecutionException(error);
+        }
       }
     }
     catch (MojoExecutionException e)
@@ -738,10 +743,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
   }
 
 
-  abstract ExceptionHandlerCollectingImpl build(
-      MetadataImplementor metadata,
-      File file
-      )
+  abstract ExceptionHandlerCollectingImpl build(MetadataImplementor metadata)
     throws
       MojoFailureException,
       MojoExecutionException;
@@ -1064,26 +1066,6 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
     return output;
   }
 
-  private void checkOutputFile(File output, ModificationTracker tracker)
-      throws
-        MojoExecutionException
-  {
-    try
-    {
-      if (output.exists())
-        tracker.track(SCRIPT, new FileInputStream(output));
-      else
-        tracker.track(SCRIPT, ZonedDateTime.now().toString());
-    }
-    catch (IOException e)
-    {
-      String error =
-          "Error while checking the generated script: " + e.getMessage();
-      getLog().error(error);
-      throw new MojoExecutionException(error);
-    }
-  }
-
   private void addMappings(MetadataSources sources, ModificationTracker tracker)
       throws MojoFailureException
   {