2f5927228c026a1872142c0a5b7e75ba8939e344
[hibernate4-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.util.EnumSet;
21 import java.util.Map;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.hibernate.boot.spi.MetadataImplementor;
25 import org.hibernate.cfg.AvailableSettings;
26 import org.hibernate.engine.config.spi.ConfigurationService;
27 import org.hibernate.service.ServiceRegistry;
28 import org.hibernate.tool.schema.TargetType;
29 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
30 import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
31 import org.hibernate.tool.schema.spi.ExecutionOptions;
32 import org.hibernate.tool.schema.spi.SchemaManagementTool;
33 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
34 import org.hibernate.tool.schema.spi.ScriptTargetOutput;
35 import org.hibernate.tool.schema.spi.TargetDescriptor;
36
37
38 /**
39  * Generate/Execute SQL to drop all tables of a database-schema that represents
40  * the configured mappings.
41  *
42  * @goal drop
43  * @phase process-classes
44  * @threadSafe
45  * @requiresDependencyResolution runtime
46  */
47 public class DropMojo extends AbstractSchemaMojo
48 {
49   /**
50    * Output file.
51    * <p>
52    * If the specified filename is not absolut, the file will be created
53    * relative to the project build directory
54    * (<code>project.build.directory</code>).
55    *
56    * @parameter property="hibernate.schema.drop" default-value="drop.sql"
57    * @since 1.0
58    */
59   private String outputFile;
60
61
62   @Override
63   public final void execute()
64     throws
65       MojoFailureException,
66       MojoExecutionException
67   {
68     super.execute(outputFile);
69   }
70
71
72   @Override
73   ExceptionHandlerCollectingImpl build(
74       final MetadataImplementor metadata,
75       final File output
76       )
77       throws
78         MojoExecutionException,
79         MojoFailureException
80   {
81     final ServiceRegistry registry =
82         metadata.getMetadataBuildingOptions().getServiceRegistry();
83     final Map settings = 
84         registry.getService(ConfigurationService.class).getSettings();
85     SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
86
87     final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
88     if ((Boolean)settings.get(EXECUTE))
89       targetTypes.add(TargetType.DATABASE);
90
91     TargetDescriptor target = new TargetDescriptor()
92     {
93       @Override
94       public EnumSet<TargetType> getTargetTypes()
95       {
96         return targetTypes;
97       }
98
99       @Override
100       public ScriptTargetOutput getScriptTargetOutput()
101       {
102         String charset =
103             (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
104         return new ScriptTargetOutputToFile(output, charset);
105       }
106     };
107
108     ExceptionHandlerCollectingImpl handler =
109         new ExceptionHandlerCollectingImpl();
110
111     ExecutionOptions options =
112         SchemaManagementToolCoordinator.buildExecutionOptions(
113             registry
114                 .getService(ConfigurationService.class)
115                 .getSettings(),
116             handler
117             );
118
119     Map config = options.getConfigurationValues();
120
121     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
122
123     return handler;
124   }
125 }