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