WIP
[hibernate-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / DropMojo.java
1 package de.juplo.plugins.hibernate;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.io.File;
20 import java.security.NoSuchAlgorithmException;
21 import java.util.EnumSet;
22 import java.util.Map;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.hibernate.boot.spi.MetadataImplementor;
26 import org.hibernate.cfg.AvailableSettings;
27 import org.hibernate.engine.config.spi.ConfigurationService;
28 import org.hibernate.service.ServiceRegistry;
29 import org.hibernate.tool.schema.TargetType;
30 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
31 import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
32 import org.hibernate.tool.schema.spi.ExecutionOptions;
33 import org.hibernate.tool.schema.spi.SchemaManagementTool;
34 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
35 import org.hibernate.tool.schema.spi.ScriptTargetOutput;
36 import org.hibernate.tool.schema.spi.TargetDescriptor;
37
38
39 /**
40  * Generate/Execute SQL to drop all tables of a database-schema that represents
41  * the configured mappings.
42  *
43  * @goal drop
44  * @phase process-classes
45  * @threadSafe
46  * @requiresDependencyResolution runtime
47  */
48 public class DropMojo extends AbstractSchemaMojo
49 {
50   /**
51    * Output file.
52    * <p>
53    * If the specified filename is not absolut, the file will be created
54    * relative to the project build directory
55    * (<code>project.build.directory</code>).
56    *
57    * @parameter property="hibernate.schema.drop" default-value="drop.sql"
58    * @since 1.0
59    */
60   private String outputFile;
61
62
63   @Override
64   public final void execute()
65     throws
66       MojoFailureException,
67       MojoExecutionException
68   {
69     try
70     {
71       super.execute(new MD5ModificationTracker(buildDirectory, outputFile, getLog()));
72     }
73     catch (NoSuchAlgorithmException e)
74     {
75       throw new MojoFailureException("Digest-Algorithm MD5 is missing!", e);
76     }
77   }
78
79
80   @Override
81   ExceptionHandlerCollectingImpl build(final MetadataImplementor metadata)
82       throws
83         MojoExecutionException,
84         MojoFailureException
85   {
86     final ServiceRegistry registry =
87         metadata.getMetadataBuildingOptions().getServiceRegistry();
88     final Map settings = 
89         registry.getService(ConfigurationService.class).getSettings();
90     SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
91
92     final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
93     if ((Boolean)settings.get(EXECUTE))
94       targetTypes.add(TargetType.DATABASE);
95
96     TargetDescriptor target = new TargetDescriptor()
97     {
98       @Override
99       public EnumSet<TargetType> getTargetTypes()
100       {
101         return targetTypes;
102       }
103
104       @Override
105       public ScriptTargetOutput getScriptTargetOutput()
106       {
107         String charset =
108             (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
109         return new ScriptTargetOutputToFile(new File(outputFile), charset);
110       }
111     };
112
113     ExceptionHandlerCollectingImpl handler =
114         new ExceptionHandlerCollectingImpl();
115
116     ExecutionOptions options =
117         SchemaManagementToolCoordinator.buildExecutionOptions(
118             registry
119                 .getService(ConfigurationService.class)
120                 .getSettings(),
121             handler
122             );
123
124     Map config = options.getConfigurationValues();
125
126     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
127
128     return handler;
129   }
130 }