WIP
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / ValidateMojo.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.Map;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.hibernate.boot.spi.MetadataImplementor;
24 import org.hibernate.engine.config.spi.ConfigurationService;
25 import org.hibernate.service.ServiceRegistry;
26 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
27 import org.hibernate.tool.schema.spi.ExecutionOptions;
28 import org.hibernate.tool.schema.spi.SchemaManagementTool;
29 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
30
31
32 /**
33  * Validate a database-schema against the configured mappings.
34  *
35  * @goal validate
36  * @phase process-classes
37  * @threadSafe
38  * @requiresDependencyResolution runtime
39  */
40 public class ValidateMojo extends AbstractSchemaMojo
41 {
42   @Override
43   public final void execute()
44     throws
45       MojoFailureException,
46       MojoExecutionException
47   {
48     super.execute(null);
49   }
50
51
52   @Override
53   ExceptionHandlerCollectingImpl build(
54       MetadataImplementor metadata,
55       File output
56       )
57       throws
58         MojoExecutionException,
59         MojoFailureException
60   {
61     ServiceRegistry registry =
62         metadata.getMetadataBuildingOptions().getServiceRegistry();
63     SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
64
65     ExceptionHandlerCollectingImpl handler =
66         new ExceptionHandlerCollectingImpl();
67
68     ExecutionOptions options =
69         SchemaManagementToolCoordinator.buildExecutionOptions(
70             registry
71                 .getService(ConfigurationService.class)
72                 .getSettings(),
73             handler
74             );
75     Map config = options.getConfigurationValues();
76
77     tool.getSchemaValidator(config).doValidation(metadata, options);
78
79     return handler;
80   }
81 }