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(final MetadataImplementor metadata)
54       throws
55         MojoExecutionException,
56         MojoFailureException
57   {
58     ServiceRegistry registry =
59         metadata.getMetadataBuildingOptions().getServiceRegistry();
60     SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
61
62     ExceptionHandlerCollectingImpl handler =
63         new ExceptionHandlerCollectingImpl();
64
65     ExecutionOptions options =
66         SchemaManagementToolCoordinator.buildExecutionOptions(
67             registry
68                 .getService(ConfigurationService.class)
69                 .getSettings(),
70             handler
71             );
72     Map config = options.getConfigurationValues();
73
74     tool.getSchemaValidator(config).doValidation(metadata, options);
75
76     return handler;
77   }
78 }