1 package org.apache.torque.ant.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.Task;
32 import org.apache.torque.generator.configuration.ConfigurationException;
33 import org.apache.torque.generator.configuration.UnitDescriptor;
34 import org.apache.torque.generator.configuration.UnitDescriptor.Packaging;
35 import org.apache.torque.generator.configuration.controller.Loglevel;
36 import org.apache.torque.generator.configuration.option.MapOptionsConfiguration;
37 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
38 import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
39 import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
40 import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
41 import org.apache.torque.generator.configuration.paths.Maven2JarProjectPaths;
42 import org.apache.torque.generator.configuration.paths.ProjectPaths;
43 import org.apache.torque.generator.control.Controller;
44 import org.apache.torque.generator.file.Fileset;
45 import org.apache.torque.generator.source.stream.FileSourceProvider;
46
47
48
49
50
51
52
53
54 public class TorqueGeneratorTask extends Task
55 {
56
57
58
59
60 private String packaging = "directory";
61
62
63
64
65
66
67 private File projectRootDir = new File(".");
68
69
70
71
72
73 private File configDir;
74
75
76
77
78
79 private String configPackage;
80
81
82
83
84 private File sourceDir;
85
86
87
88
89
90
91
92
93 private Set<String> sourceIncludes;
94
95
96
97
98
99
100
101
102 private Set<String> sourceExcludes;
103
104
105
106
107
108 private File defaultOutputDir = new File("target/generated-sources");
109
110
111
112
113
114 private Map<String, File> outputDirMap = new HashMap<String, File>();
115
116
117
118
119
120 private String jarFile;
121
122
123
124
125
126
127 private File overrideConfigDir;
128
129
130
131
132
133
134 private String loglevel;
135
136
137
138
139
140 private String defaultOutputEncoding;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 private Boolean combineFiles;
164
165
166 private final List<Option> options = new ArrayList<Option>();
167
168
169
170
171
172
173 public Option createOption()
174 {
175 Option option = new Option();
176 options.add(option);
177 return option;
178 }
179
180
181
182
183 @Override
184 public void execute() throws BuildException
185 {
186 Controller controller = new Controller();
187 List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>();
188
189 UnitDescriptor.Packaging packaging;
190 if ("jar".equals(this.packaging))
191 {
192 packaging = UnitDescriptor.Packaging.JAR;
193 }
194 else if ("directory".equals(this.packaging))
195 {
196 packaging = UnitDescriptor.Packaging.DIRECTORY;
197 }
198 else if ("classpath".equals(this.packaging))
199 {
200 packaging = UnitDescriptor.Packaging.CLASSPATH;
201 }
202 else
203 {
204 throw new IllegalArgumentException(
205 "Unknown packaging " + this.packaging
206 + ", must be jar, directory or classpath");
207 }
208 log("Packaging is " + packaging, Project.MSG_DEBUG);
209
210 ProjectPaths defaultProjectPaths;
211 if (UnitDescriptor.Packaging.JAR == packaging)
212 {
213 defaultProjectPaths
214 = new Maven2JarProjectPaths(projectRootDir, jarFile);
215 }
216 else if (UnitDescriptor.Packaging.DIRECTORY == packaging)
217 {
218 defaultProjectPaths
219 = new Maven2DirectoryProjectPaths(projectRootDir);
220 }
221 else if (UnitDescriptor.Packaging.CLASSPATH == packaging)
222 {
223 defaultProjectPaths
224 = new Maven2DirectoryProjectPaths(projectRootDir);
225 }
226 else
227 {
228 throw new IllegalStateException("Unknown packaging" + packaging);
229 }
230
231 CustomProjectPaths projectPaths
232 = new CustomProjectPaths(defaultProjectPaths);
233
234 if (UnitDescriptor.Packaging.CLASSPATH == packaging)
235 {
236 if (configPackage == null)
237 {
238 throw new BuildException(
239 "configPackage must be set for packaging =\"classpath\"");
240 }
241 projectPaths.setConfigurationPackage(configPackage);
242 projectPaths.setConfigurationDir(null);
243 }
244 else
245 {
246 if (configDir != null)
247 {
248 projectPaths.setConfigurationDir(configDir);
249 log("Setting config dir to " + configDir.toString(),
250 Project.MSG_DEBUG);
251 }
252 }
253
254 if (sourceDir != null)
255 {
256 projectPaths.setSourceDir(sourceDir);
257 log("Setting source dir to " + sourceDir.toString(),
258 Project.MSG_DEBUG);
259 }
260
261 FileSourceProvider fileSourceProvider = null;
262 if (sourceIncludes != null || sourceExcludes != null)
263 {
264 Fileset sourceFileset
265 = new Fileset(
266 projectPaths.getDefaultSourcePath(),
267 sourceIncludes,
268 sourceExcludes);
269 log("Setting source includes to " + sourceIncludes,
270 Project.MSG_DEBUG);
271 log("Setting source excludes to " + sourceExcludes,
272 Project.MSG_DEBUG);
273 try
274 {
275 fileSourceProvider = new FileSourceProvider(
276 null,
277 sourceFileset,
278 combineFiles);
279 }
280 catch (ConfigurationException e)
281 {
282 throw new BuildException(
283 "The source provider cannot be instantiated", e);
284 }
285 }
286
287 if (defaultOutputDir != null)
288 {
289 projectPaths.setOutputDirectory(null, defaultOutputDir);
290 log("Setting defaultOutputDir to "
291 + defaultOutputDir.getAbsolutePath(),
292 Project.MSG_DEBUG);
293 }
294 if (outputDirMap != null)
295 {
296 for (Map.Entry<String, File> dirEntry : outputDirMap.entrySet())
297 {
298 projectPaths.setOutputDirectory(
299 dirEntry.getKey(),
300 dirEntry.getValue());
301 log("Setting output directory with key " + dirEntry.getKey()
302 + " to "
303 + dirEntry.getValue(),
304 Project.MSG_DEBUG);
305 }
306 }
307 log("ProjectPaths = " + projectPaths, Project.MSG_DEBUG);
308
309 OptionsConfiguration optionConfiguration = null;
310 if (!options.isEmpty())
311 {
312 Map<String, String> optionsMap = new HashMap<String, String>();
313 for (Option option : options)
314 {
315 optionsMap.put(option.getKey(), option.getValue());
316 }
317 optionConfiguration = new MapOptionsConfiguration(optionsMap);
318 }
319 Loglevel convertedLoglevel = null;
320 if (this.loglevel != null)
321 {
322 convertedLoglevel = Loglevel.getByKey(loglevel);
323 }
324 UnitDescriptor unitDescriptor = new UnitDescriptor(
325 packaging,
326 projectPaths,
327 new DefaultTorqueGeneratorPaths());
328 unitDescriptor.setOverrideSourceProvider(fileSourceProvider);
329 unitDescriptor.setOverrideOptions(optionConfiguration);
330 unitDescriptor.setLoglevel(convertedLoglevel);
331 unitDescriptor.setDefaultOutputEncoding(defaultOutputEncoding);
332 log("unit descriptor created", Project.MSG_DEBUG);
333 if (overrideConfigDir != null)
334 {
335 CustomProjectPaths childProjectPaths
336 = new CustomProjectPaths(projectPaths);
337 childProjectPaths.setConfigurationDir(overrideConfigDir);
338
339 UnitDescriptor parentUnitDescriptor = new UnitDescriptor(
340 Packaging.DIRECTORY,
341 childProjectPaths,
342 new DefaultTorqueGeneratorPaths());
343 parentUnitDescriptor.setInheritsFrom(unitDescriptor);
344 parentUnitDescriptor.setOverrideSourceProvider(fileSourceProvider);
345 parentUnitDescriptor.setOverrideOptions(optionConfiguration);
346 parentUnitDescriptor.setLoglevel(convertedLoglevel);
347 parentUnitDescriptor.setDefaultOutputEncoding(
348 defaultOutputEncoding);
349 log("child unit descriptor created",Project.MSG_DEBUG);
350 unitDescriptor = parentUnitDescriptor;
351 }
352 unitDescriptors.add(unitDescriptor);
353 try
354 {
355 log("Generation started", Project.MSG_DEBUG);
356 controller.run(unitDescriptors);
357 log("Generation successful", Project.MSG_INFO);
358 }
359 catch (Exception e)
360 {
361 log("Error during generation", e, Project.MSG_ERR);
362 throw new BuildException(e.getMessage());
363 }
364 }
365
366
367
368
369
370
371 public void setPackaging(String packaging)
372 {
373 this.packaging = packaging;
374 }
375
376
377
378
379
380
381 public void setProjectRootDir(File projectRootDir)
382 {
383 this.projectRootDir = projectRootDir;
384 }
385
386 public void setConfigDir(File configDir)
387 {
388 this.configDir = configDir;
389 }
390
391 public void setConfigPackage(String configPackage)
392 {
393 this.configPackage = configPackage;
394 }
395
396
397
398
399
400
401
402 public void setDefaultOutputDir(File defaultOutputDir)
403 {
404 this.defaultOutputDir = defaultOutputDir;
405 }
406
407
408
409
410
411
412
413 public void setOutputDirMap(Map<String, File> outputDirMap)
414 {
415 this.outputDirMap = outputDirMap;
416 }
417
418
419
420
421
422
423 public void setJarFile(String jarFile)
424 {
425 this.jarFile = jarFile;
426 }
427
428
429
430
431
432
433 public void setSourceDir(File sourceDir)
434 {
435 this.sourceDir = sourceDir;
436 }
437
438
439
440
441
442
443
444 public void setSourceIncludes(Set<String> sourceIncludes)
445 {
446 this.sourceIncludes = sourceIncludes;
447 }
448
449
450
451
452
453
454
455 public void setSourceExcludes(Set<String> sourceExcludes)
456 {
457 this.sourceExcludes = sourceExcludes;
458 }
459
460
461
462
463
464
465
466
467
468 public void setOverrideConfigDir(File overrideConfigDir)
469 {
470 this.overrideConfigDir = overrideConfigDir;
471 }
472
473
474
475
476
477
478
479
480 public void setLoglevel(String loglevel)
481 {
482 this.loglevel = loglevel;
483 }
484
485
486
487
488
489
490
491
492
493 public void setDefaultOutputEncoding(String defaultOutputEncoding)
494 {
495 this.defaultOutputEncoding = defaultOutputEncoding;
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521 public void setCombineFiles(Boolean combineFiles)
522 {
523 this.combineFiles = combineFiles;
524 }
525 }