1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.mojo;
32
33 import java.io.File;
34 import java.util.logging.Level;
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.util.JAXBSource;
37 import javax.xml.transform.Source;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.jomc.model.Module;
40 import org.jomc.modlet.ModelContext;
41 import org.jomc.modlet.ModelValidationReport;
42 import org.jomc.modlet.ObjectFactory;
43 import org.jomc.tools.ClassFileProcessor;
44
45
46
47
48
49
50
51 public abstract class AbstractClassesValidateMojo extends AbstractJomcMojo
52 {
53
54
55 private static final String TOOLNAME = "ClassFileProcessor";
56
57
58 public AbstractClassesValidateMojo()
59 {
60 super();
61 }
62
63 @Override
64 protected final void executeTool() throws Exception
65 {
66 this.logSeparator();
67
68 if ( this.isClassProcessingEnabled() )
69 {
70 this.logProcessingModule( TOOLNAME, this.getClassesModuleName() );
71
72 final ModelContext context = this.createModelContext( this.getClassesClassLoader() );
73 final ClassFileProcessor tool = this.createClassFileProcessor( context );
74 final JAXBContext jaxbContext = context.createContext( this.getModel() );
75 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
76 ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
77 this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
78
79 if ( validationReport.isModelValid() )
80 {
81 final Module module =
82 tool.getModules() != null ? tool.getModules().getModule( this.getClassesModuleName() ) : null;
83
84 if ( module != null )
85 {
86 validationReport = tool.validateModelObjects( module, context, this.getClassesDirectory() );
87
88 if ( validationReport != null )
89 {
90 this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE,
91 validationReport );
92
93 if ( !validationReport.isModelValid() )
94 {
95 throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
96 }
97 }
98
99 this.logToolSuccess( TOOLNAME );
100 }
101 else
102 {
103 this.logMissingModule( this.getClassesModuleName() );
104 }
105 }
106 else
107 {
108 throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
109 }
110 }
111 else if ( this.isLoggable( Level.INFO ) )
112 {
113 this.log( Level.INFO, Messages.getMessage( "classFileValidationDisabled" ), null );
114 }
115 }
116
117
118
119
120
121
122
123
124 protected abstract String getClassesModuleName() throws MojoExecutionException;
125
126
127
128
129
130
131
132
133 protected abstract ClassLoader getClassesClassLoader() throws MojoExecutionException;
134
135
136
137
138
139
140
141
142 protected abstract File getClassesDirectory() throws MojoExecutionException;
143
144 }