WIP
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / UpdateMojo.java
index c5237bd..b64b4b2 100644 (file)
@@ -16,13 +16,23 @@ package de.juplo.plugins.hibernate;
  * limitations under the License.
  */
 
+import java.io.File;
+import java.security.NoSuchAlgorithmException;
+import java.util.EnumSet;
 import java.util.Map;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.hibernate.boot.spi.MetadataImplementor;
+import org.hibernate.cfg.AvailableSettings;
+import org.hibernate.engine.config.spi.ConfigurationService;
 import org.hibernate.service.ServiceRegistry;
+import org.hibernate.tool.schema.TargetType;
+import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
+import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
 import org.hibernate.tool.schema.spi.ExecutionOptions;
 import org.hibernate.tool.schema.spi.SchemaManagementTool;
+import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
+import org.hibernate.tool.schema.spi.ScriptTargetOutput;
 import org.hibernate.tool.schema.spi.TargetDescriptor;
 
 
@@ -56,26 +66,65 @@ public class UpdateMojo extends AbstractSchemaMojo
       MojoFailureException,
       MojoExecutionException
   {
-    super.execute(outputFile);
+    try
+    {
+      super.execute(new MD5ModificationTracker(buildDirectory, outputFile, getLog()));
+    }
+    catch (NoSuchAlgorithmException e)
+    {
+      throw new MojoFailureException("Digest-Algorithm MD5 is missing!", e);
+    }
   }
 
 
   @Override
-  void build(
-      MetadataImplementor metadata,
-      ExecutionOptions options,
-      TargetDescriptor target
-      )
+  ExceptionHandlerCollectingImpl build(final MetadataImplementor metadata)
       throws
         MojoExecutionException,
         MojoFailureException
   {
-    ServiceRegistry service =
+    final ServiceRegistry registry =
         metadata.getMetadataBuildingOptions().getServiceRegistry();
-    SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
+    final Map settings = 
+        registry.getService(ConfigurationService.class).getSettings();
+    SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
+
+    final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
+    if (Boolean.parseBoolean(settings.get(EXECUTE).toString()))
+      targetTypes.add(TargetType.DATABASE);
+
+    TargetDescriptor target = new TargetDescriptor()
+    {
+      @Override
+      public EnumSet<TargetType> getTargetTypes()
+      {
+        return targetTypes;
+      }
+
+      @Override
+      public ScriptTargetOutput getScriptTargetOutput()
+      {
+        String charset =
+            (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
+        return new ScriptTargetOutputToFile(new File(outputFile), charset);
+      }
+    };
+
+    ExceptionHandlerCollectingImpl handler =
+        new ExceptionHandlerCollectingImpl();
+
+    ExecutionOptions options =
+        SchemaManagementToolCoordinator.buildExecutionOptions(
+            registry
+                .getService(ConfigurationService.class)
+                .getSettings(),
+            handler
+            );
 
     Map config = options.getConfigurationValues();
 
     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
+
+    return handler;
   }
 }