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