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.ant;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.logging.Level;
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.util.JAXBSource;
39 import javax.xml.transform.Source;
40 import org.apache.tools.ant.BuildException;
41 import org.jomc.model.Implementation;
42 import org.jomc.model.Module;
43 import org.jomc.model.Specification;
44 import org.jomc.modlet.Model;
45 import org.jomc.modlet.ModelContext;
46 import org.jomc.modlet.ModelException;
47 import org.jomc.modlet.ModelValidationReport;
48 import org.jomc.modlet.ObjectFactory;
49 import org.jomc.tools.ClassFileProcessor;
50
51
52
53
54
55
56
57 public final class ValidateClassesTask extends ClassFileProcessorTask
58 {
59
60
61
62
63 private File classesDirectory;
64
65
66
67
68 public ValidateClassesTask()
69 {
70 super();
71 }
72
73
74
75
76
77
78
79
80 public File getClassesDirectory()
81 {
82 return this.classesDirectory;
83 }
84
85
86
87
88
89
90
91
92 public void setClassesDirectory( final File value )
93 {
94 this.classesDirectory = value;
95 }
96
97
98
99
100 @Override
101 public void preExecuteTask() throws BuildException
102 {
103 super.preExecuteTask();
104
105 this.assertNotNull( "classesDirectory", this.getClassesDirectory() );
106 }
107
108
109
110
111
112
113 @Override
114 public void processClassFiles() throws BuildException
115 {
116 ProjectClassLoader classLoader = null;
117 boolean suppressExceptionOnClose = true;
118
119 try
120 {
121 this.log( Messages.getMessage( "validatingModelObjects", this.getModel() ) );
122
123 classLoader = this.newProjectClassLoader();
124 final ModelContext context = this.newModelContext( classLoader );
125 final ClassFileProcessor tool = this.newClassFileProcessor();
126 final JAXBContext jaxbContext = context.createContext( this.getModel() );
127 final Model model = this.getModel( context );
128 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
129 ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
130
131 this.logValidationReport( context, validationReport );
132 tool.setModel( model );
133
134 if ( validationReport.isModelValid() )
135 {
136 final Specification s = this.getSpecification( model );
137 final Implementation i = this.getImplementation( model );
138 final Module m = this.getModule( model );
139
140 if ( s != null )
141 {
142 validationReport = tool.validateModelObjects( s, context, this.getClassesDirectory() );
143
144 if ( validationReport != null )
145 {
146 this.logValidationReport( context, validationReport );
147
148 if ( !validationReport.isModelValid() )
149 {
150 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
151 }
152 }
153 }
154
155 if ( i != null )
156 {
157 validationReport = tool.validateModelObjects( i, context, this.getClassesDirectory() );
158
159 if ( validationReport != null )
160 {
161 this.logValidationReport( context, validationReport );
162
163 if ( !validationReport.isModelValid() )
164 {
165 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
166 }
167 }
168 }
169
170 if ( m != null )
171 {
172 validationReport = tool.validateModelObjects( m, context, this.getClassesDirectory() );
173
174 if ( validationReport != null )
175 {
176 this.logValidationReport( context, validationReport );
177
178 if ( !validationReport.isModelValid() )
179 {
180 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
181 }
182 }
183 }
184
185 if ( this.isModulesProcessingRequested() )
186 {
187 validationReport = tool.validateModelObjects( context, this.getClassesDirectory() );
188
189 if ( validationReport != null )
190 {
191 this.logValidationReport( context, validationReport );
192
193 if ( !validationReport.isModelValid() )
194 {
195 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
196 }
197 }
198 }
199
200 suppressExceptionOnClose = false;
201 }
202 else
203 {
204 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
205 }
206 }
207 catch ( final IOException e )
208 {
209 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
210 }
211 catch ( final JAXBException e )
212 {
213 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
214 }
215 catch ( final ModelException e )
216 {
217 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
218 }
219 finally
220 {
221 try
222 {
223 if ( classLoader != null )
224 {
225 classLoader.close();
226 }
227 }
228 catch ( final IOException e )
229 {
230 if ( suppressExceptionOnClose )
231 {
232 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
233 }
234 else
235 {
236 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
237 }
238 }
239 }
240 }
241
242
243
244
245 @Override
246 public ValidateClassesTask clone()
247 {
248 final ValidateClassesTask clone = (ValidateClassesTask) super.clone();
249 clone.classesDirectory =
250 this.classesDirectory != null ? new File( this.classesDirectory.getAbsolutePath() ) : null;
251
252 return clone;
253 }
254
255 }